Skip to content

Commit 5d8b5de

Browse files
fix: sign up with passkey (#1077)
* fix: sign up with passkey Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * resolve: comments on PR Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> --------- Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com>
1 parent c197495 commit 5d8b5de

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

nextjs/src/features/components/UserInfoForm.tsx

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { AlertComponent } from '@/components/AlertComponent'
2525
import { Button } from '@/components/ui/button'
2626
import { Input } from '@/components/ui/input'
2727
import { startRegistration } from '@simplewebauthn/browser'
28+
import { v4 as uuidv4 } from 'uuid'
2829

2930
interface StepUserInfoProps {
3031
email: string
@@ -39,6 +40,13 @@ export interface RegistrationOptionInterface {
3940
export enum Devices {
4041
Linux = 'linux',
4142
}
43+
export interface AddPasswordDetails {
44+
email: string
45+
password: string
46+
isPasskey: boolean
47+
firstName: string | null
48+
lastName: string | null
49+
}
4250

4351
const validationSchema = Yup.object().shape({
4452
firstName: Yup.string()
@@ -78,14 +86,15 @@ export default function UserInfoForm({
7886
const [, setDisableFlag] = useState<boolean>(false)
7987
const [, setAddFailure] = useState<string | null>(null)
8088
const [, setAddSuccess] = useState<string | null>(null)
81-
const [, setErrMsg] = useState<string | null>(null)
8289
const router = useRouter()
8390
const [, setFidoLoader] = useState<boolean>(false)
8491
const [, setFidoError] = useState('')
8592
const [showPassword, setShowPassword] = useState(false)
8693
const [showConfirmPassword, setShowConfirmPassword] = useState(false)
8794
const [success, setSuccess] = useState<string | null>(null)
8895
const [failure, setFailure] = useState<string | null>(null)
96+
const [passkeyFirstName, setPasskeyFirstName] = useState('')
97+
const [passkeyLastName, setPasskeyLastName] = useState('')
8998

9099
const searchParams = useSearchParams()
91100
const redirectTo = searchParams.get('redirectTo')
@@ -134,6 +143,37 @@ export default function UserInfoForm({
134143
}
135144
}
136145

146+
const passkeySubmit = async (values: {
147+
firstName: string
148+
lastName: string
149+
}): Promise<void> => {
150+
const password: string = uuidv4()
151+
const payload: AddPasswordDetails = {
152+
email,
153+
isPasskey: true,
154+
firstName: values.firstName,
155+
lastName: values.lastName,
156+
password: passwordEncryption(password),
157+
}
158+
159+
setLoading(true)
160+
try {
161+
const userRsp = await addPasswordDetails(payload)
162+
const { data } = userRsp as AxiosResponse
163+
164+
if (data?.statusCode === apiStatusCodes.API_STATUS_CREATED) {
165+
window.location.href =
166+
'/sign-in?signup=true&fidoFlag=true&method=passkey'
167+
} else {
168+
setFailure(data?.message || 'Passkey registration failed')
169+
}
170+
} catch (error) {
171+
setFailure('Error during passkey registration')
172+
} finally {
173+
setLoading(false)
174+
}
175+
}
176+
137177
const showFidoError = (error: unknown): void => {
138178
const err = error as AxiosError
139179
if (
@@ -153,7 +193,10 @@ export default function UserInfoForm({
153193
const deviceDetailsResp = await addDeviceDetails(deviceBody)
154194
const { data } = deviceDetailsResp as AxiosResponse
155195
if (data?.statusCode === apiStatusCodes.API_STATUS_SUCCESS) {
156-
router.push('/sign-in')
196+
passkeySubmit({
197+
firstName: passkeyFirstName,
198+
lastName: passkeyLastName,
199+
})
157200
}
158201
setTimeout(() => {
159202
setAddSuccess('')
@@ -230,7 +273,7 @@ export default function UserInfoForm({
230273

231274
await verifyRegistrationMethod(verifyRegistrationObj, email)
232275
} else {
233-
setErrMsg(
276+
setFailure(
234277
(generateRegistrationResponse as AxiosResponse)?.data?.message ||
235278
'An error occurred',
236279
)
@@ -317,7 +360,10 @@ export default function UserInfoForm({
317360
placeholder="First name"
318361
name="firstName"
319362
value={values.firstName}
320-
onChange={handleChange}
363+
onChange={(e) => {
364+
handleChange(e)
365+
setPasskeyFirstName(e.target.value)
366+
}}
321367
onBlur={handleBlur}
322368
className="bg-[var(--color-bg-primary)] text-[var(--color-text-primary)]"
323369
/>
@@ -332,7 +378,10 @@ export default function UserInfoForm({
332378
placeholder="Last name"
333379
name="lastName"
334380
value={values.lastName}
335-
onChange={handleChange}
381+
onChange={(e) => {
382+
handleChange(e)
383+
setPasskeyLastName(e.target.value)
384+
}}
336385
onBlur={handleBlur}
337386
className="bg-[var(--color-bg-primary)] text-[var(--color-text-primary)]"
338387
/>

0 commit comments

Comments
 (0)