refactor: some adjustments

This commit is contained in:
Daniel Soares
2021-12-19 20:03:59 -03:00
parent a9ff01aa52
commit e4b037f273
52 changed files with 417 additions and 481 deletions

38
src/utils/cookies.ts Normal file
View File

@@ -0,0 +1,38 @@
import { destroyCookie, setCookie } from 'nookies'
import { authConstants } from 'constants/auth'
import { api } from 'services/api'
import type { GetServerSidePropsContext } from 'next'
type SetCookiesProps = {
ctx?: GetServerSidePropsContext
accessToken: string
refreshToken: string
}
export function setCookies({ ctx = undefined, accessToken, refreshToken }: SetCookiesProps) {
const { SESSION_EXPIRES_IN_S } = authConstants
// @ts-ignore
api.defaults.headers['authorization'] = `Bearer ${accessToken}`
setCookie(ctx, 'accessToken', accessToken, {
maxAge: SESSION_EXPIRES_IN_S,
path: '/'
})
setCookie(ctx, 'refreshToken', refreshToken, {
maxAge: SESSION_EXPIRES_IN_S,
path: '/'
})
}
export function destroyCookies(ctx: GetServerSidePropsContext | undefined = undefined) {
destroyCookie(ctx, 'accessToken', {
path: '/'
})
destroyCookie(ctx, 'refreshToken', {
path: '/'
})
}