From 9bfd15819e1def6d222142b5e548f83bae07d7d8 Mon Sep 17 00:00:00 2001 From: "Aidar Shaikhutdin @makeweb.space" Date: Tue, 9 May 2023 20:45:54 +0600 Subject: [PATCH] fix: token async update flow --- src/auth/AuthContext.ts | 6 +++--- src/auth/AuthProvider.tsx | 6 ++++-- src/components/EmailEnterPage/index.tsx | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/auth/AuthContext.ts b/src/auth/AuthContext.ts index 147741f..c47592c 100644 --- a/src/auth/AuthContext.ts +++ b/src/auth/AuthContext.ts @@ -5,8 +5,8 @@ export interface AuthContextValue { user: User | null token: AuthToken logout: () => void - signUp: (payload: SignUpPayload) => Promise - addBirthday: (birthday: string) => Promise + signUp: (payload: SignUpPayload) => Promise + addBirthday: (birthday: string, token: AuthToken) => Promise } export interface SignUpPayload { @@ -19,7 +19,7 @@ const initialContext: AuthContextValue = { token: '', user: null, logout: () => undefined, - signUp: () => Promise.resolve(), + signUp: () => Promise.resolve(''), addBirthday: () => Promise.resolve(), } diff --git a/src/auth/AuthProvider.tsx b/src/auth/AuthProvider.tsx index 3d8b6ea..ce9bd58 100644 --- a/src/auth/AuthProvider.tsx +++ b/src/auth/AuthProvider.tsx @@ -1,13 +1,14 @@ import { useDispatch, useSelector } from 'react-redux' import { AuthContext, SignUpPayload } from './AuthContext' import { RootState, actions } from '../store' +import { AuthToken } from '../types' import routes from '../routes' export function AuthProvider({ children }: React.PropsWithChildren): JSX.Element { const dispatch = useDispatch() const token = useSelector((state: RootState) => state.token) const user = useSelector((state: RootState) => state.user) - const signUp = async ({ email, timezone, locale }: SignUpPayload): Promise => { + const signUp = async ({ email, timezone, locale }: SignUpPayload): Promise => { const response = await fetch(routes.server.token(), { method: 'POST', headers: { 'Content-Type': 'application/json' }, @@ -17,6 +18,7 @@ export function AuthProvider({ children }: React.PropsWithChildren): JS const { auth: { token, user } } = await response.json() dispatch(actions.token.update(token)) dispatch(actions.user.update(user)) + return token } else { const { errors } = await response.json() if (Array.isArray(errors)) { @@ -28,7 +30,7 @@ export function AuthProvider({ children }: React.PropsWithChildren): JS } } } - const addBirthday = async (birthday: string): Promise => { + const addBirthday = async (birthday: string, token: AuthToken): Promise => { const response = await fetch(routes.server.user(), { method: 'PATCH', headers: { diff --git a/src/components/EmailEnterPage/index.tsx b/src/components/EmailEnterPage/index.tsx index 0e6f717..7b3a25d 100644 --- a/src/components/EmailEnterPage/index.tsx +++ b/src/components/EmailEnterPage/index.tsx @@ -42,7 +42,7 @@ function EmailEnterPage(): JSX.Element { timezone: getClientTimezone(), locale: getClientLocale(), }) - .then(() => addBirthday(`${birthdate} ${birthtime}`)) + .then((token) => addBirthday(`${birthdate} ${birthtime}`, token)) .then(() => navigate(routes.client.subscription())) .catch((error: Error) => setError(error)) .finally(() => setIsLoading(false))