Spaces:
Runtime error
Runtime error
File size: 1,927 Bytes
97dab2a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import { Injectable, Inject, Scope } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Request, Response } from 'express';
@Injectable({ scope: Scope.REQUEST }) // one instance per request
export class UserSessionService {
constructor(@Inject(REQUEST) private readonly request: Request) {}
private readonly isProduction = process.env.NODE_ENV === 'production';
private readonly cookieOptions = {
httpOnly: true, // Prevents client-side JavaScript from accessing the cookie
secure: this.isProduction, // Ensures the cookie is sent over HTTPS in production
maxAge: 24 * 60 * 60 * 1000, // 1 day in milliseconds
};
// --- Getters ---
get user() {
return this.request.cookies['user'] || '';
}
get userName() {
return this.request.cookies['userName'] || '';
}
get roles(): string[] {
const roles = this.request.cookies['roles'];
return roles ? JSON.parse(roles) : [];
}
get userId() {
return this.request.cookies['userId'] || '';
}
get accessToken() {
return this.request.cookies['accessToken'] || '';
}
// --- Setters ---
setUser(response: Response, value: Object | null) {
response.cookie('user', JSON.stringify(value), this.cookieOptions);
}
setUserName(response: Response, value: string | undefined) {
response.cookie('userName', value, this.cookieOptions);
}
setRoles(response: Response, roles: string[]) {
response.cookie('roles', JSON.stringify(roles), this.cookieOptions);
}
setUserId(response: Response, value: string | undefined) {
response.cookie('userId', value, this.cookieOptions);
}
setAccessToken(response: Response, value: string) {
response.cookie('accessToken', value, this.cookieOptions);
}
// --- Clear all ---
clear(response: Response) {
['user', 'userName', 'roles', 'userId', 'accessToken'].forEach((key) =>
response.clearCookie(key),
);
}
} |