当前位置:网站首页>登陆验证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
}
}
})
边栏推荐
- Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
- 第7章 __consumer_offsets topic
- LeetCode 1562. Find the latest group of size M
- Chapter III principles of MapReduce framework
- Record the error reason
- ~81 long table
- Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
- Eureka high availability
- string. How to choose h and string and CString
- Chapter 7__ consumer_ offsets topic
猜你喜欢

LeetCode 1560. The sector with the most passes on the circular track

SQL快速入门

Record the error reason: terminate called after throwing an instance

~82 style of table

我走过最迷的路,是字节跳动程序员的脑回路

7-4 harmonic average

I'm "fixing movies" in ByteDance

~79 Movie card exercise
![Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)](/img/92/9465a6c9f1ab88c4851a47fabe750c.jpg)
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)

100张图训练1小时,照片风格随意变,文末有Demo试玩|SIGGRAPH 2021
随机推荐
Codeforces Global Round 19
Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
我在字节跳动「修电影」
Simple records of business system migration from Oracle to opengauss database
~69 other ways to use icon fonts
~72 horizontal and vertical alignment of text
视频压缩编码和音频压缩编码基本原理
7-6 sum of combinatorial numbers
QT system learning series: 1.2 style sheet sub control lookup
DS18B20数字温度计系统设计
Solve the problem that intel12 generation core CPU single thread only runs on small cores
LeetCode 1560. The sector with the most passes on the circular track
ByteDance new programmer's growth secret: those glittering treasures mentors
Eureka single machine construction
Restful style interface design
LeetCode 1638. Count the number of substrings with only one character difference
Simply try the new amp model of deepfacelab (deepfake)
One hundred questions of image processing (11-20)
Spark independent cluster dynamic online and offline worker node
字节跳动2022校招研发提前批宣讲会,同学们最关心的10个问题