node.js - 在 REST 中使用 Facebook Flow 登录(带有 TypeScript + Passport.js 的 Express.js)- 几个问题
问题描述
我对在 REST 应用程序中使用 facebook 流程登录有几个问题。我使用 Passport.js、Express.js、Typeorm 和 TypeScript。我还没有实现客户端站点。
首先,我创建了 facebook 策略,并在代码中的这个地方获得了配置文件信息,但我想通过使用服务层在控制器中处理配置文件(检查、保存和生成 JWT)。
passport.ts
const facebookStrategyCallback = async(accessToken: any, refreshToken: any, profile: any, done: any) => {
// I can get and process profile here, but i want to do it in controller use only
// service layer to check, save and generate JWT token.
done(null, profile);
};
这是我的带有 facebook 路由和回调路由的控制器:
facebookAuth = (request: Request, response: Response, next: NextFunction) => {
passport.authenticate('facebook')(request, response, next);
};
facebookAuthCallback = (request: Request, response: Response, next: NextFunction) => {
passport.authenticate('facebook', {
session: false,
failureRedirect: '/authenticate/fail'
}, (error, profile) => {
// process profile here using service functions
// But what do next?
})(request, response, next);
};
我现在的流程是:
- 在客户中,有人点击使用 Facebook 登录(重定向到
localhost:8080/oauth2/facebook
) - 用户查看 facebook 登录表单并填写
- 带有查询参数代码的用户配置文件返回到服务器站点应用程序
localhost:8080/oauth2/facebook/callback?Code={somecode}
(这是正确的方式吗?)...
现在下一步是什么?我应该对从 ( /ouath2/facebook/callback?CODE={somecodehere}
) 获取的 CODE 做什么。我可以处理配置文件将其保存到数据库中,但是如何将令牌返回给客户端?使用重定向和查询参数?我猜它的安全性较低。也许我应该将 facebook 回调 url 设置为localhost:3000/facebook/callback
- 但是当我将查询参数中的这段代码传递回服务器站点时,我应该怎么做?
有人可以在 REST 应用程序中给我写一个正确的流程吗?
解决方案
推荐阅读
- python - 我需要在 Python 中做一个 nxn 块来显示该块的“*”
- python - 查找列表中的所有最小值
- javascript - 如何让敌人看起来与英雄画布不同 socket.io
- c++ - Visual Studio 2019 C++ 多行注释
- angular - 分派到状态时,Angular NGRX 出错
- z-index - 当 selectize.js 打开时 Sweetalert2 confirmButton 保持可见 - 独立于 z-index
- python - 打印由 DataGenerator 类发送到 .fit_generator() 函数的一批训练数据
- macos - macOS 上的 OpenSSL 1.0.2m
- microsoft-teams - 如何找到办公室团队中所有成员的电子邮件?
- python - 如何从 tkinter 中的绑定回调中获取回调引用?