-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logout): implement logout functionality
- Loading branch information
Showing
5 changed files
with
66 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters