authorizer-upgrade-3

This commit is contained in:
Untone 2024-01-31 16:24:40 +03:00
parent cf19cdd39b
commit 5a95e7490e
2 changed files with 29 additions and 13 deletions

View File

@ -12,7 +12,6 @@ import { validateEmail } from '../../../utils/validateEmail'
import { email, setEmail } from './sharedLogic'
import styles from './AuthModal.module.scss'
import { ApiResponse, ForgotPasswordResponse } from '@authorizerdev/authorizer-js'
type FormFields = {
email: string
@ -28,7 +27,7 @@ export const ForgotPasswordForm = () => {
setEmail(newEmail.toLowerCase())
}
const {
actions: { authorizer },
actions: { forgotPassword },
} = useSession()
const [submitError, setSubmitError] = createSignal('')
const [isSubmitting, setIsSubmitting] = createSignal(false)
@ -62,22 +61,28 @@ export const ForgotPasswordForm = () => {
setIsSubmitting(true)
try {
const response: ApiResponse<ForgotPasswordResponse> = await authorizer().forgotPassword({
const { data, errors } = await forgotPassword({
email: email(),
redirect_uri: window.location.origin,
})
console.debug('[ForgotPasswordForm] authorizer response:', response)
if (response?.data) setMessage(response.data.message)
else {
console.warn(response.errors)
if (data) {
console.debug('[ForgotPasswordForm] authorizer response:', data)
setMessage(data.message)
}
if (errors) {
console.warn(errors)
if (errors) {
const error: Error = errors[0]
if (error.cause === 'user_not_found') {
setIsUserNotFound(true)
return
} else {
setSubmitError(error.message)
}
}
}
} catch (error) {
console.error(error)
if (error?.code === 'user_not_found') {
setIsUserNotFound(true)
return
}
setSubmitError(error?.message)
} finally {
setIsSubmitting(false)
}

View File

@ -12,7 +12,8 @@ import {
AuthorizeResponse,
ApiResponse,
GenericResponse,
// GraphqlQueryInput,
ForgotPasswordResponse,
ForgotPasswordInput,
} from '@authorizerdev/authorizer-js'
import {
createContext,
@ -62,6 +63,9 @@ export type SessionContextType = {
signIn: (params: LoginInput) => Promise<void>
signOut: () => Promise<void>
oauth: (provider: string) => Promise<void>
forgotPassword: (
params: ForgotPasswordInput,
) => Promise<{ data: ForgotPasswordResponse; errors: Error[] }>
changePassword: (password: string, token: string) => void
confirmEmail: (input: VerifyEmailInput) => Promise<AuthToken | void> // email confirm callback is in auth.discours.io
setIsSessionLoaded: (loaded: boolean) => void
@ -293,6 +297,12 @@ export const SessionProvider = (props: {
console.debug('[context.session] change password response:', resp)
}
const forgotPassword = async (params: ForgotPasswordInput) => {
const resp = await authorizer().forgotPassword(params)
console.debug('[context.session] change password response:', resp)
return { data: resp?.data, errors: resp.errors }
}
const confirmEmail = async (input: VerifyEmailInput) => {
console.debug(`[context.session] calling authorizer's verify email with`, input)
try {
@ -338,6 +348,7 @@ export const SessionProvider = (props: {
setAuthor,
authorizer,
loadAuthor,
forgotPassword,
changePassword,
oauth,
}