Skip to content

Commit

Permalink
Merge pull request #50 from DevKor-github/api/signUp
Browse files Browse the repository at this point in the history
feat: AuthProvider
  • Loading branch information
halionaz authored Aug 21, 2024
2 parents 1f4af86 + 91c3ded commit cc50828
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
2 changes: 2 additions & 0 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import StyledComponentsRegistry from '@/libs/design-system/styled-components/reg
import ThemeClient from '@/libs/design-system/styled-components/ThemeClient';
import { OverlayProvider } from '@/libs/design-system/overlay';
import QueryProvider from '@/libs/queries/QueryProvider';
import { AuthProvider } from '@/libs/clients/AuthProvider';

export const metadata: Metadata = {
title: '신나는 정기전 승부예측, TOKY',
Expand All @@ -20,6 +21,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
<ThemeClient>
<OverlayProvider>
<QueryProvider>
<AuthProvider />
<main>{children}</main>
</QueryProvider>
</OverlayProvider>
Expand Down
44 changes: 44 additions & 0 deletions libs/clients/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use client';

import { refresh } from '@/libs/clients/refresh';
import { useAuthStore } from '@/libs/store/useAuthStore';
import { useCallback, useEffect } from 'react';

export function AuthProvider() {
const { accessToken, refreshToken, clearTokens, setTokens } = useAuthStore();

const setTokenFromCookie = useCallback(() => {
let accessToken = null,
refreshToken = null;

document.cookie.split(';').forEach((c) => {
if (c.trim().startsWith('access-token=')) accessToken = c.split('=')[1];
if (c.trim().startsWith('refresh-token=')) refreshToken = c.split('=')[1];
});
if (!accessToken || !refreshToken) {
clearTokens();
return false;
}

document.cookie = 'access-token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
document.cookie = 'refresh-token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';

setTokens(accessToken, refreshToken);

return true;
}, [clearTokens, setTokens]);

const checkAuth = useCallback(async () => {
if (refreshToken) {
await refresh(process.env.NEXT_PUBLIC_API_URL ?? '');
} else {
setTokenFromCookie();
}
}, [refreshToken, setTokenFromCookie]);

useEffect(() => {
if (accessToken === null) checkAuth();
}, [accessToken, checkAuth]);

return <></>;
}
21 changes: 0 additions & 21 deletions libs/clients/setTokenFromCookie.ts

This file was deleted.

21 changes: 21 additions & 0 deletions libs/store/useAuthStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { create } from 'zustand';
import { persist } from 'zustand/middleware';

interface AuthStore {
accessToken: string | null;
refreshToken: string | null;
setTokens: (accessToken: string, refreshToken: string) => void;
clearTokens: () => void;
}

export const useAuthStore = create<AuthStore>()(
persist(
(set) => ({
accessToken: null,
refreshToken: null,
setTokens: (accessToken, refreshToken) => set({ accessToken, refreshToken }),
clearTokens: () => set({ accessToken: null, refreshToken: null }),
}),
{ name: 'user auth' },
),
);

0 comments on commit cc50828

Please sign in to comment.