implemented session

This commit is contained in:
lnn0q 2025-03-30 20:35:24 +03:00
parent b6b7cea210
commit d179b8c7c8
9 changed files with 700 additions and 783 deletions

View file

@ -1,8 +1,8 @@
import 'dotenv/config';
import { Request, Response } from 'express';
import jwt from 'jsonwebtoken';
import { prisma } from './prisma/client';
import { Session } from '@generated/types';
export interface IdentityContext {
identity?: {
@ -18,14 +18,22 @@ export async function getIdentityContext({
req: Request;
res: Response;
}): Promise<IdentityContext> {
const authHeader = req.headers.authorization;
if (authHeader == null) return {};
const token = authHeader.split(' ')[1];
const authHeaders = req.headers.authorization;
if (authHeaders == null) return {};
const session: Session = JSON.parse(authHeaders);
console.log(session);
const accessToken = session.accessToken;
const accessTokenSecret = process.env.ACCESS_TOKEN_SECRET;
if (accessTokenSecret == null) throw new Error('No `accessToken`.');
const decoded = jwt.verify(token, accessTokenSecret) as { userId: string };
const decoded = jwt.verify(accessToken, accessTokenSecret) as {
userId: string;
iat: number;
exp: number;
};
const identity = await prisma.user.findUnique({
where: { id: decoded.userId },
select: {