Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implementation of simple identity Authentication with express+session

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

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 () );})

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report