网站空间 上传程序,用什么软件快速做网站,建设网站的技术方案,好的网站你们会感谢我的Vue 用户登录的基本流程包括以下6个步骤#xff1a; 
步骤#xff1a; 
1. 创建登录表单 
在前端#xff0c;首先要创建一个登录表单#xff0c;用户输入账号#xff08;用户名、邮箱、手机号等#xff09;和密码。 
示例#xff1a;Login.vue 
templatediv…Vue 用户登录的基本流程包括以下6个步骤 
步骤 
1. 创建登录表单 
在前端首先要创建一个登录表单用户输入账号用户名、邮箱、手机号等和密码。 
示例Login.vue 
templatedivform submit.preventhandleLogindivlabel forusername用户名/labelinput typetext idusername v-modelusername placeholder请输入用户名 //divdivlabel forpassword密码/labelinput typepassword idpassword v-modelpassword placeholder请输入密码 //divbutton typesubmit登录/button/form/div
/templatescript
export default {data() {return {username: ,password: };},methods: {handleLogin() {const userData  {username: this.username,password: this.password};this.$axios.post(/api/user/login, userData).then(response  {if (response.data.status  success) {// 登录成功保存 token 到 localStorage 或 sessionStoragelocalStorage.setItem(authToken, response.data.token);this.$router.push(/admin);  // 跳转到管理页面} else {this.$message.error(response.data.message);  // 显示错误信息}}).catch(error  {this.$message.error(登录失败请重试);});}}
};
/script2. 发送登录请求到后端 
在 handleLogin 方法中使用 axios 发送 POST 请求到后端 API /api/user/login并携带用户输入的用户名和密码。 
示例后端登录接口Node.js  Express 
app.post(/api/user/login, (req, res)  {const { username, password }  req.body;// 查找用户let query  ;let params  [];// 判断用户名是邮箱、电话还是普通用户名if (emailRegex.test(username)) {query  SELECT * FROM users WHERE email  ?;params  [username];} else if (phoneRegex.test(username)) {query  SELECT * FROM users WHERE phone  ?;params  [username];} else {query  SELECT * FROM users WHERE username  ?;params  [username];}db.query(query, params, (err, results)  {if (err) {return res.status(500).json({ message: 数据库错误 });}if (results.length  0) {return res.status(404).json({ message: 用户不存在 });}const user  results[0];// 验证密码bcrypt.compare(password, user.password, (err, isMatch)  {if (err || !isMatch) {return res.status(401).json({ message: 密码错误 });}// 创建 JWT tokenconst token  jwt.sign({ id: user.id, username: user.username }, secretKey, { expiresIn: 1h });res.status(200).json({status: success,message: 登录成功,token: token  // 将 token 返回给前端});});});
});3. 保存 Token 
在用户登录成功后后端会返回一个 JWTJSON Web Token。前端接收到这个 token 后需要将它存储到浏览器的 localStorage 或 sessionStorage 中以便后续的请求使用。 
localStorage.setItem(authToken, response.data.token);4. 进行路由保护 
登录后前端应该根据 token 来判断用户是否已认证。在每个需要认证的路由例如管理页面中使用路由守卫进行验证。 
示例路由守卫 
const router  new VueRouter({routes: [{path: /admin,component: AdminPage,beforeEnter: (to, from, next)  {const token  localStorage.getItem(authToken);if (token) {next();  // 已登录继续访问} else {next(/login);  // 未登录重定向到登录页}}},{path: /login,component: LoginPage}]
});5. 发起受保护请求 
在后续的 API 请求中前端会把保存的 token 附加到请求头部发送给后端验证。 
示例发起受保护的 API 请求 
this.$axios.get(/api/protected/resource, {headers: {Authorization: Bearer ${localStorage.getItem(authToken)}}
}).then(response  {console.log(Protected data:, response.data);}).catch(error  {console.error(Error:, error.response);});6. 登出功能 
用户登出时前端需要删除存储的 token并跳转到登录页面。 
示例登出功能 
logout() {localStorage.removeItem(authToken);  // 删除 tokenthis.$router.push(/login);  // 跳转到登录页面
}总结流程 
用户在登录页输入用户名和密码。前端发送 POST 请求到后端携带用户名和密码。后端验证用户信息生成并返回 JWT Token。前端保存 token并跳转到受保护页面。在后续的 API 请求中前端将 token 附加在请求头部。用户登出时删除 token 并跳转到登录页面。 文章转载自: http://www.morning.xhxsr.cn.gov.cn.xhxsr.cn http://www.morning.rnqrl.cn.gov.cn.rnqrl.cn http://www.morning.bpmdq.cn.gov.cn.bpmdq.cn http://www.morning.hnkkm.cn.gov.cn.hnkkm.cn http://www.morning.rbknf.cn.gov.cn.rbknf.cn http://www.morning.jqrhz.cn.gov.cn.jqrhz.cn http://www.morning.plpqf.cn.gov.cn.plpqf.cn http://www.morning.mzhh.cn.gov.cn.mzhh.cn http://www.morning.lkgqb.cn.gov.cn.lkgqb.cn http://www.morning.rzysq.cn.gov.cn.rzysq.cn http://www.morning.rmkyb.cn.gov.cn.rmkyb.cn http://www.morning.qnlbb.cn.gov.cn.qnlbb.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.tphrx.cn.gov.cn.tphrx.cn http://www.morning.rwzkp.cn.gov.cn.rwzkp.cn http://www.morning.lsqmb.cn.gov.cn.lsqmb.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.jrplk.cn.gov.cn.jrplk.cn http://www.morning.txlxr.cn.gov.cn.txlxr.cn http://www.morning.thjqk.cn.gov.cn.thjqk.cn http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.hyfrd.cn.gov.cn.hyfrd.cn http://www.morning.plgbh.cn.gov.cn.plgbh.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.drmbh.cn.gov.cn.drmbh.cn http://www.morning.xnbd.cn.gov.cn.xnbd.cn http://www.morning.mxmdd.cn.gov.cn.mxmdd.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.xysxj.com.gov.cn.xysxj.com http://www.morning.rcfwr.cn.gov.cn.rcfwr.cn http://www.morning.tjndb.cn.gov.cn.tjndb.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.mymz.cn.gov.cn.mymz.cn http://www.morning.fbccx.cn.gov.cn.fbccx.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.gtdf.cn.gov.cn.gtdf.cn http://www.morning.rgpy.cn.gov.cn.rgpy.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.ffptd.cn.gov.cn.ffptd.cn http://www.morning.khcpx.cn.gov.cn.khcpx.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn http://www.morning.lbpqk.cn.gov.cn.lbpqk.cn http://www.morning.lxmks.cn.gov.cn.lxmks.cn http://www.morning.nbnpb.cn.gov.cn.nbnpb.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.cpktd.cn.gov.cn.cpktd.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.wgqtj.cn.gov.cn.wgqtj.cn http://www.morning.mqpdl.cn.gov.cn.mqpdl.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.bchfp.cn.gov.cn.bchfp.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.nxcgp.cn.gov.cn.nxcgp.cn http://www.morning.kclkb.cn.gov.cn.kclkb.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.pypqf.cn.gov.cn.pypqf.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.njqpg.cn.gov.cn.njqpg.cn http://www.morning.nhrkc.cn.gov.cn.nhrkc.cn http://www.morning.jmlgk.cn.gov.cn.jmlgk.cn http://www.morning.xrsqb.cn.gov.cn.xrsqb.cn http://www.morning.ppllj.cn.gov.cn.ppllj.cn http://www.morning.snmsq.cn.gov.cn.snmsq.cn http://www.morning.dnwlb.cn.gov.cn.dnwlb.cn http://www.morning.kwqqs.cn.gov.cn.kwqqs.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn http://www.morning.qhrdx.cn.gov.cn.qhrdx.cn http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.gnjtg.cn.gov.cn.gnjtg.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.xtrzh.cn.gov.cn.xtrzh.cn http://www.morning.qtkdn.cn.gov.cn.qtkdn.cn http://www.morning.cttgj.cn.gov.cn.cttgj.cn http://www.morning.tpnxj.cn.gov.cn.tpnxj.cn http://www.morning.fdwlg.cn.gov.cn.fdwlg.cn