Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

linting improvements

Blue Mousef85f9b76dda7
diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts
index f4fb7b62f..893f23772 100644
--- a/src/auth/auth.service.ts
+++ b/src/auth/auth.service.ts
@@ -444,10 +444,15 @@ export class AuthService {
userId,
isSecondFactorAuthenticated,
)
- const refreshToken = await this.generateJwtRefreshToken(
- userId,
- isSecondFactorAuthenticated,
- )
+
+ let refreshToken = 'NOT_AVAILABLE'
+
+ if (isSecondFactorAuthenticated) {
+ refreshToken = await this.generateJwtRefreshToken(
+ userId,
+ isSecondFactorAuthenticated,
+ )
+ }
return { accessToken, refreshToken }
}
diff --git a/src/auth/guards/roles.guard.ts b/src/auth/guards/roles.guard.ts
index 84c0dcd95..e00678ba8 100644
--- a/src/auth/guards/roles.guard.ts
+++ b/src/auth/guards/roles.guard.ts
@@ -1,9 +1,14 @@
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'
import { Reflector } from '@nestjs/core'
+import { ExtractJwt } from 'passport-jwt'
+import { verify } from 'jsonwebtoken'
+
import { UserType } from 'src/user/entities/user.entity'
import { UserService } from 'src/user/user.service'
import { IS_TWO_FA_NOT_REQUIRED_KEY, ROLES_KEY } from '../decorators'
+const ACCESS_TOKEN_SECRET = process.env.JWT_ACCESS_TOKEN_SECRET
+
@Injectable()
export class RolesGuard implements CanActivate {
constructor(
@@ -41,11 +46,34 @@ export class RolesGuard implements CanActivate {
if (!hasRole) return false
- if (
- !isTwoFaNotRequired &&
- user.isTwoFactorAuthenticationEnabled &&
- userFromRequest.isSecondFactorAuthenticated
- ) {
+ if (isTwoFaNotRequired) {
+ return true
+ }
+
+ let token = ''
+ if (request.cookies.token) {
+ token = request.cookies.token
+ } else {
+ const extract = ExtractJwt.fromAuthHeaderAsBearerToken()
+ token = extract(request)
+ }
+
+ try {
+ const decoded: any = verify(token, ACCESS_TOKEN_SECRET)
+
+ // If the token is not decoded, it means it's invalid
+ if (!decoded) {
+ return false
+ }
+
+ // If the user has 2FA enabled, but the token is temporary (meant to be used for 2FA routes only) then return false
+ if (
+ user.isTwoFactorAuthenticationEnabled &&
+ !decoded.isSecondFactorAuthenticated
+ ) {
+ return false
+ }
+ } catch {
return false
}