当前位置:网站首页>Vue+mysql login registration case
Vue+mysql login registration case
2022-06-27 22:34:00 【Meow meow more】
Vue+MySQL Implement login and registration cases
1. newly build vue Project and connect to the database
Please refer to vue Connect mysql database
2. New login page 、 Registration page and home page
stay src/views Under the folder , newly build login.vue( The login page )、register.vue( Registration page ) and home.vue( home page )
Build a page according to your own preferences ( I use here elementUI The components of ,cv Install before elementUI middleware )
npm i element-ui -S
The login page


4. Page routing configuration
stay src/router/index.js Route corresponding to the configuration page in
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const home = () => import("../views/home.vue") // Lazy loading
const login = () => import("../views/login.vue")
const register = () => import("../views/register.vue")
const routes = [
{
path: '',
redirect: '/login' // Redirect
},
{
path: '/login',
name: 'login',
component: login
},
{
path: '/register',
name: 'register',
component: register
},
{
path: '/home',
name: 'home',
component: home,
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
// Solve the same path jump error reporting problem
// Use push Methods
const RouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
return RouterPush.call(this, to).catch(err => err)
}
// Use replace Methods
const RouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (to) {
return RouterReplace.call(this, to).catch(err => err)
}
export default router
4. newly build /server/API/login.js
receive req.query / req.bosy The parameters passed in , Query the corresponding data through the query statement and put back the results
let db = require('../db/index')
exports.login = (req, res) => {
var sql = 'select * from user where name = ? and password = ?'
db.query(sql, [req.query.name, req.query.password], (err, data) => {
if(err) {
return res.send({
status: 400,
message: " Login failed "
})
}
if(data.length > 0) {
res.send({
status: 200,
message: " Login successful "
})
}else{
res.send({
status: 202,
message: ' Wrong user name or password '
})
}
})
}
exports.register = (req, res) => {
const sql1 = 'select * from user where name = ?'
const sql2 = 'insert into user (name, password) value (?, ?)'
db.query(sql1, [req.body.params.name], (err, data) => {
if(err) {
return res.send({
status: 400,
message: " operation failed "
})
}
if(data.length > 0) {
return res.send({
status: 202,
message: ' User name already exists '
})
}else{
db.query(sql2, [req.body.params.name, req.body.params.password], (err, data) => {
if(err) {
return res.send({
status: 400,
message: " Registration failed "
})
}
res.send({
status: 200,
message: " Registered successfully "
})
})
}
})
}
5. stay /server/router.js Configure the corresponding route in
let express = require('express')
let router = express.Router()
let login = require('./API/login')
router.get('/login', login.login)
router.post('/register', login.register)
module.exports = router
6. stay /views/login.vue、/views/register.vue and /views/home.vue Write corresponding methods in
<template>
<div class="bg">
<div id="login">
<h2> The login page </h2>
<el-form ref="form" :model="form" label-width="20%">
<el-form-item label=" user name :">
<el-input v-model="form.username"></el-input>
</el-form-item>
<el-form-item label=" The secret code :">
<el-input v-model="form.password" type="password"></el-input>
</el-form-item>
</el-form>
<el-button type="primary" round @click="login" class="btn"> Sign in </el-button>
</div>
</div>
</template>
<script>
import axios from "axios"
export default {
data () {
return {
form: {
username: '',
password: ''
}
};
},
methods: {
login() {
if(this.form.username == '') {
this.$message.error(' The username cannot be empty ');
}else if(this.form.password == '') {
this.$message.error(' The password cannot be empty ');
}else{
axios.get('http://127.0.0.1/login', {
params: {
name: this.form.username,
password: this.form.password
}
}).then(res=>{
if(res.data.status == 200) {
this.$router.push({
path: '/home',
query: {
name: this.form.username
}
})
}else{
this.$alert(' Wrong user name or password ', ' Login failed ', {
confirmButtonText: ' determine ',
callback: action => {
this.form.username = '',
this.form.password = ''
}
});
}
}).catch(err=>{
console.log(" Login failed " + err);
})
}
},
register() {
this.$router.push('/register')
}
}
}
</script>
<template>
<div class="bg">
<div id="register">
<h2> Registration page </h2>
<el-form ref="form" :model="form" label-width="20%">
<el-form-item label=" user name :">
<el-input v-model="form.username"></el-input>
</el-form-item>
<el-form-item label=" The secret code :">
<el-input v-model="form.password" type="password"></el-input>
</el-form-item>
</el-form>
<el-button type="primary" round @click="register" class="btn"> register </el-button>
</div>
</div>
</template>
<script>
import axios from "axios"
export default {
data () {
return {
form: {
username: '',
password: ''
},
isnull: false
};
},
methods: {
register() {
if(this.form.username == '') {
this.$message.error(' The username cannot be empty ');
}else if(this.form.password == '') {
this.$message.error(' The password cannot be empty ');
}else{
axios.post('http://127.0.0.1/register', {
params: {
name: this.form.username,
password: this.form.password
}
}).then(res => {
// console.log(res.data.message);
if(res.data.status == 200) {
this.$alert(' Whether to return to the login page ', ' Registered successfully ', {
confirmButtonText: ' determine ',
callback: action => {
this.$router.push('/login')
}
})
}else if(res.data.status == 202) {
this.$alert(' User name already exists ', ' Registration failed ', {
confirmButtonText: ' determine ',
callback: action => {
this.form.username = '',
this.form.password = ''
}
})
}else{
console.log(res.message);
}
}).catch(err => {
console.log(' operation failed ' + err);
})
}
}
}
}
</script>
<template>
<div id="main">
<el-container>
<el-header>
<div class="logo" >
<img src="../assets/img/logo.png"> <!-- Please prepare the pictures here in advance -->
</div>
<div class="user">
{
{username}}
</div>
</el-header>
<el-main>main</el-main>
<el-footer>Footer</el-footer>
</el-container>
</div>
</template>
<script>
export default {
name: 'Main',
data() {
return{
username: ''
}
},
created() { // When creating a page , Assign the user name passed from the route to data Medium username, In this way, the user name can be displayed on the page ( See the top right corner of the home page for the effect )
this.username = this.$route.query.name;
}
}
</script>
Effect display
Log in to register demo
git Source code address :https://gitee.com/xie-xiaochun/login-registration-demo
Be careful : The resource does not contain a database , You need to create your own database , And modify the relevant information of the database in the source code .
边栏推荐
- Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
- 爬虫笔记(1)- urllib
- 百万年薪独家专访,开发人员不修复bug怎么办?
- 對話喬心昱:用戶是魏牌的產品經理,零焦慮定義豪華
- Gbase 8A OLAP analysis function cume_ Example of dist
- Flask application case
- Codeforces Round #721 (Div. 2)
- 解决本地连接不上虚拟机的问题
- Introduction to ARCS Model
- Acwing week 57 longest continuous subsequence - (binary or tree array)
猜你喜欢

PCIe knowledge point -008: structure of PCIe switch

Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing

Gao fushuai in the unit testing industry, pytest framework, hands-on teaching, will do this in the future test reports~

【Redis】零基础十分钟学会Redis

信通院举办“业务与应用安全发展论坛” 天翼云安全能力再获认可

average-population-of-each-continent

Luogu p5706 redistributing fertilizer and house water

go语言切片Slice和数组Array对比panic: runtime error: index out of range问题解决

百万年薪独家专访,开发人员不修复bug怎么办?

使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次
随机推荐
Example of using gbase 8A OLAP function group by grouping sets
How to open an account for agricultural futures? How much is the handling charge for opening an account for futures? Who can give you a preferential handling charge?
【mysql实战】查询语句实战演示
Start the start php
xpath
YOLOv6:又快又准的目标检测框架开源啦
[MySQL practice] query statement demonstration
Penetration learning - shooting range chapter - detailed introduction to Pikachu shooting range (under continuous update - currently only the SQL injection part is updated)
Dialogue with Qiao Xinyu: the user is the product manager of Wei brand, and zero anxiety defines luxury
哈希表-数组之和
MONTHS_BETWEEN函数使用
Penetration learning - shooting range chapter -dvwa shooting range detailed introduction (continuous updating - currently only the SQL injection part is updated)
管理系统-ITclub(中)
Gbase 8A OLAP analysis function cume_ Example of dist
[MySQL] database function clearance Tutorial Part 2 (window function topic)
QT base64 encryption and decryption
OpenSSL Programming II: building CA
同花顺炒股软件可靠吗??安全嘛?
Beijing University of Posts and Telecommunications - multi-agent deep reinforcement learning for cost and delay sensitive virtual network function placement and routing
Go from introduction to actual combat - execute only once (note)