Преглед изворни кода

fix(server): Invalidate token if no session found

Michael Bromley пре 7 година
родитељ
комит
a789ec7d66
1 измењених фајлова са 13 додато и 1 уклоњено
  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> {
     ): Promise<Session | undefined> {
         const authToken = extractAuthToken(req, this.configService.authOptions.tokenMethod);
         const authToken = extractAuthToken(req, this.configService.authOptions.tokenMethod);
         if (authToken) {
         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) {
         } else if (hasOwnerPermission) {
             const session = await this.authService.createAnonymousSession();
             const session = await this.authService.createAnonymousSession();
             setAuthToken({
             setAuthToken({