Browse Source

Fix internal server error on failed social login by redirecting to login page

pull/7637/head
Thomas Kaul 2 weeks ago
parent
commit
8c39e6834e
  1. 9
      apps/api/src/app/auth/auth.controller.ts
  2. 21
      apps/api/src/guards/oauth-callback.guard.ts

9
apps/api/src/app/auth/auth.controller.ts

@ -2,6 +2,7 @@ import { WebAuthService } from '@ghostfolio/api/app/auth/web-auth.service';
import { AllowDuringImpersonation } from '@ghostfolio/api/decorators/allow-during-impersonation.decorator';
import { CustomThrottlerGuard } from '@ghostfolio/api/guards/custom-throttler.guard';
import { HasPermissionGuard } from '@ghostfolio/api/guards/has-permission.guard';
import { OAuthCallbackGuard } from '@ghostfolio/api/guards/oauth-callback.guard';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { DEFAULT_LANGUAGE_CODE } from '@ghostfolio/common/config';
import {
@ -62,13 +63,13 @@ export class AuthController {
}
@Get('google/callback')
@UseGuards(AuthGuard('google'))
@UseGuards(OAuthCallbackGuard('google'))
@Version(VERSION_NEUTRAL)
public googleLoginCallback(
@Req() request: Request,
@Res() response: Response
) {
const jwt: string = (request.user as any).jwt;
const jwt: string = (request.user as any)?.jwt;
if (jwt) {
response.redirect(
@ -98,10 +99,10 @@ export class AuthController {
}
@Get('oidc/callback')
@UseGuards(AuthGuard('oidc'))
@UseGuards(OAuthCallbackGuard('oidc'))
@Version(VERSION_NEUTRAL)
public oidcLoginCallback(@Req() request: Request, @Res() response: Response) {
const jwt: string = (request.user as any).jwt;
const jwt: string = (request.user as any)?.jwt;
if (jwt) {
response.redirect(

21
apps/api/src/guards/oauth-callback.guard.ts

@ -0,0 +1,21 @@
import { Logger, mixin, Type } from '@nestjs/common';
import { AuthGuard, IAuthGuard } from '@nestjs/passport';
export function OAuthCallbackGuard(strategy: string): Type<IAuthGuard> {
class OAuthCallbackGuardMixin extends AuthGuard(strategy) {
private readonly logger = new Logger(OAuthCallbackGuard.name);
public override handleRequest(error: Error, user: any) {
if (error) {
this.logger.error(
`Authentication with the ${strategy} strategy has failed: ${error.message}`
);
}
// Do not throw, the callback handler redirects to the login page instead
return user;
}
}
return mixin(OAuthCallbackGuardMixin);
}
Loading…
Cancel
Save