Skip to content

Commit

Permalink
initial commit on passport jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
Nina1o1 committed Dec 7, 2023
1 parent a95d41a commit f79465d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions back-end/src/config/jwt-config.mjs
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
2 changes: 1 addition & 1 deletion back-end/src/models/User.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const userSchema = new mongoose.Schema({
__v: {
type: Number // Number type for the version key
}
});
})

const User = model('User', userSchema,'users');
export default User;

0 comments on commit f79465d

Please sign in to comment.