I have done a small project before, which involves the function of user registration and login. Now I will record the implementation process for future maintenance.
directory
- The principle of analysis
- Practical cases
The principle of analysis
Simple flowchart for user registration
- Enter a user name and password
- Check whether the user exists
- If yes, a message is displayed
- No registration succeeded. Procedure
Simple flowchart for user login
Practical cases
Mysql built table
SQL > create table CURD; SQL > create table CURD
Create table
mysql> CREATE TABLE `user` (
`id` int(11) NOT NULL COMMENT 'id',
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Username',
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'password',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update Time'
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='User table';
Copy the code
The node to write
According to the above flow chart, start to write the program.
Before we write. Start by installing a dependency package in your project that generates mysql statements.
npm install xqsql
Copy the code
XQSQL NPM address
Specific use method, click the address for documentation.
- User registration
app.post('/reg'.async (req, res) => {
let params = req.body;
if (params.name && params.password) {
let getSql = xqsql.get('user', {
type: 'one'.key: 'name'.ids: [params.name],
}, 'default'.'id,name');
let getSqlResult = await db(getSql);
if (getSqlResult.code == 200 &&
getSqlResult.data.list.length == 0) {
let addParams = [
{
name: params.name,
password: params.password,
}
]
let addFields = [
{
name: 'Username'.value: 'name'.isMust: true
},
{
name: 'password'.value: 'password'.isMust: true},]let addSql = xqsql.add('user', addParams, addFields);
let addSqlResult = await db(addSql);
if (addSqlResult.code == 200) {
return res.json({
code: 200.msg: 'get_succ'.data: {
info: "Registration successful!",}}); }else {
returnres.json(addSqlResult); }}else {
return res.json({
code: 101.msg: 'get_succ'.data: {
info: "User already exists!,}})}}else {
return res.json({
code: 101.msg: 'get_succ'.data: {
info: "Username and password cannot be empty!",}})}})Copy the code
- The user login
The user information for logging in to this section is stored in session, or you can choose JWT, this time just session.
Install dependency packages; npm install express-session cookie-parser
/ / introduction
const session = require('express-session');
const cookieParser = require('cookie-parser');
/ / configuration
app.use(cookieParser());
app.use(session({
resave: true.saveUninitialized: false.secret: 'demo'
}))
Copy the code
app.post('/login'.async (req, res) => {
let params = req.body;
if (params.name && params.password) {
let getSql = xqsql.get('user', {
type: 'one'.key: 'name'.ids: [params.name],
}, 'default'.'id,name,password');
let getSqlResult = await db(getSql);
if (getSqlResult.code == 200 &&
getSqlResult.data.list.length) {
let userInfo = getSqlResult.data.list[0];
if (params.name == getSqlResult.data.list[0].name &&
params.password == getSqlResult.data.list[0].password) {
let loginInfo = req.session.user;
if (loginInfo && loginInfo.name == params.name) {
return res.json({
code: 101.msg: 'get_fail'.data: {
info: "User logged in!",}}); }else {
let user = {
name: params.name,
}
req.session.user = user;
return res.json({
code: 200.msg: 'get_succ'.data: {
info: "Login successful!",}}); }}else {
return res.json({
code: 101.msg: 'get_fail'.data: {
info: "Wrong username or password!",}})}}else {
return res.json({
code: 101.msg: 'get_fail'.data: {
info: "The user does not exist!",}})}}else {
return res.json({
code: 101.msg: 'get_succ'.data: {
info: "Username and password cannot be empty!",}})}})Copy the code
- Log out
In this case, it is easy to change the user information to NULL.
app.post('/logout'.(req, res) = > {
let user = req.session.user;
if(user && user.name ! =' ') {
req.session.user = null;
return res.json({
code: 200.msg: 'get_succ'.data: {
info: "Logged out successfully!",}}); }else {
return res.json({
code: 101.msg: 'get_fail'.data: {
info: "User not logged in!",}})}})Copy the code
This summary is written here, of course, this is only the most basic registration and login function, but very complex registration and login is also in the most basic above processing complex, the most important is to cultivate logical thinking ability. How to implement a feature and optimize it to its best.