-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.tsx
More file actions
109 lines (97 loc) · 2.94 KB
/
index.tsx
File metadata and controls
109 lines (97 loc) · 2.94 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
'use client'
import type { AdminViewClientProps, FormState } from 'payload'
import { MinimalTemplate } from '@payloadcms/next/templates'
import {
Form,
FormSubmit,
HiddenField,
Link,
TextField,
useConfig,
useTranslation,
} from '@payloadcms/ui'
import { useRouter, useSearchParams } from 'next/navigation.js'
import React from 'react'
import { getLoginOptions } from '../../utilities/getLoginOptions.js'
import { localStorageKey } from '../shared.js'
const baseClass = 'login-otp'
export const LoginOTP: React.FC<AdminViewClientProps> = () => {
const redirect = useSearchParams().get('redirect')
const { config, getEntityConfig } = useConfig()
const { t } = useTranslation()
const router = useRouter()
const {
admin: { user: userSlug },
routes: { admin, api },
} = config
const collectionConfig = getEntityConfig({ collectionSlug: userSlug })
const { auth: authOptions } = collectionConfig
const loginWithUsername = authOptions?.loginWithUsername
const { canLoginWithEmail, canLoginWithUsername } = getLoginOptions(loginWithUsername ?? false)
const [loginType] = React.useState<'email' | 'emailOrUsername' | 'username'>(() => {
if (canLoginWithEmail && canLoginWithUsername) {
return 'emailOrUsername'
}
if (canLoginWithUsername) {
return 'username'
}
return 'email'
})
const [initialState] = React.useState<FormState>(() => {
const loginIdentifier =
typeof window !== 'undefined' ? window.localStorage.getItem(localStorageKey) : ''
return {
value: {
value: loginIdentifier,
},
}
})
const handleRedirect = React.useCallback(() => {
router.push(redirect ?? admin)
window.localStorage.removeItem(localStorageKey)
}, [router, admin, redirect])
return (
<MinimalTemplate>
<h3>Log in with one-time password</h3>
<br />
<Form
action={`${api}/${userSlug}/otp/login`}
className={baseClass}
initialState={initialState}
method="POST"
onSuccess={handleRedirect}
waitForAutocomplete
>
<HiddenField path="type" value={loginType} />
<TextField
field={{
name: 'value',
label: loginType === 'email' ? t('general:email') : t('general:username'),
required: true,
}}
path={'value'}
/>
<br />
<TextField
field={{
name: 'otp',
label: 'One-time password',
required: true,
}}
path="otp"
validate={(value) => {
if (!value) {
return 'You must enter a one-time password'
}
if (value.length !== 6) {
return 'Your one-time password should be 6 characters.'
}
return true
}}
/>
<FormSubmit size="large">Log in</FormSubmit>
</Form>
<Link href={`${admin}/login`}>Back to login</Link>
</MinimalTemplate>
)
}