This commit is contained in:
Evert Prants 2022-09-20 17:38:22 +03:00
parent f91eb4bd73
commit 303e52da3e
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 12 additions and 19 deletions

View File

@ -1,7 +1,10 @@
import Cookies from 'cookies';
import { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
import { getUserInfo } from './api/remote';
import { COOKIE_KEYS } from './constants';
import { CurrentUserDto } from './types/user-response.interface';
import { getActiveCookie } from './utils/get-active-cookie';
const inProd = process.env.NODE_ENV === 'production';
function withUser(
handler: (
@ -10,7 +13,13 @@ function withUser(
) => Promise<GetServerSidePropsResult<any>>
) {
return async (ctx: GetServerSidePropsContext) => {
const accessToken = getActiveCookie(ctx, 'authorization');
const cookies = new Cookies(ctx.req, ctx.res, {
secure: inProd,
keys: COOKIE_KEYS,
});
const accessToken = cookies.get('authorization', { signed: true });
let user: CurrentUserDto | undefined;
if (accessToken) {
try {

View File

@ -1,17 +0,0 @@
import { GetServerSidePropsContext } from 'next';
import { getCookie } from './get-cookie';
export const getActiveCookie = (
ctx: GetServerSidePropsContext,
cookieName: string
): string | undefined => {
const setCookies = ctx.res.getHeader('set-cookie') as string[];
const fallback = ctx.req.cookies[cookieName];
const entry = setCookies?.find((item) => item.startsWith(cookieName));
if (entry === 'undefined') {
return;
}
return getCookie(cookieName, entry) || fallback;
};

View File

@ -23,6 +23,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
client_id: CLIENT_ID,
response_type: 'code',
redirect_uri: REDIRECT_URL,
prompt: 'consent',
scope: 'management',
state,
});