From 409a89f798e3f231c1446852c686380589bb6336 Mon Sep 17 00:00:00 2001 From: Clue355 Date: Tue, 9 Jul 2024 21:45:36 -0500 Subject: [PATCH] update and adjust code --- context/AuthContextProvider.test.tsx | 49 +++++++++++++++--- context/AuthContextProvider.tsx | 17 +++++-- context/AuthHelper.tsx | 74 +++++++++------------------- 3 files changed, 75 insertions(+), 65 deletions(-) diff --git a/context/AuthContextProvider.test.tsx b/context/AuthContextProvider.test.tsx index 8000aaee..20d4ecd8 100644 --- a/context/AuthContextProvider.test.tsx +++ b/context/AuthContextProvider.test.tsx @@ -3,7 +3,7 @@ import { account } from '../api/config'; import Alert from '../components/AlertNotification/AlertNotification'; import { AlertVariants } from '../components/AlertNotification/Alerts.enum'; import { toast } from 'react-hot-toast'; -import { loginAccount } from './AuthHelper'; +import { loginAccount, logoutAccount } from './AuthHelper'; import { NextRouter } from 'next/router'; const mockCreateEmailPasswordSession = jest.fn(); @@ -32,21 +32,29 @@ describe('AuthContextProvider', () => { password: 'password1234', }); + const setIsSignedIn = jest.fn(); + const resetUser = jest.fn(); + beforeEach(() => { jest.clearAllMocks(); }); test('should show success notification after a successful login', async () => { - mockCreateEmailPasswordSession.mockResolvedValue({}); - await loginAccount({user, router, getUser}); + await loginAccount({ user, router, getUser }); - expect(mockCreateEmailPasswordSession).toHaveBeenCalledWith(user.email, user.password); + expect(mockCreateEmailPasswordSession).toHaveBeenCalledWith( + user.email, + user.password, + ); expect(getUser).toHaveBeenCalled(); expect(router.push).toHaveBeenCalledWith('/league/all'); expect(toast.custom).toHaveBeenCalledWith( - , + , ); }); @@ -55,11 +63,36 @@ describe('AuthContextProvider', () => { mockCreateEmailPasswordSession.mockRejectedValue(mockError); - const error = await loginAccount({user, router, getUser}); + const error = await loginAccount({ user, router, getUser }); + + expect(error).toEqual(mockError); + expect(toast.custom).toHaveBeenCalledWith( + , + ); + }); + + //logout tests + test('after a successful logout it shows success notification', async () => { + await logoutAccount({ resetUser, setIsSignedIn, router }); + + expect(setIsSignedIn).toHaveBeenCalledWith(false); + expect(resetUser).toHaveBeenCalled(); + expect(router.push).toHaveBeenCalledWith('/login'); + expect(toast.custom).toHaveBeenCalledWith( + , + ); + }); + + test('after logout attempt errors it shows error notification', async () => { + const mockError = new Error('Logout error'); + + account.deleteSession = jest.fn().mockRejectedValue(mockError); + + const error = await logoutAccount({ resetUser, setIsSignedIn, router }); expect(error).toEqual(mockError); expect(toast.custom).toHaveBeenCalledWith( - , + , ); }); -}); \ No newline at end of file +}); diff --git a/context/AuthContextProvider.tsx b/context/AuthContextProvider.tsx index 5120c1a8..dddddbbe 100644 --- a/context/AuthContextProvider.tsx +++ b/context/AuthContextProvider.tsx @@ -23,7 +23,7 @@ type AuthContextType = { isSignedIn: boolean; setIsSignedIn: React.Dispatch>; loginAccount: (user: UserCredentials) => Promise; // eslint-disable-line no-unused-vars - logoutAccount: () => Promise; + logoutAccount: () => Promise; getUser: () => Promise; }; @@ -62,8 +62,8 @@ export const AuthContextProvider = ({ const loginAccount = async (user: UserCredentials): Promise => { try { await account.createEmailPasswordSession(user.email, user.password); - await getUser(); // Fetch user data and update state - router.push('/league/all'); + getUser(); + router.push('/weeklyPicks'); toast.custom( } + * @returns {Promise} */ - const logoutAccount = async (): Promise => { + const logoutAccount = async (): Promise => { try { await account.deleteSession('current'); setIsSignedIn(false); resetUser(); // Reset user data in the store + toast.custom( + , + ); router.push('/login'); } catch (error) { + toast.custom( + , + ); console.error('Logout error:', error); + return error as Error; } }; diff --git a/context/AuthHelper.tsx b/context/AuthHelper.tsx index 737c1091..ea08eb0f 100644 --- a/context/AuthHelper.tsx +++ b/context/AuthHelper.tsx @@ -15,21 +15,6 @@ type UserCredentials = { password: string; }; -/** - * Authenticate and set session state - * @param user.user - * @param user.email - * @param user.password - * @param user - the user credentials - * @param router - router function - * @param getUser - getUser() function - * @param user.router - * @param user.getUser - * @param getUser.user - * @param getUser.router - * @param getUser.getUser - * @returns The error if there is one - */ export const loginAccount = async ({ user, router, @@ -59,46 +44,31 @@ export const loginAccount = async ({ }; /** - * Get user data from the session - * @param router - router to navigate the page - * @param updateUser - updateUser() function - * @param resetUser - resetUser() function - * @param setIsSignedIn - changes to false if errors + * Log out and clear session state * @returns {Promise} */ -export const getUser = async ( - router: NextRouter, - updateUser: (id: string, email: string, leagues: any) => void, - resetUser: () => void, - setIsSignedIn: (value: boolean) => void, -) => { - if (!isSessionInLocalStorage()) { - router.push('/login'); - return; - } - +export const logoutAccount = async ({ + resetUser, + setIsSignedIn, + router, +}: { + resetUser: () => void; + setIsSignedIn: (bool: false) => void; + router: NextRouter; +}): Promise => { try { - const user = await account.get(); - const userData: IUser = await getCurrentUser(user.$id); - updateUser(userData.id, userData.email, userData.leagues); - return userData; - } catch (error) { - resetUser(); + await account.deleteSession('current'); setIsSignedIn(false); + resetUser(); // Reset user data in the store + toast.custom( + , + ); + router.push('/login'); + } catch (error) { + toast.custom( + , + ); + console.error('Logout error:', error); + return error as Error; } }; - -/** - * Helper function to validate session data in local storage - * @returns {boolean} - Whether the session is in local storage. - */ -const isSessionInLocalStorage = (): boolean => { - const loadedDataString = localStorage.getItem('cookieFallback'); - - if (!loadedDataString || loadedDataString === '[]') { - localStorage.removeItem('cookieFallback'); - return false; - } - - return true; -};