-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.ts
More file actions
210 lines (193 loc) · 5.74 KB
/
index.ts
File metadata and controls
210 lines (193 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import * as R from 'remeda'
import type { Simplify } from 'type-fest'
import { type AuthContext, createAuthContext } from './context'
import { createAuthHandlers } from './handlers'
import { getFieldsClient, type MinimalContext } from '../config'
import type { ApiRouteHandler } from '../endpoint'
import type { Fields, FieldsClient } from '../field'
import type { AnyTypedColumn, WithAnyTable, WithHasDefault, WithNotNull } from '../table'
export type AnyUserTable = WithAnyTable<{
id: WithHasDefault<WithNotNull<AnyTypedColumn<string>>>
name: WithNotNull<AnyTypedColumn<string>>
email: WithNotNull<AnyTypedColumn<string>>
emailVerified: WithNotNull<AnyTypedColumn<boolean>>
image: AnyTypedColumn<string>
}>
export type AnySessionTable = WithAnyTable<{
id: WithHasDefault<WithNotNull<AnyTypedColumn<string>>>
expiresAt: WithNotNull<AnyTypedColumn<Date>>
token: WithNotNull<AnyTypedColumn<string>>
ipAddress: AnyTypedColumn<string>
userAgent: AnyTypedColumn<string>
userId: WithNotNull<AnyTypedColumn<string>>
}>
export type AnyAccountTable = WithAnyTable<{
id: WithHasDefault<WithNotNull<AnyTypedColumn<string>>>
accountId: WithNotNull<AnyTypedColumn<string>>
providerId: WithNotNull<AnyTypedColumn<string>>
userId: WithNotNull<AnyTypedColumn<string>>
accessToken: AnyTypedColumn<string>
refreshToken: AnyTypedColumn<string>
idToken: AnyTypedColumn<string>
accessTokenExpiresAt: AnyTypedColumn<Date>
refreshTokenExpiresAt: AnyTypedColumn<Date>
scope: AnyTypedColumn<string>
password: AnyTypedColumn<string>
}>
export type AnyVerificationTable = WithAnyTable<{
id: WithHasDefault<WithNotNull<AnyTypedColumn<string>>>
identifier: WithNotNull<AnyTypedColumn<string>>
value: AnyTypedColumn<string>
expiresAt: WithNotNull<AnyTypedColumn<Date>>
}>
export interface AuthConfig {
secret: string
user: {
model: AnyUserTable
}
session: {
model: AnySessionTable
cookiePrefix?: string
}
account: {
model: AnyAccountTable
}
verification: {
model: AnyVerificationTable
}
emailAndPassword?: {
enabled: boolean
passwordHasher?: (password: string) => Promise<string> // default: scrypt
signUp?: {
autoLogin?: boolean // default: true
additionalFields?: Fields<any>
}
}
oauth2?: {
google?: {
enabled: boolean
clientId: string
clientSecret: string
}
}
resetPassword?: {
enabled?: boolean // default: false
expiresInMs?: number // default: 1 day (1000 * 60 * 60 * 24)
resetPasswordUrl?: string // default: `/auth/reset-password`
redirectTo?: string // default: `/auth/login`
sendEmailResetPassword: (email: string, token: string) => Promise<void>
}
ui?: {
login?: {
strategies?: { style?: string; text: string; href: string }[]
}
signUp?: {}
}
}
type ChangeAuthHandlerContextToMinimalContext<
TContext extends MinimalContext,
THandlers extends Record<string, { handler: ApiRouteHandler<any, any> }>,
> = {
[K in keyof THandlers]: THandlers[K]['handler'] extends ApiRouteHandler<
any,
infer TApiRouteSchema
>
? { schema: TApiRouteSchema; handler: ApiRouteHandler<TContext, TApiRouteSchema> }
: never
}
type AddObjectKeyPrefix<T extends Record<string, any>, TPrefix extends string> = Simplify<{
[K in keyof T as K extends string ? `${TPrefix}.${K}` : never]: T[K]
}>
export type Auth<
TContext extends MinimalContext = MinimalContext,
TAuthConfig extends AuthConfig = AuthConfig,
> = {
config: TAuthConfig
context: TContext
authContext: AuthContext
handlers: Simplify<
AddObjectKeyPrefix<
ChangeAuthHandlerContextToMinimalContext<
TContext,
ReturnType<typeof createAuthHandlers>['handlers']
>,
'auth'
>
>
}
export type AuthHandlers = Auth<any, any>['handlers']
export type AuthClient = {
login: {
emailAndPassword: {
enabled: boolean
}
}
resetPassword: {
enabled: boolean
redirectTo: string
}
ui: {
login: {
strategies: { style?: string; text: string; href: string }[]
}
signUp: {
autoLogin?: boolean
additionalFields: FieldsClient
}
}
}
export function createAuth<TContext extends MinimalContext<any> = MinimalContext<any>>(
config: AuthConfig,
context: TContext
): Auth<TContext> {
const authContext = createAuthContext(config, context)
const { handlers: originalHandlers } = createAuthHandlers(config)
const handlers = R.mapValues(originalHandlers, (h) => {
const handler: ApiRouteHandler<TContext, any> = (args) => {
return h.handler({ ...args, context: authContext } as any) as any
}
return { schema: h.schema, handler }
}) as ChangeAuthHandlerContextToMinimalContext<
TContext,
ReturnType<typeof createAuthHandlers>['handlers']
>
const prefixedHandlers = Object.fromEntries(
Object.entries(handlers).map(([key, value]) => {
return [`auth.${key}`, value]
})
) as unknown as AddObjectKeyPrefix<
ChangeAuthHandlerContextToMinimalContext<
TContext,
ReturnType<typeof createAuthHandlers>['handlers']
>,
'auth'
>
return {
config,
context,
authContext,
handlers: prefixedHandlers,
}
}
export function getAuthClient(config: AuthConfig): AuthClient {
return {
login: {
emailAndPassword: {
enabled: config.emailAndPassword?.enabled ?? false,
},
},
resetPassword: {
enabled: config.resetPassword?.enabled ?? false,
redirectTo: config.resetPassword?.redirectTo ?? '/auth/login',
},
ui: {
login: {
strategies: config.ui?.login?.strategies ?? [],
},
signUp: {
autoLogin: config.emailAndPassword?.signUp?.autoLogin ?? true,
additionalFields: getFieldsClient(config.emailAndPassword?.signUp?.additionalFields ?? {}),
},
},
}
}