Skip to content

Commit

Permalink
token expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
bloombar committed Apr 17, 2023
1 parent 08dc29e commit 007c08a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
9 changes: 8 additions & 1 deletion back-end/config/jwt-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ let jwtOptions = {
const jwtVerifyToken = async function (jwt_payload, next) {
// console.log("JWT payload received", jwt_payload) // debugging

// check if the token has expired
const expirationDate = new Date(jwt_payload.exp * 1000) // convert from seconds to milliseconds
if (expirationDate < new Date()) {
// the token has expired
return next(null, false, { message: "JWT token has expired." })
}

// try to find a matching user in our database

// find this user in the database
Expand All @@ -27,7 +34,7 @@ const jwtVerifyToken = async function (jwt_payload, next) {
next(null, user)
} else {
// we didn't find the user... fail!
next(null, false)
next(null, false, { message: "User not found" })
}
}

Expand Down
1 change: 1 addition & 0 deletions back-end/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const UserSchema = new Schema({
// mongoose provides hooks that allow us to run code before or after specific events
UserSchema.pre("save", function (next) {
const user = this
// if the password has not changed, no need to hash it
if (!user.isModified("password")) return next()
// otherwise, the password is being modified, so hash it
bcrypt.hash(user.password, 10, (err, hash) => {
Expand Down
2 changes: 1 addition & 1 deletion back-end/routes/authentication-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const authenticationRouter = () => {

// a route to handle login attempts requested to /auth/login
router.post("/login", async function (req, res) {
// brab the name and password that were submitted as POST body data
// grab the name and password that were submitted as POST body data
const username = req.body.username
const password = req.body.password
// console.log(`${username}, ${password}`)
Expand Down

0 comments on commit 007c08a

Please sign in to comment.