-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
ForbiddenException, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { Request } from 'express'; | ||
import * as jwt from 'jsonwebtoken'; | ||
import { UsersService } from 'src/users/services/users.service'; | ||
|
||
@Injectable() | ||
export class AdminGuard implements CanActivate { | ||
constructor( | ||
private readonly configservice: ConfigService, | ||
private readonly userService: UsersService, | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest<Request>(); | ||
|
||
const accessToken = request?.cookies?.accessToken; | ||
if (!accessToken) { | ||
throw new UnauthorizedException('Access token not found'); | ||
} | ||
|
||
try { | ||
const payload = jwt.verify( | ||
accessToken, | ||
this.configservice.get<string>('ACCESS_SECRET'), | ||
) as any; | ||
|
||
const { userId } = payload; | ||
const user = await this.userService.getUser(userId); | ||
|
||
if (user.role === true) { | ||
return true; | ||
} | ||
|
||
throw new ForbiddenException('User does not have admin privileges'); | ||
} catch (accessError) { | ||
throw new UnauthorizedException('Invalid access token or User not found'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters