Skip to content

Commit

Permalink
fix(core): Fix intermittent "no active session" errors
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed May 23, 2019
1 parent c0ea092 commit 1313ca7
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions packages/core/src/api/middleware/auth-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,33 @@ export class AuthGuard implements CanActivate {
hasOwnerPermission: boolean,
): Promise<Session | undefined> {
const authToken = extractAuthToken(req, this.configService.authOptions.tokenMethod);
let session: Session | undefined;
if (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: '',
});
session = await this.authService.validateSession(authToken);
if (session) {
return session;
}
return session;
} else if (hasOwnerPermission) {
const session = await this.authService.createAnonymousSession();
// 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: '',
});
}

if (hasOwnerPermission && !session) {
session = await this.authService.createAnonymousSession();
setAuthToken({
authToken: session.token,
rememberMe: true,
authOptions: this.configService.authOptions,
req,
res,
});
return session;
}
return session;
}
}

0 comments on commit 1313ca7

Please sign in to comment.