-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.guard.ts
42 lines (35 loc) · 1.12 KB
/
auth.guard.ts
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
import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common';
import { Request } from 'express';
export const USER_COOKIE_NAME = 'user__';
export const TEST_USER = {
userId: 123,
orgId: 1,
};
export interface AuthenticatedRequest extends Request {
user?: {
orgId: string;
userId: string;
};
}
// A basic auth guard implementation, replace with yours.
@Injectable()
export class AuthGuard implements CanActivate {
canActivate(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
const cookies = request.cookies;
if (!cookies || !cookies[USER_COOKIE_NAME]) {
throw new UnauthorizedException('Missing authentication cookie');
}
try {
const decoded = Buffer.from(cookies[USER_COOKIE_NAME], 'base64').toString('utf-8');
const user = JSON.parse(decoded);
if (user.userId !== TEST_USER.userId || user.orgId !== TEST_USER.orgId) {
throw new UnauthorizedException('Invalid authentication token');
}
request.user = user;
return true;
} catch (error) {
throw new UnauthorizedException('Invalid authentication token');
}
}
}