fix: token async update flow

This commit is contained in:
Aidar Shaikhutdin @makeweb.space 2023-05-09 20:45:54 +06:00
parent 70aac95661
commit 9bfd15819e
3 changed files with 8 additions and 6 deletions

View File

@ -5,8 +5,8 @@ export interface AuthContextValue {
user: User | null user: User | null
token: AuthToken token: AuthToken
logout: () => void logout: () => void
signUp: (payload: SignUpPayload) => Promise<void> signUp: (payload: SignUpPayload) => Promise<AuthToken>
addBirthday: (birthday: string) => Promise<void> addBirthday: (birthday: string, token: AuthToken) => Promise<void>
} }
export interface SignUpPayload { export interface SignUpPayload {
@ -19,7 +19,7 @@ const initialContext: AuthContextValue = {
token: '', token: '',
user: null, user: null,
logout: () => undefined, logout: () => undefined,
signUp: () => Promise.resolve(), signUp: () => Promise.resolve(''),
addBirthday: () => Promise.resolve(), addBirthday: () => Promise.resolve(),
} }

View File

@ -1,13 +1,14 @@
import { useDispatch, useSelector } from 'react-redux' import { useDispatch, useSelector } from 'react-redux'
import { AuthContext, SignUpPayload } from './AuthContext' import { AuthContext, SignUpPayload } from './AuthContext'
import { RootState, actions } from '../store' import { RootState, actions } from '../store'
import { AuthToken } from '../types'
import routes from '../routes' import routes from '../routes'
export function AuthProvider({ children }: React.PropsWithChildren<unknown>): JSX.Element { export function AuthProvider({ children }: React.PropsWithChildren<unknown>): JSX.Element {
const dispatch = useDispatch() const dispatch = useDispatch()
const token = useSelector((state: RootState) => state.token) const token = useSelector((state: RootState) => state.token)
const user = useSelector((state: RootState) => state.user) const user = useSelector((state: RootState) => state.user)
const signUp = async ({ email, timezone, locale }: SignUpPayload): Promise<void> => { const signUp = async ({ email, timezone, locale }: SignUpPayload): Promise<AuthToken> => {
const response = await fetch(routes.server.token(), { const response = await fetch(routes.server.token(), {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
@ -17,6 +18,7 @@ export function AuthProvider({ children }: React.PropsWithChildren<unknown>): JS
const { auth: { token, user } } = await response.json() const { auth: { token, user } } = await response.json()
dispatch(actions.token.update(token)) dispatch(actions.token.update(token))
dispatch(actions.user.update(user)) dispatch(actions.user.update(user))
return token
} else { } else {
const { errors } = await response.json() const { errors } = await response.json()
if (Array.isArray(errors)) { if (Array.isArray(errors)) {
@ -28,7 +30,7 @@ export function AuthProvider({ children }: React.PropsWithChildren<unknown>): JS
} }
} }
} }
const addBirthday = async (birthday: string): Promise<void> => { const addBirthday = async (birthday: string, token: AuthToken): Promise<void> => {
const response = await fetch(routes.server.user(), { const response = await fetch(routes.server.user(), {
method: 'PATCH', method: 'PATCH',
headers: { headers: {

View File

@ -42,7 +42,7 @@ function EmailEnterPage(): JSX.Element {
timezone: getClientTimezone(), timezone: getClientTimezone(),
locale: getClientLocale(), locale: getClientLocale(),
}) })
.then(() => addBirthday(`${birthdate} ${birthtime}`)) .then((token) => addBirthday(`${birthdate} ${birthtime}`, token))
.then(() => navigate(routes.client.subscription())) .then(() => navigate(routes.client.subscription()))
.catch((error: Error) => setError(error)) .catch((error: Error) => setError(error))
.finally(() => setIsLoading(false)) .finally(() => setIsLoading(false))