Refactor cookie setting in _app
This commit is contained in:
parent
c49e930861
commit
0bc0251dad
1 changed files with 19 additions and 11 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { useEffect } from 'react'
|
||||
import { getCookie } from 'cookies-next'
|
||||
import { getCookie, getCookies } from 'cookies-next'
|
||||
import { appWithTranslation } from 'next-i18next'
|
||||
import { ThemeProvider } from 'next-themes'
|
||||
|
||||
|
|
@ -12,27 +12,35 @@ import setUserToken from '~utils/setUserToken'
|
|||
import '../styles/globals.scss'
|
||||
|
||||
function MyApp({ Component, pageProps }: AppProps) {
|
||||
const cookie = getCookie('account')
|
||||
const cookieData: AccountCookie = cookie ? JSON.parse(cookie as string) : null
|
||||
const accountCookie = getCookie('account')
|
||||
const userCookie = getCookie('user')
|
||||
|
||||
const cookieData = {
|
||||
account: accountCookie ? JSON.parse(accountCookie as string) : undefined,
|
||||
user: userCookie ? JSON.parse(userCookie as string) : undefined,
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
setUserToken()
|
||||
|
||||
if (cookie) {
|
||||
console.log(`Logged in as user "${cookieData.username}"`)
|
||||
if (accountCookie) {
|
||||
console.log(`Logged in as user "${cookieData.user}"`)
|
||||
console.log(cookieData.account, cookieData.user)
|
||||
|
||||
accountState.account.authorized = true
|
||||
accountState.account.user = {
|
||||
id: cookieData.userId,
|
||||
username: cookieData.username,
|
||||
picture: '',
|
||||
element: '',
|
||||
gender: 0,
|
||||
id: cookieData.account.userId,
|
||||
username: cookieData.account.username,
|
||||
picture: cookieData.user.picture,
|
||||
element: cookieData.user.element,
|
||||
gender: cookieData.user.gender,
|
||||
language: cookieData.user.language,
|
||||
theme: cookieData.user.theme,
|
||||
}
|
||||
} else {
|
||||
console.log(`You are not currently logged in.`)
|
||||
}
|
||||
}, [cookie, cookieData])
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<ThemeProvider>
|
||||
|
|
|
|||
Loading…
Reference in a new issue