Skip to content

logout api implemented #75

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

Merged
merged 1 commit into from
Mar 31, 2024
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
4 changes: 2 additions & 2 deletions server/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ const login = errorWrapper(

const roles = await getPermittedRoleNames(PERMISSIONS.LOGIN);

console.log(roles);
console.log(user.roleName);
// console.log(roles);
// console.log(user.roleName);

if (!roles.includes(user.roleName)) {
throw new CustomError("You are not allowed to login", 403);
Expand Down
12 changes: 9 additions & 3 deletions server/src/services/Token.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import jwt, { Secret } from "jsonwebtoken";
import { Request } from "express";
import CustomError from "./CustomError";

const tokenBlacklist: Set<string> = new Set();

const getToken = (req: Request) => {
const authHeader = req.headers.authorization;
Expand All @@ -12,7 +15,7 @@ const getToken = (req: Request) => {
const generateToken = (info: any, expiry: string | number | undefined) => {
const secret: Secret | undefined = process.env.JWT_SECRET;
if (!secret) {
throw new Error("JWT secret is undefined.");
throw new CustomError("JWT secret is undefined.", 500);
}
return jwt.sign(info, secret, { expiresIn: expiry });
};
Expand All @@ -22,12 +25,15 @@ const verifyToken = (token: string) => {
if (!secret) {
throw new Error("JWT secret is undefined.");
}
if (tokenBlacklist.has(token)) {
throw new CustomError("User is logged out!", 401);
}

return jwt.verify(token, secret);
};

const invalidateToken = (token: string) => {
// This is a dummy function that does nothing.
// In a real-world application, you would probably want to blacklist the token.
tokenBlacklist.add(token);
return;
};

Expand Down