All Projects → vkcyan → login_push

vkcyan / login_push

Licence: other
vue+koa2+jwt实现单点登录 + todolist增删改查

Programming Languages

javascript
184084 projects - #8 most used programming language
Vue
7211 projects
HTML
75241 projects

Projects that are alternatives of or similar to login push

Smarthome
💡 智能电器管理综合系统
Stars: ✭ 33 (+65%)
Mutual labels:  pm2, axios, koa2, element-ui
Vue Blog
A single-user blog built with vue2, koa2 and mongodb which supports Server-Side Rendering
Stars: ✭ 586 (+2830%)
Mutual labels:  mongoose, axios, vue-router, koa2
Vue Mall Mobile
🔥 vue + koa + mongodb 搭建 mobile web 商城 (End。。。)
Stars: ✭ 201 (+905%)
Mutual labels:  mongoose, axios, vue-router, koa2
element-ui-demo
A element-ui admin base on vue2
Stars: ✭ 18 (-10%)
Mutual labels:  axios, vue-router, element-ui
Nuxt Blog
基于Nuxt.js服务器渲染(SSR)搭建的个人博客系统
Stars: ✭ 277 (+1285%)
Mutual labels:  mongoose, axios, koa2
Vue Element Responsive Demo
基于 Vue + Element 的响应式后台模板
Stars: ✭ 54 (+170%)
Mutual labels:  mongoose, koa2, element-ui
Nuxt Ssr Demo
✨ 高仿掘金,整合 vue + nuxt + axios + vuex + vue-router (nuxt 自带 vuex 和 vue-router),一个基于 Nuxt 的服务器端渲染 Demo
Stars: ✭ 856 (+4180%)
Mutual labels:  pm2, axios, vue-router
Vuesocial
something like QQ、weibo、weChat(vue+express+socket.io仿微博、微信的聊天社交平台)
Stars: ✭ 189 (+845%)
Mutual labels:  mongoose, axios, vue-router
Node React Ecommerce
Build ECommerce Website Like Amazon By React & Node & MongoDB
Stars: ✭ 1,080 (+5300%)
Mutual labels:  mongoose, axios, jsonwebtoken
vue-element-admin
Vuejs实例-Vuejs2.0全家桶结合ELementUI制作后台管理系统http://www.cnblogs.com/weiqinl/p/6873761.html
Stars: ✭ 97 (+385%)
Mutual labels:  axios, vue-router, element-ui
vue-admin-work
🎉🎉🚀🚀🚀🚀vue-admin-work是一个中后台系统管理方案。使用 vue2.x 及周边全家桶工具开发而来。支持多种功能,不同角色权限🚀🚀🚀🎉🎉
Stars: ✭ 74 (+270%)
Mutual labels:  axios, vue-router, element-ui
Vue Koa2 Login
基于 token 的登录注册。
Stars: ✭ 275 (+1275%)
Mutual labels:  mongoose, axios, vue-router
lcddjmwebsite
A website bases on Vue + express.
Stars: ✭ 22 (+10%)
Mutual labels:  mongoose, pm2, element-ui
vue2.0-SellPosSystem
vue2.0实战项目——简单的快餐店系统
Stars: ✭ 35 (+75%)
Mutual labels:  axios, vue-router, element-ui
node-server
(@nestjs refactoring)⚡️My personal website's api server, a RESTful application that powered by @eggjs
Stars: ✭ 17 (-15%)
Mutual labels:  mongoose, jsonwebtoken, koa2
Questionnaire
📋 问卷系统
Stars: ✭ 128 (+540%)
Mutual labels:  mongoose, vue-router, element-ui
Filmsys
一个使用Vue全家桶和后台Express框架结合Mysql数据库搭建起来的移动端电影售票和管理系统,实现了热映、即将上映、电影和影院全局搜索、评论、选座、购票、点赞、收藏、订单等一系列购票和管理流程功能
Stars: ✭ 217 (+985%)
Mutual labels:  axios, vue-router, element-ui
Vue Chat
📲 A web chat application. Vue + node(koa2) + Mysql + socket.io
Stars: ✭ 617 (+2985%)
Mutual labels:  pm2, vue-router, koa2
vue-element-admin-ts
vue-element-admin 的 typescript 版本
Stars: ✭ 101 (+405%)
Mutual labels:  axios, vue-router, element-ui
vue-typescript-admin
Vue typescript 开发的基础模板,多页面支持,基础整合完毕。
Stars: ✭ 26 (+30%)
Mutual labels:  axios, vue-router, element-ui

Vue+Koa2使用jwt实现单点登录🎁


2018-12-17

本次更新README主要是把挂掉的图连接补上

从我个人角度来说,我还是蛮喜欢这样的全栈项目的,实践性很足,不一定要看我的代码,完全可以直接去写一个类似的todo,虽然页面上看起来很简单,但是存在很多知识点

  • jsonwebtoken如何实际运用?
  • 单点登录路由的控制
  • 如何使用koa2搭建出一个让自己满意的后台开发环境
  • 了解mongodb,完成入门,了解大致api的使用
  • 前端Vue React 等等 自己喜欢就好,写的多了,自然会越来越熟练

怎么运行?

需要环境: npm 安装并启动的mongodb (环境有问题是肯定启动不起来的)

git clone https://github.com/vkcyan/login_push.git

// 服务端
cd server
npm install
npm install start

//客户端
cd client
npm install
npm install start

(客户端)client:

  • vue
  • vue-router
  • axios
  • element
  • jsonwebtoken

(服务端)server

  • koa2
  • koa-bodyparser
  • koa-router
  • md5
  • mongoose
  • pm2

实现了什么功能?

  • 登录
  • 注册
  • 注销
  • todolist增加 删除 修改 查看
  • todolist多选操作

jwt验证模式

这里token存储在localStorage里面,还有一种做法是存出来cookice里面,代码可能有点变化,但是效果是一模一样的

login

registry

todolist

关于路由控制的思路

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token') // 获取token

  if (to.name == 'login') { // 假如登录 判断token是不是存在 存在让他跳转到主页面
    verification(token, next)
    .then((data) => {
      if (data.data.type) { // type 为1 直接跳过登录
        Message({
          showClose: true,
          message: '您已经登录,重新登录请退出账户'
        });
        next('/todolist')
      } else {
        next()
      }
    })
  }

  if (to.name == 'todoList') {
    verification(token, next)
    .then((data) => {
      if (data.data.type) {
        // type 为1说明token没有失效
        // 跳转到主页面
        next()
      } else {
        // token失效 强制定位到登录页面
        if (token === null) { // 说明从来没有登陆过
          Message({
            showClose: true,
            message: '您还没有登录',
            type: 'warning'
          })
          next('/login')
        } else {
          Message.error('登录信息失效')
          next('/login')
          localStorage.removeItem('token')
        }
      }
    })
    .catch((data) => {
      console.log(data);
    })
  }

  if (to.meta.title) {
    document.title = to.meta.title
  }
})



// 验证
let verification = (token, next) => {
  return axios.post('/api/verification', { token })
}

后端统一验证接口/api/verification,每次路由跳转都判断token是不是过期或者被改动

没有登录的情况下

当第一次登录 token不存在 则肯定后端相应0 直接通过

没有登录的情况下,去访问登录后的页面,没登录, token分别提示

if (token === null) { // 说明从来没有登陆过
          Message({
            showClose: true,
            message: '您还没有登录',
            type: 'warning'
          })
          next('/login')
        } else {
          Message.error('登录信息失效')
          next('/login')
          localStorage.removeItem('token')
        }

这就又到login type肯定为0, 通过 next()

登录的情况下

token已经存在,type为1,在访问/todoList的情况下,next()

访问/login,type为1,跳转到/todolist,同时给用户一个提示

if (data.data.type) { // type 为1 直接跳过登录
  Message({
    showClose: true,
    message: '您已经登录,重新登录请退出账户'
  });
  next('/todolist')
} else {
  next()
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].