Skip to content

Commit

Permalink
fix: refine middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Sep 11, 2024
1 parent 328ab70 commit 681cd8d
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/infrastructures/trpcBackend/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { logger } from '../pino';

import { middleware } from './trpc';

const accessTokenRegex = /(^|;)\s*sAccessToken=([^;]*)/;
const accessTokenRegex = /(?:^|;)\s*sAccessToken=([^;]*)/;

// https://dev.to/franciscomendes10866/authentication-and-authorization-in-a-node-api-using-fastify-trpc-and-supertokens-3cgn
export const authorize = middleware(async ({ ctx, next }) => {
logger.trace('Headers: %o', ctx.req.headers);

let session;
try {
const match = ctx.req.headers.get('Cookie')?.match(accessTokenRegex);
const token = match && decodeURIComponent(match[2]);
const [, match] = ctx.req.headers.get('Cookie')?.match(accessTokenRegex) ?? [];
const token = match && decodeURIComponent(match);
if (token) session = await getSessionOnNode(token);
} catch (error) {
logger.warn(error);
Expand Down

0 comments on commit 681cd8d

Please sign in to comment.