-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathPassKeyAddDevice.tsx
More file actions
161 lines (147 loc) · 4.79 KB
/
PassKeyAddDevice.tsx
File metadata and controls
161 lines (147 loc) · 4.79 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
'use client'
import * as yup from 'yup'
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
import { Eye, EyeOff } from 'lucide-react'
import { Field, Form, Formik } from 'formik'
import { useEffect, useState } from 'react'
import { AlertComponent } from '@/components/AlertComponent'
import { AxiosResponse } from 'axios'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { addPasskeyUserDetails } from '@/app/api/Fido'
import { apiStatusCodes } from '@/config/CommonConstant'
import { passwordEncryption } from '@/app/api/Auth'
interface PasswordValue {
Password: string
}
interface PasskeyAddDeviceProps {
openModal: boolean
setOpenModel: (flag: boolean) => void
closeModal: (flag: boolean) => void
registerWithPasskey: (flag: boolean) => Promise<void>
email: string | null
}
export default function PasskeyAddDevice({
openModal,
email,
setOpenModel,
registerWithPasskey,
}: PasskeyAddDeviceProps): React.JSX.Element {
const [fidoUserError, setFidoUserError] = useState<string | null>(null)
const [nextStep, setNextStep] = useState(false)
const [passwordVisible, setPasswordVisible] = useState(false)
const [userEmail, setUserEmail] = useState('')
const savePassword = async (values: PasswordValue): Promise<void> => {
try {
if (!userEmail) {
setFidoUserError('User email is missing. Please refresh the page.')
return
}
const payload = {
password: passwordEncryption(values.Password),
}
const res = await addPasskeyUserDetails(payload, userEmail)
const { data } = res as AxiosResponse
if (data?.statusCode === apiStatusCodes.API_STATUS_SUCCESS) {
setNextStep(true)
} else if (res.toString().includes('401')) {
setFidoUserError(res as string)
} else {
setFidoUserError(res as string)
}
} catch (error) {
console.error('Unexpected error:', error)
setFidoUserError('An unexpected error occurred')
}
}
useEffect(() => {
if (email) {
setUserEmail(email)
}
}, [email])
return (
<Dialog open={openModal} onOpenChange={setOpenModel}>
<DialogContent>
<DialogHeader>
<DialogTitle>Create Passkey</DialogTitle>
</DialogHeader>
{fidoUserError && (
<div className="w-full" role="alert">
<AlertComponent
message={fidoUserError}
type="failure"
onAlertClose={() => {
setFidoUserError(null)
}}
/>
</div>
)}
{!nextStep ? (
<Formik
initialValues={{ Password: '' }}
validationSchema={yup.object().shape({
Password: yup.string().required('Password is required'),
})}
onSubmit={savePassword}
>
{({ handleSubmit, errors, touched }) => (
<Form onSubmit={handleSubmit} className="mt-4 space-y-4">
<div>
<label
htmlFor="Password"
className="block text-sm font-medium"
>
Password <span className="text-red-500">*</span>
</label>
<div className="relative mt-1">
<Field
as={Input}
id="Password"
name="Password"
type={passwordVisible ? 'text' : 'password'}
/>
<button
type="button"
onClick={() => setPasswordVisible((prev) => !prev)}
className="absolute top-1/2 right-2 -translate-y-1/2"
>
{passwordVisible ? (
<EyeOff size={18} />
) : (
<Eye size={18} />
)}
</button>
</div>
{errors.Password && touched.Password && (
<p className="mt-1 text-xs text-red-500">
{errors.Password}
</p>
)}
</div>
<div className="flex justify-end">
<Button type="submit">Next</Button>
</div>
</Form>
)}
</Formik>
) : (
<div className="mt-4 flex flex-col items-center gap-4">
<img
src="/images/passkeyAddDevice.svg"
alt="Passkey Device"
className="h-[300px] w-[300px]"
/>
<Button onClick={() => registerWithPasskey(true)}>
Create Passkey
</Button>
</div>
)}
</DialogContent>
</Dialog>
)
}