Skip to Content
Node与Express开发(第2版)
book

Node与Express开发(第2版)

by Ethan Brown
June 2021
Intermediate to advanced
290 pages
9h 21m
Chinese
Posts & Telecom Press
Content preview from Node与Express开发(第2版)
安全
|
215
app.get('/sales', employeeOnly, (req, res) => {
res.render('sales')
})
很明显,你想把基于角色的授权做得多么简单或多么复杂都可以。例如,如果你想授权多
个角色,应该怎么办?可以使用下面的函数和路由:
const allow = roles => (req, res, next) => {
if(req.user && roles.split(',').includes(req.user.role)) return next()
res.redirect(303, '/unauthorized')
}
对于基于角色的授权,你可以充分发挥创造性,希望这个例子能提供一个思路。还可以基
于其他的属性授权,比如说用户成为会员的时长,或者用户在你这里已经订购了多少度假
产品。
18.3.8
 增加认证提供者
既然框架已经搭建好,增加更多的认证提供者就会变得很容易。不妨说我们希望能使用谷
歌来认证。在开始增加新代码之前,需要先在谷歌账号上设置一个项目。
进入谷歌开发者控制台,从导航栏上选择一个项目(如果你还没有项目,点击“
New
Project
”并遵照指示操作)
。选择一个项目之后,点击“
Enable APIs and Services
”(启用
API
和服务)
,并启用
Cloud Identity API
。点击“
Credentials
”(凭据),然后点击“
Create
Credentials
”
(创建凭据),选择“
OAuth ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

面向对象的思考过程(原书第5版)

面向对象的思考过程(原书第5版)

马特 魏斯费尔德
精通模块化JavaScript

精通模块化JavaScript

Nicolás Bevacqua

Publisher Resources

ISBN: 9787115565099