Created
March 6, 2023 13:13
-
-
Save rodrigobertin/d0d448d0bddfe3ebe24eccad13509c0b to your computer and use it in GitHub Desktop.
Chekc token Express
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
import {NextFunction, Request, Response} from "express"; | |
import Users from "../apps/users/users"; | |
export const checkToken = async (req: Request, res: Response, next: NextFunction) => { | |
const headers = req.headers; | |
if (!headers.authorization) { | |
res.status(401).json({ | |
message: "No token send!!!" | |
}) | |
return | |
} else { | |
let token = headers.authorization.split(' ').pop() | |
const user = await Users.findOne({'token': token}); | |
if (!user) { | |
res.status(401).json({ | |
message: "Invalid token or expired!!!" | |
}); | |
return | |
} | |
} | |
next() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment