-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(experience): migrate the password register and sign-in
migrate the password register and sign-in flow
- Loading branch information
Showing
10 changed files
with
190 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
type IdentificationApiPayload, | ||
InteractionEvent, | ||
type PasswordVerificationPayload, | ||
SignInIdentifier, | ||
type UpdateProfileApiPayload, | ||
} from '@logto/schemas'; | ||
|
||
import api from './api'; | ||
|
||
const prefix = '/api/experience'; | ||
|
||
const experienceRoutes = Object.freeze({ | ||
prefix, | ||
identification: `${prefix}/identification`, | ||
verification: `${prefix}/verification`, | ||
profile: `${prefix}/profile`, | ||
mfa: `${prefix}/profile/mfa`, | ||
}); | ||
|
||
type VerificationResponse = { | ||
verificationId: string; | ||
}; | ||
|
||
type SubmitInteractionResponse = { | ||
redirectTo: string; | ||
}; | ||
|
||
const initInteraction = async (interactionEvent: InteractionEvent) => | ||
api.put(`${experienceRoutes.prefix}`, { | ||
json: { | ||
interactionEvent, | ||
}, | ||
}); | ||
|
||
const identifyUser = async (payload: IdentificationApiPayload = {}) => | ||
api.post(experienceRoutes.identification, { json: payload }); | ||
|
||
const submitInteraction = async () => | ||
api.post(`${experienceRoutes.prefix}/submit`).json<SubmitInteractionResponse>(); | ||
|
||
const updateProfile = async (payload: UpdateProfileApiPayload) => { | ||
await api.post(experienceRoutes.profile, { json: payload }); | ||
}; | ||
|
||
export const signInWithPasswordIdentifier = async (payload: PasswordVerificationPayload) => { | ||
await initInteraction(InteractionEvent.SignIn); | ||
|
||
const { verificationId } = await api | ||
.post(`${experienceRoutes.verification}/password`, { | ||
json: payload, | ||
}) | ||
.json<VerificationResponse>(); | ||
|
||
await identifyUser({ verificationId }); | ||
|
||
return submitInteraction(); | ||
}; | ||
|
||
export const registerWithUsername = async (username: string) => { | ||
await initInteraction(InteractionEvent.Register); | ||
|
||
return updateProfile({ type: SignInIdentifier.Username, value: username }); | ||
}; | ||
|
||
export const continueRegisterWithPassword = async (password: string) => { | ||
await updateProfile({ type: 'password', value: password }); | ||
|
||
await identifyUser(); | ||
|
||
return submitInteraction(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/experience/src/hooks/use-password-policy-checker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useCallback } from 'react'; | ||
|
||
import usePasswordErrorMessage from './use-password-error-message'; | ||
import { usePasswordPolicy } from './use-sie'; | ||
|
||
type PasswordPolicyCheckProps = { | ||
setErrorMessage: (message?: string) => void; | ||
}; | ||
|
||
const usePasswordPolicyChecker = ({ setErrorMessage }: PasswordPolicyCheckProps) => { | ||
const { getErrorMessage } = usePasswordErrorMessage(); | ||
const { policyChecker } = usePasswordPolicy(); | ||
|
||
const checkPassword = useCallback( | ||
async (password: string) => { | ||
// Perform fast check before sending request | ||
const fastCheckErrorMessage = getErrorMessage(policyChecker.fastCheck(password)); | ||
|
||
if (fastCheckErrorMessage) { | ||
setErrorMessage(fastCheckErrorMessage); | ||
return false; | ||
} | ||
|
||
return true; | ||
}, | ||
[getErrorMessage, policyChecker, setErrorMessage] | ||
); | ||
|
||
return checkPassword; | ||
}; | ||
|
||
export default usePasswordPolicyChecker; |
31 changes: 31 additions & 0 deletions
31
packages/experience/src/hooks/use-password-rejection-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { type RequestErrorBody } from '@logto/schemas'; | ||
import { useCallback, useMemo } from 'react'; | ||
|
||
import type { ErrorHandlers } from './use-error-handler'; | ||
import usePasswordErrorMessage from './use-password-error-message'; | ||
|
||
type ErrorHandlerProps = { | ||
setErrorMessage: (message?: string) => void; | ||
}; | ||
|
||
const usePasswordRejectionErrorHandler = ({ setErrorMessage }: ErrorHandlerProps) => { | ||
const { getErrorMessageFromBody } = usePasswordErrorMessage(); | ||
|
||
const passwordRejectionHandler = useCallback( | ||
(error: RequestErrorBody) => { | ||
setErrorMessage(getErrorMessageFromBody(error)); | ||
}, | ||
[getErrorMessageFromBody, setErrorMessage] | ||
); | ||
|
||
const passwordRejectionErrorHandler = useMemo<ErrorHandlers>( | ||
() => ({ | ||
'password.rejected': passwordRejectionHandler, | ||
}), | ||
[passwordRejectionHandler] | ||
); | ||
|
||
return passwordRejectionErrorHandler; | ||
}; | ||
|
||
export default usePasswordRejectionErrorHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters