一、登录功能

后端server/routes/users.js

  1. var User = require('./../models/users.js');
  2.  
  3. // 二级路由
  4. // 登录接口
  5. router.post("/login",function(req, res, next){
  6.     // 获取参数
  7.     var param = {
  8.         userName:req.body.userName,
  9.         userPwd:req.body.userPwd
  10.     }
  11.     User.findOne(param, function(err,doc){ // 根据用户名密码查找数据库
  12.         if(err){
  13.             res.json({
  14.                 status:"",
  15.                 msg:err.message
  16.             })
  17.         }else{
  18.             if(doc){
  19.                 res.cookie("userId",doc.userId,{
  20.                     path:'/',
  21.                     maxAge:100*60*60
  22.                 });
  23.                 // res.cookie("userName",doc.userName,{
  24.                 // path:'/',
  25.                 // maxAge:1000*60*60
  26.                 // });
  27.                 // req.session.user = doc;
  28.                 res.json({
  29.                     status:"",
  30.                     msg:'',
  31.                     result:{
  32.                         userName:doc.userName
  33.                     }
  34.                 })
  35.             }
  36.         }
  37.     })
  38. })

添加代理config/index.js

  1. proxyTable: {
  2.     '/users/*':{ // users/路由的下一级路由
  3.         target:'http://localhost:3000'
  4.     }
  5. },
  6.  
  7. 说明:如果是有三级路由,例'/users/cart/del',需要配置'/users/**';否则请求时会出现404错误。

前端NavHeader.vue

  1. methods:{
  2.     login(){ // 点击登录
  3.       console.log("userName:"+this.userName)
  4.       if(!this.userName || !this.userPwd){
  5.         this.errorTip = true;
  6.         return
  7.       }
  8.       axios.post("/users/login",{
  9.         userName:this.userName,
  10.         userPwd:this.userPwd
  11.       }).then((response)=>{
  12.         let res = response.data;
  13.         "){
  14.           this.errorTip = false;
  15.           this.loginModalFlag = false;
  16.           this.nickName = res.result.userName;
  17.         }else{
  18.           this.errorTip = true;
  19.         }
  20.       })
  21.     }
  22. }

二、登出功能

后端server/routes/users.j

  1. / 登出接口
  2. router.post("/logout",function(req,res,next){
  3.     res.cookie("userId","",{
  4.         path:"/",
  5.         maxAge:-1 // 生命周期
  6.     })
  7.     res.json({
  8.         status:"",
  9.         msg:'',
  10.         result:''
  11.     })
  12. })

前端NavHeader.vue

  1. methods:{
  2.     logOut(){ // 点击logout登出
  3.       axios.post("/users/logout").then((response)=>{
  4.         let res = response.data;
  5.         "){
  6.           this.nickName = '';
  7.         }
  8.       })
  9.     }
  10. }

三、登录拦截功能

server/app.js

  1. // 捕获登录状态
  2. app.use(function(req,res,next){ // 进入路由之前优先进入function
  3.     if(req.cookies.userId){ // 有cookies,说明已经登录
  4.         next();
  5.     }else{
  6.         console.log("url:"+req.originalUrl);
  7.         if(req.originalUrl =='/users/login' || req.originalUrl == '/users/logout' || req.originalUrl == '/goods/list'){ // 未登录时可以点击登录login登出logout和查看商品列表
  8.             next();
  9.         }else{
  10.             res.json({
  11.                 status:'1001',
  12.                 msg:'当前未登录',
  13.                 result:''
  14.             })
  15.         }
  16.     }
  17. })

四、校验登录

server/routes/users.js

  1. 登录接口添加userName的cookie
  2. res.cookie("userName",doc.userName,{
  3.     path:'/',
  4.     maxAge:1000*60*60
  5. });
  6.  
  7. // 校验是否登录
  8. router.get("/checkLogin",function(req,res,next){
  9.     if(req.cookies.userId){
  10.         res.json({
  11.             status:'0',
  12.             msg:'',
  13.             result:req.cookies.userName || ''
  14.         });
  15.     }else{
  16.         res.json({
  17.             status:'1',
  18.             msg:'未登录',
  19.             result:''
  20.         })
  21.     }
  22. })

src/components/NavHeader.vue

  1. mounted(){
  2.     this.checkLogin();
  3. },
  4. methods:{
  5.     checkLogin(){ // 检查是否登录
  6.       axios.get("/users/checkLogin").then((response)=>{
  7.         let res = response.data;
  8.         if(res.status == '0'){
  9.           this.nickName = res.result;
  10.         }
  11.       })
  12.     }
  13. }

Vue nodejs商城项目-登录模块的更多相关文章

  1. Vue nodejs商城项目-项目概述

    项目概况 用vue2.0 +node.js +MongonDB 做了一个商城系统 技术选型 Vue2.0+node.js+express+MongoDB+axios+vuex 构建工具 Webpack ...

  2. Vue nodejs商城项目- 前后端数据传递

    .利用Mongoose查询MongoDB 通过mongoose插件可以简捷地从mondodb中获取数据,首先安装mongoose: cnpm install mongoose --save   使用m ...

  3. Vue nodejs商城项目-商品列表价格过滤和加入购物车功能

    一.价格过滤功能 GoodsList.vue >>点击价格区间时发送请求 methods:{     getGoodsList(flag){         var param = {   ...

  4. Vue nodejs商城项目-商品的分页、排序、筛选

    .分页 ,要查第3页的数据,则跳过2*8条数据,然后返回8条数据. 在实现滚动加载时,页面刚一加载完成并不需要请求所有数据,当用户下拉到页面底部时,再去请求数据并拼接到商品数据列表中. 通过vue-i ...

  5. Vue nodejs商城项目-商品列表页面组件

    data(){        return {            goodsList:[], // 商品列表            priceFilter:[ // 价格区间数组          ...

  6. Vue nodejs商城项目-搭建express框架环境

    1.express-project 搭建express框架环境 安装express generator生成器 通过生成器自动创建项目 配置分析 安装 cnpm i -g express-generat ...

  7. iOS不得姐项目--登录模块的布局,设置文本框占位文字颜色,自定义内部控件竖直排列的按钮

    一.登录模块的布局 将一整部分切割成若干部分来完成,如图分成了三部分来完成 设置顶部状态栏为白色的方法 二.设置文本框占位文字颜色 <1>方法一与方法二实现原理是同一种,都是通过设置pla ...

  8. Vue nodejs商城-订单模块

    一.订单列表渲染 新建OrderConfirm.vue订单确认页面,添加路由 src/router/index.js添加路由 import OrderConfirm from '@/views/Ord ...

  9. Vue nodejs商城-地址模块

    一.地址列表渲染 ,则不可以点击. src/views/Cart.vue <a class="btn btn--red" v-bind:class="{'btn-- ...

随机推荐

  1. Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】

    Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB   Total Submit: 623  Accepted: ...

  2. JavaScript对象 继承

    JavaScript继承主要依靠原型链实现. 原型链 利用原型让一个引用类型继承另一个引用类型水位属性和方法. 每一个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指 ...

  3. OpenLayers3 实现自定义放大缩小滑块,自定义方向按钮

    由于项目需要,需要自定义滑块.为此记录如下: <div id="map" class = "map"> <div id = "zoo ...

  4. win10下MySQL 5.7.20解压版安装步骤

    1.从官网下载MySQL5.7.20解压版64位:https://dev.mysql.com/downloads/file/?id=473309. 2.解压(我的解压路径为:E:\mysql-5.7. ...

  5. jQuery基础——选择器、效果

    一.使用JS的痛处 在学习和使用js的过程中发现了js的一些痛处: 1.书写繁琐,代码量大. 2.代码复杂. 3.动画效果很难实现.使用定时器,要小心各种定时器的清除.各种操作和处理事件不好实现. 4 ...

  6. scss-注释

    在scss中有两种注释方式 原生css的注释多行注释: /* *  注释的内容 */ 单行注释:// 注释内容一致延续到行末. 在尽可能的情况下,多行注释会被保留在输出的CSS中,而单行注释会被删除.

  7. 前端(三大框架、Bootstrap,jQuery,自整理)

    前端,HTML(超文本标记语言),CSS(层叠样式表)和JavaScript(脚本语言) HTML,通常说的h5,其实按标准来说,HTML4的后续版本不带编号了,并保证向前的兼容性 CSS的版本3,增 ...

  8. 使用 Python 设置数据的路径

    使用 Python 设置数据的路径 编程语言(如 Python)将反斜线 (\) 用作转义字符.例如,\n 表示换行符,\t 表示制表符.指定路径时,可使用正斜线 (/) 代替反斜线.使用两条反斜线( ...

  9. Prime Numbers in a Grid素数网格

    &/@ Shorthand notation for Map If[PrimeQ[#], Framed@Style[#, Orange, Bold, 15], #] & /@ Rang ...

  10. 2016微软技术大会Azure相关回顾

    3 天的时间稍纵即逝,伴随着本届大会压轴大奖的揭晓,2016 年度的微软技术大会完美落幕.以“数字化转型”为主题,来自微软全球的近百位顶尖技术专家.工程师和业务负责人拔冗而至,在 130 余场的专业技 ...