首页 > 解决方案 > 在 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);
  };

我现在的流程是:

  1. 在客户中,有人点击使用 Facebook 登录(重定向到localhost:8080/oauth2/facebook
  2. 用户查看 facebook 登录表单并填写
  3. 带有查询参数代码的用户配置文件返回到服务器站点应用程序localhost:8080/oauth2/facebook/callback?Code={somecode}(这是正确的方式吗?)...

现在下一步是什么?我应该对从 ( /ouath2/facebook/callback?CODE={somecodehere}) 获取的 CODE 做什么。我可以处理配置文件将其保存到数据库中,但是如何将令牌返回给客户端?使用重定向和查询参数?我猜它的安全性较低。也许我应该将 facebook 回调 url 设置为localhost:3000/facebook/callback- 但是当我将查询参数中的这段代码传递回服务器站点时,我应该怎么做?

有人可以在 REST 应用程序中给我写一个正确的流程吗?

标签: node.jstypescriptfacebookexpressauthentication

解决方案


推荐阅读