-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
116a4a6
commit b8057be
Showing
5 changed files
with
122 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Request, Response } from 'express'; | ||
import jwt from 'jsonwebtoken'; | ||
import { sendInternalErrorResponse } from '../validations'; | ||
import logger from '../logs/config'; | ||
import BlacklistedToken from '../database/models/blackListedToken'; | ||
import { extractTokenMiddleware } from '../helpers/tokenExtractor'; | ||
|
||
const logout = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const token = (req as any).token; | ||
|
||
jwt.verify(token, process.env.JWT_SECRET as string, (err: any) => { | ||
if (err) { | ||
res.status(401).json({ | ||
ok: false, | ||
message: 'Invalid token', | ||
}); | ||
return; | ||
} | ||
|
||
BlacklistedToken.create({ token }) | ||
.then(() => { | ||
res.status(200).json({ | ||
ok: true, | ||
message: 'Logged out successfully', | ||
}); | ||
}) | ||
.catch(error => { | ||
throw error; | ||
}); | ||
}); | ||
} catch (err: any) { | ||
const message = (err as Error).message; | ||
logger.error(message); | ||
sendInternalErrorResponse(res, err); | ||
} | ||
}; | ||
|
||
export { logout, extractTokenMiddleware }; |
28 changes: 28 additions & 0 deletions
28
src/database/migrations/20240428210949-create-blacklisted-token.js
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
await queryInterface.createTable('BlacklistedTokens', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER, | ||
}, | ||
token: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
}); | ||
}, | ||
down: async (queryInterface, Sequelize) => { | ||
await queryInterface.dropTable('BlacklistedTokens'); | ||
}, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Model, DataTypes, Sequelize } from 'sequelize'; | ||
import sequelize from './index'; | ||
|
||
class BlacklistedToken extends Model { | ||
public id!: number; | ||
public token!: string; | ||
|
||
// timestamps! | ||
public readonly createdAt!: Date; | ||
public readonly updatedAt!: Date; | ||
} | ||
|
||
BlacklistedToken.init( | ||
{ | ||
id: { | ||
type: DataTypes.INTEGER, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
}, | ||
token: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
}, | ||
{ | ||
sequelize: sequelize as Sequelize, | ||
modelName: 'BlacklistedToken', | ||
tableName: 'blacklisted_tokens', | ||
timestamps: true, | ||
} | ||
); | ||
|
||
export default BlacklistedToken; |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
|
||
const extractTokenMiddleware = (req: Request, res: Response, next: NextFunction) => { | ||
const token = req.headers.authorization?.split(' ')[1]; | ||
if (!token) { | ||
res.status(400).json({ | ||
ok: false, | ||
message: 'Token not provided', | ||
}); | ||
return; | ||
} | ||
(req as any).token = token; | ||
next(); | ||
}; | ||
|
||
export { extractTokenMiddleware }; |
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