Skip to content

Commit

Permalink
refactor: create more sustainable solution for understanding a user's…
Browse files Browse the repository at this point in the history
… login status (null, true, false)
  • Loading branch information
Danielle254 committed Nov 7, 2024
1 parent 4e72c42 commit 62d396b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
27 changes: 10 additions & 17 deletions app/(main)/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,15 @@ type LoginUserSchemaType = z.infer<typeof LoginUserSchema>;
*/
const Login = (): React.JSX.Element => {
const router = useRouter();
const { login, getUser } = useAuthContext();
const { login, isSignedIn } = useAuthContext();
const [isLoading, setIsLoading] = useState<boolean>(false);
const [checkAuth, setCheckAuth] = useState<boolean>(true);

useEffect(() => {
async function checkSignedIn() {
const userSignedIn = await getUser();
if (userSignedIn) {
router.push('/league/all');
}
else {
setCheckAuth(false);
}
if (isSignedIn === true) {
router.push('league/all');
}

checkSignedIn();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [isSignedIn]);

const form = useForm<LoginUserSchemaType>({
resolver: zodResolver(LoginUserSchema),
Expand Down Expand Up @@ -106,9 +97,11 @@ const Login = (): React.JSX.Element => {
};

return (
<section className={`grid xl:${checkAuth ? '' : 'grid-cols-2'} xl:grid-rows-none`}>
{checkAuth ? (
<GlobalSpinner data-testid="global-spinner" />) : (
<section className={`grid xl:${isSignedIn === null ? '' : 'grid-cols-2'} xl:grid-rows-none`}>
{isSignedIn === null || isSignedIn === true &&
<GlobalSpinner data-testid="global-spinner" />
}
{isSignedIn === false &&
<>
<div className="row-span-1 grid w-full place-items-center from-[#4E160E] to-zinc-950 bg-gradient-to-b xl:h-screen xl:bg-gradient-to-b">
<Logo className="mx-auto w-52 xl:w-64 xl:place-self-end" src={logo} />
Expand Down Expand Up @@ -202,7 +195,7 @@ const Login = (): React.JSX.Element => {
</Form>
</div>
</>
)}
}
</section>
);
};
Expand Down
5 changes: 3 additions & 2 deletions context/AuthContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type AuthContextType = {
getUser: () => Promise<IUser | undefined>;
login: (user: UserCredentials) => Promise<void | Error>; // eslint-disable-line no-unused-vars
logoutAccount: () => Promise<void | Error>;
isSignedIn: boolean;
isSignedIn: boolean | null;
};

export const AuthContext = createContext<AuthContextType | null>(null);
Expand All @@ -39,7 +39,7 @@ export const AuthContextProvider = ({
}: {
children: React.ReactNode;
}): JSX.Element => {
const [isSignedIn, setIsSignedIn] = useState<boolean>(false);
const [isSignedIn, setIsSignedIn] = useState<boolean | null>(null);
const { updateUser, resetUser, user } = useDataStore<DataStore>(
(state) => state,
);
Expand Down Expand Up @@ -86,6 +86,7 @@ export const AuthContextProvider = ({
*/
const getUser = async (): Promise<IUser | undefined> => {
if (!isSessionInLocalStorage()) {
setIsSignedIn(false);
if (isAuthRequiredPath(pathname)) {
router.push('/login');
}
Expand Down
2 changes: 1 addition & 1 deletion context/AuthHelper.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.share
export interface ILogoutType {
resetUser: React.Dispatch<React.SetStateAction<void>>;
router: AppRouterInstance;
setIsSignedIn: React.Dispatch<React.SetStateAction<boolean>>;
setIsSignedIn: React.Dispatch<React.SetStateAction<boolean | null>>;
}

0 comments on commit 62d396b

Please sign in to comment.