-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { LoaderFunction } from '@remix-run/node'; | ||
import { loginKakao } from 'src/types'; | ||
import { useLoaderData } from '@remix-run/react'; | ||
import { useEffect } from 'react'; | ||
import { useAuthStore } from 'src/entities/auth/useAuthStore'; | ||
|
||
export const loader: LoaderFunction = async ({ request }) => { | ||
const searchParams = new URL(request.url).searchParams; | ||
const code = searchParams.get('code'); | ||
|
||
if (!code) { | ||
throw new Response('', { | ||
status: 404, | ||
statusText: '', | ||
}); | ||
} | ||
|
||
return { code }; | ||
}; | ||
|
||
export default function KakaoAuthPage() { | ||
const { code } = useLoaderData<typeof loader>(); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
try { | ||
const response = await loginKakao({ | ||
code, | ||
}); | ||
const token = response.accessToken; | ||
useAuthStore.getState().login(token); | ||
} catch (e) { | ||
location.replace('/login'); | ||
} | ||
})(); | ||
}, [code]); | ||
|
||
return <></>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { create } from 'zustand'; | ||
import { persist } from 'zustand/middleware'; | ||
|
||
type AuthStore = { | ||
isLoggedIn: boolean; | ||
login: (token: string) => void; | ||
logout: () => void; | ||
}; | ||
|
||
const ACCESS_TOKEN_STORAGE_KEY = 'accessToken'; | ||
|
||
export const useAuthStore = create( | ||
persist<AuthStore>( | ||
(set) => ({ | ||
isLoggedIn: false, | ||
login: (token: string) => { | ||
localStorage.setItem(ACCESS_TOKEN_STORAGE_KEY, token); | ||
if (token) { | ||
set({ isLoggedIn: true }); | ||
} | ||
}, | ||
logout: () => { | ||
set({ isLoggedIn: false }); | ||
localStorage.removeItem(ACCESS_TOKEN_STORAGE_KEY); | ||
}, | ||
}), | ||
{ | ||
name: 'authStatus', | ||
}, | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters