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 { useEffect } from 'react'
|
||||||
import { getCookie } from 'cookies-next'
|
import { getCookie, getCookies } from 'cookies-next'
|
||||||
import { appWithTranslation } from 'next-i18next'
|
import { appWithTranslation } from 'next-i18next'
|
||||||
import { ThemeProvider } from 'next-themes'
|
import { ThemeProvider } from 'next-themes'
|
||||||
|
|
||||||
|
|
@ -12,27 +12,35 @@ import setUserToken from '~utils/setUserToken'
|
||||||
import '../styles/globals.scss'
|
import '../styles/globals.scss'
|
||||||
|
|
||||||
function MyApp({ Component, pageProps }: AppProps) {
|
function MyApp({ Component, pageProps }: AppProps) {
|
||||||
const cookie = getCookie('account')
|
const accountCookie = getCookie('account')
|
||||||
const cookieData: AccountCookie = cookie ? JSON.parse(cookie as string) : null
|
const userCookie = getCookie('user')
|
||||||
|
|
||||||
|
const cookieData = {
|
||||||
|
account: accountCookie ? JSON.parse(accountCookie as string) : undefined,
|
||||||
|
user: userCookie ? JSON.parse(userCookie as string) : undefined,
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setUserToken()
|
setUserToken()
|
||||||
|
|
||||||
if (cookie) {
|
if (accountCookie) {
|
||||||
console.log(`Logged in as user "${cookieData.username}"`)
|
console.log(`Logged in as user "${cookieData.user}"`)
|
||||||
|
console.log(cookieData.account, cookieData.user)
|
||||||
|
|
||||||
accountState.account.authorized = true
|
accountState.account.authorized = true
|
||||||
accountState.account.user = {
|
accountState.account.user = {
|
||||||
id: cookieData.userId,
|
id: cookieData.account.userId,
|
||||||
username: cookieData.username,
|
username: cookieData.account.username,
|
||||||
picture: '',
|
picture: cookieData.user.picture,
|
||||||
element: '',
|
element: cookieData.user.element,
|
||||||
gender: 0,
|
gender: cookieData.user.gender,
|
||||||
|
language: cookieData.user.language,
|
||||||
|
theme: cookieData.user.theme,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(`You are not currently logged in.`)
|
console.log(`You are not currently logged in.`)
|
||||||
}
|
}
|
||||||
}, [cookie, cookieData])
|
}, [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ThemeProvider>
|
<ThemeProvider>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue