Skip to content

Commit

Permalink
refactor: update JwtAuthGuard constructor and canActivate method
Browse files Browse the repository at this point in the history
  • Loading branch information
typeWolffo committed Jul 11, 2024
1 parent 28b97ca commit ff805e4
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import {
CreateAccountBody,
createAccountSchema,
} from "../schemas/create-account.schema";
import { LoginBody, loginSchema } from "../schemas/login";
import { RefreshTokenBody } from "../schemas/refresh-token";
import { LoginBody, loginSchema } from "../schemas/login.schema";
import { RefreshTokenBody } from "../schemas/refresh-token.schema";
import { TokenService } from "../token.service";
import { Public } from "src/utils/decorators/public.decorator";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ import { CurrentUser } from "src/utils/decorators/user.decorator";
import {
ChangePasswordBody,
changePasswordSchema,
} from "../schemas/change-password";
import { UpdateUserBody, updateUserSchema } from "../schemas/update-user";
} from "../schemas/change-password.schema";
import {
UpdateUserBody,
updateUserSchema,
} from "../schemas/update-user.schema";
import {
AllUsersResponse,
allUsersSchema,
UserResponse,
userSchema,
} from "../schemas/user";
} from "../schemas/user.schema";
import { UsersService } from "../users.service";

@Controller("users")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@ import {
ExecutionContext,
UnauthorizedException,
} from "@nestjs/common";
import { Reflector } from "@nestjs/core";
import { JwtService } from "@nestjs/jwt";

@Injectable()
export class JwtAuthGuard implements CanActivate {
constructor(private jwtService: JwtService) {}
constructor(
private jwtService: JwtService,
private reflector: Reflector,
) {}

async canActivate(context: ExecutionContext): Promise<boolean> {
const isPublic = this.reflector.getAllAndOverride<boolean>("isPublic", [
context.getHandler(),
context.getClass(),
]);

if (isPublic) {
return true;
}

const request = context.switchToHttp().getRequest();
const token = request.cookies["access_token"];

Expand Down

0 comments on commit ff805e4

Please sign in to comment.