Explorar o código

fix(server): Invalidate token if no session found

Michael Bromley %!s(int64=7) %!d(string=hai) anos
pai
achega
a789ec7d66
Modificáronse 1 ficheiros con 13 adicións e 1 borrados
  1. 13 1
      server/src/api/middleware/auth-guard.ts

+ 13 - 1
server/src/api/middleware/auth-guard.ts

@@ -54,7 +54,19 @@ export class AuthGuard implements CanActivate {
     ): Promise<Session | undefined> {
         const authToken = extractAuthToken(req, this.configService.authOptions.tokenMethod);
         if (authToken) {
-            return await this.authService.validateSession(authToken);
+            const session = await this.authService.validateSession(authToken);
+            if (!session) {
+                // if there is a token but it cannot be validated to a Session,
+                // then the token is no longer valid and should be unset.
+                setAuthToken({
+                    req,
+                    res,
+                    authOptions: this.configService.authOptions,
+                    rememberMe: false,
+                    authToken: '',
+                });
+            }
+            return session;
         } else if (hasOwnerPermission) {
             const session = await this.authService.createAnonymousSession();
             setAuthToken({