1. After getting the user name, password and authentication code in the background:
(1) Get the data submitted by the form
(2) Determine whether the verification code is correct or not
If the verification code is correct:
1. Encrypt the password in the form by MD5 MD5 moduleHttps://www.npmjs.com/package/md5
1,install cnpm install md5 --save 2,Introduce md5 var md5 = require('md5'); 3,Use md5(str)
2. Query the user table (collection) for the existence of the current user (mongoose operates on the mongodb database)https://www.npmjs.com/package/egg-mongoose*
1. Configure mongoose 2. model for creating operation database 3. If the database has this user (login successful): Save user information jump to background management system 4. If the database has this user (login failure): Jump to the login page
//Execute login method post async doLogin() { console.log(this.ctx.request.body); var username = this.ctx.request.body.username; var password = await this.service.tools.md5(this.ctx.request.body.password); var code = this.ctx.request.body.code; // console.log(username,password,code); if (code.toUpperCase() == this.ctx.session.code.toUpperCase()) { var result = await this.ctx.model.Admin.find({ userName: username, password: password }); if (result.length > 0) { //Login Successful // 1. Save user information this.ctx.session.userinfo = result[0]; //2. Jump to User Center this.ctx.redirect("/admin/manager"); } else { await this.error("/admin/login", "Incorrect username or password"); } } else { //Note: Asynchronous and await await this.error("/admin/login", "Authentication code error"); } }
2. Encapsulating md5 services
Install MD5 and introduce: var md5 = require('md5');
Service>Tool.jsAdd md5 method:
async md5(str){ return md5(str); }
Called this way on the page:
var password = await this.service.tools.md5(this.ctx.request.body.password);
3. mongoose Lookup User Table
The mongoose configuration is described in the text above without much introduction.
Create schema and generate model under model folder: corresponding admin table
module.exports = app => { const mongoose = app.mongoose; const Schema = mongoose.Schema; var d=new Date(); const AdminSchema = new Schema({ username: { type: String }, password: { type: String }, mobile: { type: String }, email: { type: String }, status: { type: Number,default:1 }, role_id: { type:Schema.Types.ObjectId }, add_time: { type:Number, default: d.getTime() }, is_super: { type:Number} }); return mongoose.model('Admin', AdminSchema,'admin'); }
Manually insert a fake data into the admin table of the eggxiaomi library first: the admin user password md5 decryption created is 123456
var result = await this.ctx.model.Admin.find({ userName: username, password: password });
result is the collection of users found:
When you enter the correct username password, you can find it from the table:
Then jump to the login page.
IV. Log Out
Empty the userinfo field in session, then when matching the middleware, userinfo has no value and is no longer logged on, and will be forcibly intercepted in the logon page within the three interfaces of logon page and authentication code.
async loginOut() { this.ctx.session.userinfo = null; this.ctx.redirect("/admin/login"); }
router.get('/admin/loginOut', controller.admin.login.loginOut);
In public>header:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>egg Background Management System</title> </head> <body> <!--The content below is only a placeholder and can be replaced.--> <link rel="stylesheet" href="/public/admin/bootstrap/css/bootstrap.css"> <link rel="stylesheet" href="/public/admin/css/basic.css"> <script type="text/javascript" src="/public/admin/bootstrap/js/jquery-1.10.1.js"></script> <script type="text/javascript" src="/public/admin/js/base.js"></script> <nav class="navbar navbar-inverse" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <img src="/public/admin/images/node.jpg" height="44px;" /> </div> <div class="collapse navbar-collapse" id="example-navbar-collapse"> <ul class="nav navbar-nav navbar-right"> <li><a>Welcome <%=userinfo.username%></a> </li> <li><a href="/admin/loginOut">Exit safely</a> </li> </ul> </div> </div> </nav>
That's all the core code.Details are hard to write, so you can read them later and sleep.