generated from agiledev-students-fall2023/generic-project-repository
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
2 changed files
with
32 additions
and
1 deletion.
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,31 @@ | ||
import passportJWT from "passport-jwt"; | ||
import User from "../models/User.mjs"; | ||
|
||
const ExtractJwt = passportJWT.ExtractJwt | ||
const JwtStrategy = passportJWT.ExtractJwt | ||
|
||
// how the token is extracted and verified from the request | ||
const jwtOptions = { | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme("jwt"), //fromAuthHeaderAsBearerToken() | ||
secretOrKey: process.env.JWT_SECRET, | ||
} | ||
|
||
const jwtVerifyToken = async function (jwt_payload, done) { | ||
console.log("JWT payload received", jwt_payload) // debugging | ||
|
||
// token expiration | ||
|
||
// match user in database | ||
try { | ||
const user = await User.findOne({ uuid: jwt_payload.uuid }) | ||
if (!user) throw {jwtMessage: "user not found"} | ||
return done(null, user) | ||
|
||
} catch (error) { | ||
return done(null, false, {message: error.jwtMessage}) | ||
} | ||
} | ||
|
||
const jwtStrategy = new JwtStrategy(jwtOptions, jwtVerifyToken) | ||
|
||
export default jwtStrategy |
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