Skip to content

Commit

Permalink
feat(logout): implement logout functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrixA committed Apr 24, 2024
1 parent 7a2328c commit 50e27d7
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 56 deletions.
57 changes: 34 additions & 23 deletions src/controllers/logoutController.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
import { Blacklist } from "../database/models/blacklist";
import { HttpException } from "../utils/http.exception";
import { Request, Response } from "express";
import { v4 as uuidv4 } from 'uuid';
import { v4 as uuidv4 } from "uuid";

const logout = async (req: Request, res: Response)=>{
try {
const token = req.params.token;
console.log(token);
const logout = async (req: Request, res: Response) => {
try {
const token = req.params.token;
console.log(token);

if (!token){
return res.status(404).json( new HttpException("NOT FOUND", "Token Not Found"));
}

const blacklistedToken = await Blacklist.findOne({ where: { token } });

if (!blacklistedToken){
await Blacklist.create({id: uuidv4(), token: token});
return res.status(201).json(new HttpException("CREATED", "Logged out successfully"));
}

return res.status(401).json(new HttpException("UNAUTHORIZED", "Already logged out"));
if (!token) {
return res
.status(404)
.json(new HttpException("NOT FOUND", "Token Not Found"));
}


} catch (error) {
console.error("Error during logout:", error);
return res.status(500).json(new HttpException("INTERNAL_SERVER_ERROR", "An internal server error occurred"));
}
const blacklistedToken = await Blacklist.findOne({ where: { token } });

if (!blacklistedToken) {
await Blacklist.create({ id: uuidv4(), token: token });
return res
.status(201)
.json(new HttpException("CREATED", "Logged out successfully"));
}

return res
.status(401)
.json(new HttpException("UNAUTHORIZED", "Already logged out"));
} catch (error) {
console.error("Error during logout:", error);
return res
.status(500)
.json(
new HttpException(
"INTERNAL_SERVER_ERROR",
"An internal server error occurred",
),
);
}
};

export default logout;
export default logout;
2 changes: 1 addition & 1 deletion src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const login = async (req: Request, res: Response, next: NextFunction) => {
.json(new HttpException("BAD REQUEST", "Bad Request!"));
}

if (info){
if (info) {
return res
.status(404)
.json(new HttpException("NOT FOUND", info.message));
Expand Down
42 changes: 21 additions & 21 deletions src/database/models/blacklist.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { DataTypes, Model } from "sequelize";
import { sequelizeConnection } from "../config/db.config";

export interface BlacklistModelAtributes{
id: string,
token: string
export interface BlacklistModelAtributes {
id: string;
token: string;
}

export class Blacklist extends Model<BlacklistModelAtributes> {};
export class Blacklist extends Model<BlacklistModelAtributes> {}

Blacklist.init(
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false
},
token: {
type: DataTypes.STRING,
allowNull: false
}
},
{
sequelize: sequelizeConnection,
tableName: "Blacklisted_tokens"
}
);
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
},
token: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
sequelize: sequelizeConnection,
tableName: "Blacklisted_tokens",
},
);
20 changes: 10 additions & 10 deletions src/middlewares/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export const authenticateUser = async (
try {
const verifiedToken = jwt.verify(token, JWT_SECRET as string) as JwtPayload;
const isInBlcaklist = await Blacklist.findOne({ where: { token } });

if (!verifiedToken) {
return res.status(401).json({ message: "please login to continue!" });
}

if (isInBlcaklist){
return res.status(401).json({message: "Token arleady invalidated"});
if (isInBlcaklist) {
return res.status(401).json({ message: "Token arleady invalidated" });
}

req.UserId = verifiedToken;
Expand Down Expand Up @@ -72,8 +72,8 @@ export const isBuyer = async (
return res.status(401).json({ message: "please login to continue!" });
}

if (isInBlcaklist){
return res.status(401).json({message: "Token arleady invalidated"});
if (isInBlcaklist) {
return res.status(401).json({ message: "Token arleady invalidated" });
}

if (decoded.role !== "buyer") {
Expand All @@ -86,7 +86,7 @@ export const isBuyer = async (
};

//only vendors
export const isVendor = async(
export const isVendor = async (
req: ExpandedRequest,
res: Response,
next: NextFunction,
Expand All @@ -103,8 +103,8 @@ export const isVendor = async(
return res.status(401).json({ message: "please login to continue!" });
}

if (isInBlcaklist){
return res.status(401).json({message: "Token arleady invalidated"});
if (isInBlcaklist) {
return res.status(401).json({ message: "Token arleady invalidated" });
}

if (decoded.role !== "vendor") {
Expand Down Expand Up @@ -134,8 +134,8 @@ export const isAdmin = async (
return res.status(401).json({ message: "please login to continue!" });
}

if (isInBlcaklist){
return res.status(401).json({message: "Token arleady invalidated"});
if (isInBlcaklist) {
return res.status(401).json({ message: "Token arleady invalidated" });
}

if (decoded.role !== "admin") {
Expand Down
1 change: 0 additions & 1 deletion src/services/user.services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { hashPassword } from "../utils/password";
import { User } from "../database/models/User";
import { UserModelAttributes } from "../database/models/User";
interface UserInt {
userName: string;
firstName: string;
Expand Down

0 comments on commit 50e27d7

Please sign in to comment.