Skip to content

Instantly share code, notes, and snippets.

@rodrigobertin
Created March 6, 2023 13:13
Show Gist options
  • Save rodrigobertin/d0d448d0bddfe3ebe24eccad13509c0b to your computer and use it in GitHub Desktop.
Save rodrigobertin/d0d448d0bddfe3ebe24eccad13509c0b to your computer and use it in GitHub Desktop.
Chekc token Express
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