Implementation of simple identity Authentication with express+session
Environment initialization
First, initialize the project
Express-e
Then, install the dependency.
Npm install
Next, install the session-related packages.
Npm install-save express-session session-file-storesession related configuration
The configuration is as follows, and it is not complicated. You can see the code comments or refer to the official documentation.
Var express = require ('express'); var app = express (); var session = require (' express-session'); var FileStore = require ('session-file-store') (session); var identityKey =' skey' App.use (session ({name: identityKey, secret: 'chyingp', / / is used to sign session id-related cookie store: new FileStore (), / / local storage session (text file, you can also choose other store, such as redis) saveUninitialized: false, / / whether to automatically save uninitialized sessions, it is recommended that false resave: false, / / whether to resave the session every time Recommended false cookie: {maxAge: 10 * 1000 / / valid (in milliseconds}})) Implement login / logout interface to create test account data
First, create a file locally to save the account information that can be used to log in, avoiding the hassle of creating a linked database.
/ / users.jsmodule.exports = {items: [{name: 'chyingp', password:' 123456'}]}; implementation of login and logout interfaces
Implement login and logout interfaces, where:
Login: if the user exists, create a session through req.regenerate, save it locally, and save the session id to the user side through Set-Cookie
Logout: destroy session and clear cookie
Var users = require ('. / users'). Items;var findUser = function (name, password) {return users.find (function (item) {return item.name = name & & item.password = password;});}; / / login interface app.post ('/ login', function (req, res, next) {var sess = req.session; var user = findUser (req.body.name, req.body.password) If (user) {req.session.regenerate (function (err) {if (err) {return res.json ({ret_code: 2, ret_msg: 'login failed'});} req.session.loginUser = user.name Res.json ({ret_code: 0, ret_msg: 'login successful'});} else {res.json ({ret_code: 1, ret_msg: 'wrong account or password'});}}) / / log in to app.get ('/ logout', function (req, res, next) {/ / Note: the session-file-store used here is in the destroy method, but the cookie / / of the client is not destroyed, so the cookie of the client still exists, resulting in problems-- > after logging out, the server detects cookie / / and then looks for the corresponding session file. Error / / bug req.session.destroy of session-file-store itself (function (err) {if (err) {res.json ({ret_code: 2, ret_msg: 'logout failed'}) Return;} / / req.session.loginUser = null; res.clearCookie (identityKey); res.redirect ('/');}); login status judgment
When a user accesses http://127.0.0.1:3000, determine whether the user is logged in, if so, call to the user details interface (crude); if not, skip to the login interface
App.get ('/', function (req, res, next) {var sess = req.session; var loginUser = sess.loginUser; var isLogined =!! loginUser; res.render ('index', {isLogined: isLogined, name: loginUser | |'}); UI interface
Finally, take a look at the code related to logging in and logging out of UI.
Session management session management
Current login user: log out login
$('# login') .click (function (evt) {evt.preventDefault () $.ajax ({url:'/ login', type: 'POST', data: {name: $(' # name'). Val (), password: $('# password'). Val ()} Success: function (data) {if (data.ret_code = = 0) {location.reload () );})