koa使用koa-passport实现路由进入前登录验证
现在的项目需求很简单,当进入一个页面的时候,如果没登录,则跳转到登录页面,如果登录了则直接到对应页面。
koa2写的项目,使用koa-passport,koa-session,根据koa-passport的
isAuthenticated()来判断是否登录。 这篇文章写的很好:===》https://segmentfault.com/a/1190000011557953 我的其中一个路由代码;
controllers:
exports.renderUserList = async (ctx, next) => {
if (ctx.isAuthenticated()) {
console.log(ctx.state.user)
console.log(ctx)
let data = await userDao.userList()
await ctx.render('userList', {
title: '员工列表',
csrf: ctx.csrf,
data: data
})
}else {
ctx.redirect('/login')
}
}
router.js:
router.get('/userList', User.renderUserList)
passport.js:
const passport = require('koa-passport')
const User = require('../models/user')
const log4js = require('koa-log4')
const logger = log4js.getLogger('passport')
const LocalStrategy = require('passport-local').Strategy
const md5 = require('md5')
passport.use(new LocalStrategy(
/**
* @param username 用户输入的用户名
* @param password 用户输入的密码
* @param done 验证验证完成后的回调函数,由passport调用
*/
function (username, password, done) {
User.findOne({username: username},function (err,result) {
if (result !== null) {
if (result.password === md5(password)) {
return done(null, doPassword(result),'登录成功')
} else {
return done(null, false, '密码错误')
}
} else {
return done(null, false, '用户不存在')
}
}).catch(function (err) {
logger.error(err.message)
return done(null, false, {message: err.message})
})
}
))
// serializeUser 在用户登录验证成功以后将会把用户的数据存储到 session 中
passport.serializeUser(function (user, done) {
done(null, user)
})
// deserializeUser 在每次请求的时候将从 mongodb 中读取用户对象
passport.deserializeUser(function (id, done) {
console.log(id)
User.findById(id, function (err, user) {
done(err, doPassword(user))
})
// done(null, user)
})
//隐藏密码,相当于是去掉密码的用户信息保存在session里
function doPassword(user) {
if(user) {
user.password = ''
return user
} else {
return null
}
}
module.exports = passport
问题:目前用
isAuthenticated()来判断是否登录只会在单个路由中分别判断,想问下大家有没有办法可以把这个判断是否登录的方法集成成一个方法,然后每个路由去使用。欢迎大家留言!
koa使用koa-passport实现路由进入前登录验证的更多相关文章
- vue路由守卫用于登录验证权限拦截
vue路由守卫用于登录验证权限拦截 vue路由守卫 - 全局(router.beforeEach((to, from, next) =>来判断登录和路由跳转状态) 主要方法: to:进入到哪个路 ...
- vue2.0 实现导航守卫(路由守卫)---登录验证
路由跳转前做一些验证,比如登录验证,是网站中的普遍需求. 对此,vue-route 提供的 beforeRouteUpdate 可以方便地实现导航守卫(navigation-guards). 导航守卫 ...
- koa 基础(三)路由的另一种写法
1.配置路由 app.js // 引入模块 const Koa = require('koa'); const router = require('koa-router')(); /*引入是实例化路由 ...
- 【laravel5.5+Passport】laravel5的前后端分离之Passport设计
项目中使用到了laravel5的passport组件,进行前后端分离的 api认证部分: 前后端分离的api认证,我们用的是: [密码授权令牌],需要用户登录->指定client_id/clie ...
- express+nodecoffee写passport登录验证实例(一)
项目中要用到passport登录验证,环境如标题样:express框架,coffee模版引擎,node后台 一:建项目 直接用express命令建,虽然默认模版为jade,可以手动换成coffee哦. ...
- 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证
原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证 chsakell分享了前端使用AngularJS,后端使用ASP. ...
- 验证控件,解决用于ajax提交前的验证,不是submit提交的验证
//解决ajax提交前的验证问题,主要用于onclick事件时对某一区域中(可以是form,div,table中的等)控件的验证.(function ($) { var v; //Create a n ...
- nodeJS---express4+passport实现用户注册登录验证
网上有很多关于passport模块使用方法的介绍,不过基本上都是基于express3的,本文介绍在express4框架中使用passport模块. 前言 passport是一个功能单一,但非常强大的一 ...
- form的onsubmit事件--表单提交前的验证最佳实现方式
今天遇到了一个问题,页面中include了很多的公共页面(都是没有form的),并且里面好多的地方都是自行提交的(页面中加入一个type=“submit”域,然后js中写入sumbit.click来执 ...
随机推荐
- golang深度获取子节点
起因 需要在树形结构里获取子树,树形结构一般是存储一维数组,数组元素里存储子节点的指针 代码 package main import ( "errors" "fmt&qu ...
- Beaglebone Black教程Beaglebone Black中的Cloud9 IDE基本使用
Beaglebone Black教程Beaglebone Black中的Cloud9 IDE基本使用 Beaglebone Black中的Cloud9 IDE基本使用 Cloud9是集成在Beagl ...
- [Note] Collaborator vs. the Factory
Collaborator-vs-he-Factoryhtml, body {overflow-x: initial !important;}html { font-size: 14px; } body ...
- Linux(Centos7)yum安装最新redis
正如我们所知的那样,Redis是一个开源的.基于BSD许可证的,基于内存的.键值存储NoSQL数据库.Redis经常被视为一个数据结构服务器,因为Redis支持字符串strings.哈希hashes. ...
- hbase 学习(十三)集群间备份原理
集群建备份,它是master/slaves结构式的备份,由master推送,这样更容易跟踪现在备份到哪里了,况且region server是都有自己的WAL 和HLog日志,它就像mysql的主从备份 ...
- 解决IDEA 中git 无法自动push 提交问题 Push failed: Failed with error: Could not read from remote repository.
Push failed: Failed with error: Could not read from remote repository.
- jquery-仿flash的一个导航栏特效
演示地址:http://itxiaoming.sinaapp.com/demo05/demo.html <html> <head> <meta http-equiv=&q ...
- 巧用style的另类写法
看到style,不少人可能会说这个我知道,就是控件写属性的话可以通过style来实现代码的复用,单独把这些属性及其参数写成style就可以便捷的调用. <?xml version="1 ...
- Mac 系统上安装Lua和SubmlimeText 编译器
第一步:安装命令 curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz tar zxf lua-5.2.3.tar.gz cd lua-5.2.3 ma ...
- Mac使用git/github小结
介绍 git 版本控制系统 相比CVS\SVN优势: - 支持离线开发,离线Repository- 强大的分支功能,适合多个独立开发者协作- 速度快 2. github是一个git项目托管网站 注册地 ...