mirror of
https://github.com/dsoaress/link-free.git
synced 2026-08-18 23:46:04 +01:00
refactor: some adjustments
This commit is contained in:
38
src/utils/cookies.ts
Normal file
38
src/utils/cookies.ts
Normal 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: '/'
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user