-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreateApi.ts
More file actions
55 lines (54 loc) · 2.06 KB
/
createApi.ts
File metadata and controls
55 lines (54 loc) · 2.06 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
import { validateRedirectTo } from "@/shared/utils.ts"
import { getSession, signIn, signInCredentials, signOut, updateSession } from "@/api/index.ts"
import type { GlobalContext } from "@aura-stack/router"
import type {
BuiltInOAuthProvider,
LiteralUnion,
GetSessionAPIOptions,
SessionResponse,
SignInAPIOptions,
SignInReturn,
SignOutAPIOptions,
UpdateSessionAPIOptions,
User,
SignInCredentialsAPIOptions,
} from "@/@types/index.ts"
export const createAuthAPI = <DefaultUser extends User = User>(ctx: GlobalContext) => {
return {
getSession: async (options: GetSessionAPIOptions): Promise<SessionResponse<DefaultUser>> => {
const session = await getSession<DefaultUser>({ ctx, headers: options.headers })
return session
},
signIn: async <Redirect extends boolean = true>(
oauth: LiteralUnion<BuiltInOAuthProvider>,
options?: SignInAPIOptions<Redirect>
): Promise<SignInReturn<Redirect>> => {
return signIn<Redirect>(oauth, {
ctx,
headers: options?.headers,
request: options?.request,
redirect: options?.redirect,
redirectTo: options?.redirectTo,
})
},
signInCredentials: async (options: SignInCredentialsAPIOptions) => {
return signInCredentials({
ctx,
payload: options.payload,
redirectTo: options.redirectTo,
})
},
signOut: async (options: SignOutAPIOptions) => {
const redirectTo = validateRedirectTo(options?.redirectTo ?? "/")
return signOut({ ctx, headers: options.headers, redirectTo, skipCSRFCheck: true })
},
updateSession: async (options: UpdateSessionAPIOptions<DefaultUser>) => {
return updateSession<DefaultUser>({
ctx,
headers: options.headers,
session: options.session,
skipCSRFCheck: options.skipCSRFCheck,
})
},
}
}