当前位置:网站首页>登陆验证koa-passport中间件的简单使用
登陆验证koa-passport中间件的简单使用
2022-07-06 09:29:00 【社会你磊哥,命硬不弯腰】
简述
koa-passport
是koa
的一个中间件,它实际上只是对passport
的一个封装。利用koa-passport
可以简便的实现登录注册功能,不但包括本地验证,还有很多提供第三方登录的模块可以使用。这篇博客只讲一下登陆验证这块的一些简单内容
配置
安装依赖
npm install -S koa-passport
npm install -S passport-local
引包
import passport from 'koa-passport'
import LocalStrategy from 'passport-local'
import UserModel from '../../dbs/models/users'
配置验证策略
koa-passport默认使用username
和password
做验证
passport.use(new LocalStrategy(async function(username, password, done) {
let where = {
username
};
//在数据库中寻找输入的用户信息
let result = await UserModel.findOne(where)
if (result != null) {
if (result.password === password) {
return done(null, result)
} else {
return done(null, false, '密码错误')
}
} else {
return done(null, false, '用户不存在')
}
}))
序列化
在用户登陆验证成功后将会把用户的数据储存到session
中,在调用 ctx.login()
时会触发序列化操作,把用户对象存到 session
里。
passport.serializeUser(function(user, done) {
done(null, user)
})
反序列化
每次请求将从session
中读取用户对象,session
中如果存在 "passport":{"user":"xxx"}
时会触发定义的反序列化操作
passport.deserializeUser(function(user, done) {
return done(null, user)
})
别忘了export default passport
导出配置好的模块
使用
import Koa from 'koa'
import session from 'koa-generic-session'
import Redis from 'koa-redis'
import passport from './interface/utils/passport'
const app = new Koa()
// session的加密处理
app.keys = ['mt', 'keyskeys']
app.use(session({
key: 'mt',
prefix: 'mt:uid',
store: new Redis()
}))
//initialzie()函数的作用是为上下文添加passport字段, 会在ctx挂载以下方法
//ctx.state.user 认证用户
//ctx.login(user) 登录用户
//ctx.logout() 用户退出登录
//ctx.isAuthenticated() 判断是否认证
app.use(passport.initialize())
//开启koa-passport对session的支持,passport.session()是使passport能够从session中提取用户信息
app.use(passport.session())
登陆组件
router.post('/signin', async(ctx, next) => {
return Passport.authenticate('local', function(err, user, info, status) {
if (err) {
ctx.body = {
code: -1,
msg: err
}
} else {
if (user) {
//user是我们在数据库中找到的数据
ctx.body = {
code: 0,
msg: '登录成功',
user
}
//可以存储用户的session
return ctx.login(user)
} else {
ctx.body = {
code: 1,
//info是在验证里面判断后的信息,密码错误或者用户不存在
msg: info
}
}
}
})(ctx, next)
})
登陆成功后的组件
router.get('/getUser', async(ctx) => {
if (ctx.isAuthenticated()) {
// 策略通过done将user存储到了session中, 并将sessionID写入到客户端的cookie上,
//将用户信息附加到请求对象req.session.passport.user上
const {
username, email } = ctx.session.passport.user
ctx.body = {
user: username,
email
}
} else {
ctx.body = {
user: '',
email: ''
}
}
})
退出登陆的组件
router.get('/exit', async(ctx, next) => {
//logout方法可以删除用户的session,不带参数
await ctx.logout()
if (!ctx.isAuthenticated()) {
ctx.body = {
code: 0
}
} else {
ctx.body = {
code: -1
}
}
})
边栏推荐
- ~81 long table
- ~72 horizontal and vertical alignment of text
- Some instructions on whether to call destructor when QT window closes and application stops
- CMake速成
- 100张图训练1小时,照片风格随意变,文末有Demo试玩|SIGGRAPH 2021
- Mp4 format details
- ~Introduction to form 80
- SQL quick start
- Error occurred during initialization of VM Could not reserve enough space for object heap
- Shell_ 04_ Shell script
猜你喜欢
Which is more important for programming, practice or theory [there are some things recently, I don't have time to write an article, so I'll post an article on hydrology, and I'll fill in later]
图像处理一百题(1-10)
Erlang installation
Shell_ 06_ Judgment and circulation
Gridhome, a static site generator that novices must know
JS encapsulates the method of array inversion -- Feng Hao's blog
Ffmpeg command line use
How to configure hosts when setting up Eureka
Solr new core
字节跳动开源GAN模型压缩框架,算力最高节省97.8%丨ICCV 2021
随机推荐
Jedis
~81 long table
第5章 消费者组详解
Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)
Go language uses the thrift protocol to realize the client and service end reports not enough arguments in call to oprot Writemessagebegin error resolution
~73 other text styles
字节跳动多篇论文入选 CVPR 2021,精选干货都在这里了
第7章 __consumer_offsets topic
音视频开发面试题
Simply try the new amp model of deepfacelab (deepfake)
7-4 harmonic average
Codeforces Round #771 (Div. 2)
7-5 blessing arrived
The concept of spark independent cluster worker and executor
LeetCode 1640. Can I connect to form an array
Solr new core
ByteDance 2022 school recruitment R & D advance approval publicity meeting, students' top 10 issues
第5章 NameNode和SecondaryNameNode
One hundred questions of image processing (11-20)
LeetCode 1638. Count the number of substrings with only one character difference