-
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.
* refactor: authentication * fix: remove console logs and prints * fix: lint
- Loading branch information
1 parent
7a34856
commit 83cb099
Showing
15 changed files
with
125 additions
and
243 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,55 @@ | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { IS_PUBLIC_KEY, JWT_SECRET } from './constants'; | ||
import { Request } from 'express'; | ||
import { Reflector } from '@nestjs/core'; | ||
|
||
@Injectable() | ||
export class AuthGuard implements CanActivate { | ||
constructor( | ||
private jwtService: JwtService, | ||
private reflector: Reflector, | ||
) {} | ||
|
||
// If this function returns true, the jwt token (if existing) in the incoming request is valid, | ||
// and the request gets added a 'user' field, which contains the decoded data from the jwt secret. | ||
// It works by checking if the request contains an authorization header with a Bearer token, | ||
// and validates it. | ||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [ | ||
context.getHandler(), | ||
context.getClass(), | ||
]); | ||
|
||
// For endpoints that use the @Public annotation, we directly return true and skip checking the access token | ||
if (isPublic) { | ||
return true; | ||
} | ||
const request = context.switchToHttp().getRequest(); | ||
const token = this.extractTokenFromHeader(request); | ||
if (!token) { | ||
throw new UnauthorizedException(); | ||
} | ||
try { | ||
const payload = await this.jwtService.verifyAsync(token, { | ||
secret: JWT_SECRET, | ||
}); | ||
// We're assigning the payload to the request object here | ||
// so that we can access it in our route handlers | ||
request['user'] = payload; | ||
} catch { | ||
throw new UnauthorizedException(); | ||
} | ||
return true; | ||
} | ||
|
||
private extractTokenFromHeader(request: Request): string | undefined { | ||
const [type, token] = request.headers.authorization?.split(' ') ?? []; | ||
return type === 'Bearer' ? token : undefined; | ||
} | ||
} |
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
Oops, something went wrong.