-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathauth-client.ts
More file actions
118 lines (108 loc) · 3.27 KB
/
auth-client.ts
File metadata and controls
118 lines (108 loc) · 3.27 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
import { createAuthClient } from 'better-auth/react'
import {
type GoogleOneTapActionOptions,
adminClient,
deviceAuthorizationClient,
multiSessionClient,
oneTapClient,
usernameClient,
} from 'better-auth/client/plugins'
import { apiKeyClient } from '@better-auth/api-key/client'
import { agentAuthClient } from '@better-auth/agent-auth/client'
const authBaseUrl =
process.env.NEXT_PUBLIC_BETTER_AUTH_URL ?? 'http://localhost:3000'
const publicGoogleClientId = process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID?.trim()
export const hasGoogleOneTapClient = Boolean(publicGoogleClientId)
export const authClient = createAuthClient({
plugins: [
adminClient(),
apiKeyClient(),
deviceAuthorizationClient(),
multiSessionClient(),
oneTapClient({
clientId: publicGoogleClientId ?? 'missing-google-client-id',
autoSelect: true,
context: 'signin',
uxMode: 'redirect',
additionalOptions: {
// Any extra options for the Google initialize method
},
promptOptions: {
baseDelay: 1000,
maxAttempts: 5,
},
}),
usernameClient(),
agentAuthClient(),
],
baseURL: authBaseUrl,
credentials: 'include',
})
/**
* Normalizes a device authorization code so users can paste either spaced or dashed variants.
*
* @param value - Raw user-entered device code.
* @returns The uppercase, punctuation-free device code.
*/
export function normalizeDeviceUserCode(value: string): string {
return value.trim().replace(/-/g, '').toUpperCase()
}
/**
* Verifies that a device authorization code exists before advancing to the approval screen.
*
* @param input - The user code captured from the device verification screen.
* @returns The Better Auth device lookup response.
*/
export async function checkDeviceAuthorizationCode(input: { userCode: string }) {
return authClient.device({
query: {
user_code: normalizeDeviceUserCode(input.userCode),
},
})
}
/**
* Approves a pending device authorization request.
*
* @param input - The device user code to approve.
* @returns The Better Auth approval response.
*/
export async function approveDeviceAuthorization(input: { userCode: string }) {
return authClient.device.approve({
userCode: normalizeDeviceUserCode(input.userCode),
})
}
/**
* Denies a pending device authorization request.
*
* @param input - The device user code to reject.
* @returns The Better Auth denial response.
*/
export async function denyDeviceAuthorization(input: { userCode: string }) {
return authClient.device.deny({
userCode: normalizeDeviceUserCode(input.userCode),
})
}
export async function startGoogleOneTap(
options?: GoogleOneTapActionOptions
) {
if (!hasGoogleOneTapClient) {
return
}
await authClient.oneTap(options)
}
export async function signInWithUsername(input: {
username: string
password: string
callbackURL?: string
}) {
return authClient.signIn.username(input)
}
export async function signUpWithUsername(input: {
name: string
username: string
email: string
password: string
callbackURL?: string
}) {
return authClient.signUp.email(input)
}