Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Errors on logout #524

Merged
merged 3 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Controller, UseGuards, Post, Get, Res, Req } from "@nestjs/common";
import {
Controller,
UseGuards,
Post,
Get,
Res,
Req,
HttpCode,
} from "@nestjs/common";
import { LocalAuthGuard } from "./guards/local-auth.guard";
import { AuthService } from "./auth.service";
import { JwtAuthGuard } from "./guards/jwt-auth.guard";
Expand Down Expand Up @@ -107,9 +115,9 @@ export class AuthController {
status: 200,
description: "User logged out",
})
@HttpCode(200)
@Post("logout")
async logout(@Req() req: Request, @Res() res: Response) {
await this.authService.logout(req, res);
res.send({ logout: "successful" });
async logout(@Req() req: Request) {
return this.authService.logout(req);
}
}
28 changes: 15 additions & 13 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { HttpStatus, Injectable } from "@nestjs/common";
import { Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { JwtService } from "@nestjs/jwt";
import { compare } from "bcrypt";
import { User } from "src/users/schemas/user.schema";
import { UsersService } from "../users/users.service";
import { Request, Response } from "express";
import { Request } from "express";
import { OidcConfig } from "src/config/configuration";
import { parseBoolean } from "src/common/utils";
import { Issuer } from "openid-client";
Expand Down Expand Up @@ -52,33 +52,33 @@ export class AuthService {
};
}

async logout(req: Request, res: Response) {
async logout(req: Request) {
const logoutURL = this.configService.get<string>("logoutURL") || "";
const expressSessionSecret = this.configService.get<string>(
"expressSessionSecret",
);

if (expressSessionSecret) {
await req.logout(async (err) => {
req.logout(async (err) => {
if (err) {
// we should provide a message
console.log("Logout error");
console.log(err);
//res.status(HttpStatus.BAD_REQUEST);
}
await this.additionalLogoutTasks(req, res, logoutURL);
return await this.additionalLogoutTasks(req, logoutURL);
});
} else {
await this.additionalLogoutTasks(req, res, logoutURL);
return await this.additionalLogoutTasks(req, logoutURL);
}
if (logoutURL) {
res.redirect(logoutURL);
return { logout: "successful", logoutURL: logoutURL };
}
res.status(HttpStatus.OK);
return;

return { logout: "successful" };
}

async additionalLogoutTasks(req: Request, res: Response, logoutURL: string) {
async additionalLogoutTasks(req: Request, logoutURL: string) {
const user = req.user as Omit<User, "password">;
if (user?.authStrategy == "oidc") {
const oidcConfig = this.configService.get<OidcConfig>("oidc");
Expand All @@ -89,10 +89,12 @@ export class AuthService {
);
const end_session_endpoint = trustIssuer.metadata.end_session_endpoint;
if (end_session_endpoint) {
res.redirect(
end_session_endpoint +
return {
logout: "successful",
logoutURL:
end_session_endpoint +
(logoutURL ? "?post_logout_redirect_uri=" + logoutURL : ""),
);
};
}
}
}
Expand Down
17 changes: 12 additions & 5 deletions src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import {
Delete,
UseInterceptors,
Put,
//UnauthorizedException,
Body,
Res,
ForbiddenException,
HttpCode,
} from "@nestjs/common";
import {
ApiBearerAuth,
Expand Down Expand Up @@ -272,10 +271,18 @@ export class UsersController {
}

@UseGuards(JwtAuthGuard)
@ApiOperation({
summary: "It logs the current user out.",
description: "It logs out the current user.",
})
@ApiResponse({
status: 200,
description: "User logged out",
})
@HttpCode(200)
@Post("logout")
async logout(@Req() req: Request, @Res() res: Response) {
await this.authService.logout(req, res);
return res.send({ logout: "successful" });
async logout(@Req() req: Request) {
return this.authService.logout(req);
}

@UseGuards(PoliciesGuard)
Expand Down