Created
March 14, 2019 16:51
-
-
Save joewagner/0a4600009384d3b9e5a815234376587a to your computer and use it in GitHub Desktop.
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
const express = require("express"); | |
const session = require("express-session"); | |
const cors = require("cors"); | |
const app = express(); | |
app.use(cors({ | |
origin: ["http://localhost:3000"], credentials: true | |
} | |
)); | |
app.use(session({ | |
secret: "my-super-duper-secret", | |
saveUninitialized: false, | |
resave: true, | |
cookie: { | |
maxAge: 604800000, | |
secure: false, | |
}, | |
rolling: true, | |
}) | |
); | |
const getMessage = function (req) { | |
const message = 'Session Id: ' + req.sessionID + '\nAuthenticated User: ' + (req.session && req.session.username); | |
return message | |
} | |
app.all("/", (req, res) => { | |
res.status(200).send({message: "Yup I am running"}); | |
}); | |
app.get("/login", (req, res) => { | |
req.session.username = 'Timbo'; | |
req.session.save(err => { | |
if (err) throw err; | |
console.log(getMessage(req)); | |
res.status(200).send({message: "Success", session: getMessage(req)}); | |
}); | |
}); | |
app.get("/logout", (req, res) => { | |
console.log("Session before destroying it", req.sessionID); | |
req.session.destroy((err) => { | |
if (err) throw err; | |
res.status(200).send({message: "Success", session: getMessage(req)}); | |
console.log("This session is NEVER undefined when the rolling flag is set", req.sessionID); | |
}); | |
}); | |
app.listen(3001, () => { | |
console.log("The server is now reachable at: http://localhost:3001"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment