返回 AiToEarn
auth.utils.ts
1 import type { Locale } from '@yikart/common'
2 import type { CookieOptions, Response } from 'express'
3 import { CookieName } from '@yikart/common'
4 import { config } from '../../../config'
5
6 export interface AuthViewMessages {
7 title: string
8 selectTitle: string
9 selectDescription: string
10 accountCount: string
11 selectedCount: string
12 noAccountsTitle: string
13 noAccountsDescription: string
14 continueButton: string
15 selectRequired: string
16 connectedTitle: string
17 redirectingDescription: string
18 closeDescription: string
19 connectedAccounts: string
20 accountId: string
21 platformUid: string
22 account: string
23 failedTitle: string
24 failedDescription: string
25 errorCode: string
26 }
27
28 export const authViewMessages: Record<Locale, AuthViewMessages> = {
29 'en-US': {
30 title: 'Authorization',
31 selectTitle: 'Choose accounts to connect',
32 selectDescription: 'Select the accounts you want AiToEarn to manage for publishing and analytics.',
33 accountCount: 'Available accounts',
34 selectedCount: 'Selected',
35 noAccountsTitle: 'No accounts available',
36 noAccountsDescription: 'This authorization did not return any accounts that can be connected.',
37 continueButton: 'Continue',
38 selectRequired: 'Select at least one account to continue.',
39 connectedTitle: 'Authorization completed',
40 redirectingDescription: 'Redirecting you back now.',
41 closeDescription: 'You can close this page now.',
42 connectedAccounts: 'Connected accounts',
43 accountId: 'Account ID',
44 platformUid: 'Platform ID',
45 account: 'Account',
46 failedTitle: 'Authorization failed',
47 failedDescription: 'Please close this page and try authorizing again.',
48 errorCode: 'Error code',
49 },
50 'zh-CN': {
51 title: '授权',
52 selectTitle: '选择要连接的账号',
53 selectDescription: '请选择需要交给 AiToEarn 管理发布和数据的账号。',
54 accountCount: '可连接账号',
55 selectedCount: '已选择',
56 noAccountsTitle: '没有可连接的账号',
57 noAccountsDescription: '本次授权没有返回可连接的账号。',
58 continueButton: '继续',
59 selectRequired: '请至少选择一个账号后继续。',
60 connectedTitle: '授权已完成',
61 redirectingDescription: '正在跳转回应用。',
62 closeDescription: '现在可以关闭此页面。',
63 connectedAccounts: '已连接账号',
64 accountId: '账号 ID',
65 platformUid: '平台 ID',
66 account: '账号',
67 failedTitle: '授权失败',
68 failedDescription: '请关闭此页面后重新发起授权。',
69 errorCode: '错误码',
70 },
71 }
72
73 export function setChannelAuthSessionCookie(res: Response, sessionId: string, expiresAt?: Date) {
74 const maxAge = expiresAt
75 ? Math.max(expiresAt.getTime() - Date.now(), 1000)
76 : 5 * 60 * 1000
77
78 res.cookie(CookieName.ChannelAuthSession, sessionId, {
79 ...getChannelSessionCookieOptions(),
80 maxAge,
81 })
82 }
83
84 export function clearChannelAuthSessionCookie(res: Response) {
85 res.clearCookie(CookieName.ChannelAuthSession, getChannelSessionCookieOptions())
86 }
87
88 function getChannelSessionCookieOptions(): CookieOptions {
89 return {
90 httpOnly: true,
91 sameSite: 'lax',
92 secure: config.environment === 'production',
93 path: '/api/v2/channels',
94 }
95 }
96
96 lines TYPESCRIPT