Skip to content
Snippets Groups Projects
Unverified Commit 8ecbc7b1 authored by Marius Friess's avatar Marius Friess Committed by GitHub
Browse files

Refresh jwt on every request (#20)

* refresh jwt inside of AuthGuard requests

* remove obsolete variable

* change var name
parent 0f3257e6
No related branches found
No related tags found
No related merge requests found
import { Injectable } from '@nestjs/common';
import { AuthGuard as PassportAuthGuard } from '@nestjs/passport';
import { AuthService } from './auth.service';
import { JwtService } from '@nestjs/jwt';
@Injectable()
export class AuthGuard extends PassportAuthGuard('jwt') {}
export class AuthGuard extends PassportAuthGuard('jwt') {
constructor(
private readonly auth: AuthService,
private readonly jwtService: JwtService,
) {
super();
}
async canActivate(context) {
const canActivateResult = (await super.canActivate(context)) as boolean;
if (canActivateResult) {
const response = context.switchToHttp().getResponse();
const user = await this.auth.user();
const payload = { sub: user.id };
const token = this.jwtService.sign(payload);
response.cookie('jwt', token, { httpOnly: true });
}
return canActivateResult;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment