import axios, { endpoints } from 'src/lib/axios'; import { JWT_STORAGE_KEY } from './constant'; import { setSession } from './utils'; // ---------------------------------------------------------------------- export type SignInParams = { email: string; password: string; }; export type SignUpParams = { email: string; password: string; firstName: string; lastName: string; }; const ERR_ACCESS_TOKEN_NOT_FOUND = `Access token not found in response`; /** ************************************** * Sign in *************************************** */ export const signInWithPassword = async ({ email, password }: SignInParams): Promise => { try { const params = { email, password }; const res = await axios.post(endpoints.auth.signIn, params); const { accessToken } = res.data; if (!accessToken) { throw new Error(ERR_ACCESS_TOKEN_NOT_FOUND); } setSession(accessToken); } catch (error) { console.error('Error during sign in:', error); throw error; } }; /** ************************************** * Sign up *************************************** */ export const signUp = async ({ email, password, firstName, lastName, }: SignUpParams): Promise => { const params = { email, password, firstName, lastName, }; try { const res = await axios.post(endpoints.auth.signUp, params); const { accessToken } = res.data; if (!accessToken) { throw new Error('Access token not found in response'); } sessionStorage.setItem(JWT_STORAGE_KEY, accessToken); } catch (error) { console.error('Error during sign up:', error); throw error; } }; /** ************************************** * Sign out *************************************** */ export const signOut = async (): Promise => { try { await setSession(null); } catch (error) { console.error('Error during sign out:', error); throw error; } };