【问题标题】:How can I implement passport with apple authentication如何通过苹果身份验证实现护照
【发布时间】:2020-10-07 19:49:26
【问题描述】:

我有一个 iOS 应用和 nodeJS 后端。目前我已经实施了护照-脸书策略。我从应用程序中获得了 facebook 令牌,并将其发送到我授权用户的后端。

// config
var FacebookTokenStrategy = require('passport-facebook-token');
const passport = require('passport')
const { facebook_client_id, facebook_client_secret } = require('../config')

passport.use(new FacebookTokenStrategy({
    clientID: facebook_client_id,
    clientSecret: facebook_client_secret,
}, function (accessToken, refreshToken, profile, done) {
    done(null, profile)
}
));

还有中间件

const passport = require('passport')
require('../config/passport-facebook')
require('../config/passport-apple')
require('../config/passport')

const { INVALID_TOKEN, UNAUTHORIZED } = require('../config/constants')

module.exports = (req, res, next) => {
    passport.authenticate(['apple','facebook-token', 'jwt'], function (err, user, info) {
        if (err) {
            if (err.oauthError) {
                res
                    .status(400)
                    .json({ message: INVALID_TOKEN })
            }
        } else if (!user) {
            res
                .status(401)
                .json({ message: UNAUTHORIZED })
        } else {
            req.user = user
            next()

        }
    })(req, res, next);
}

现在我需要实现苹果登录。我尝试使用这个库passport-apple 但我不能让它工作。我正在从应用程序接收令牌,将其发送到后面,但我只得到 ​​p>

GET - /api/v1/shirts/?sorted%5BcreatedAt%5D=-1&filtered%5Bstate%5D=&pageNum=1&pageSize=10 - 302 - Found - 0b sent - 15 ms

我不知道这是否是正确的方法。我应该从应用程序获取用户信息,将其发送到后端并将 JWT 令牌分配给创建的用户吗?或者我怎样才能像使用 facebook 一样做?

【问题讨论】:

    标签: node.js swift oauth-2.0 passport.js


    【解决方案1】:

    由于此文档https://developer.apple.com/documentation/sign_in_with_apple/generate_and_validate_tokens,经过多次尝试,我找到了解决方案

    您需要在您的正文 POST 中发送它:

    {
       "grant_type": "authorization_code",
       "code": "YOUR_CODE",
    }
    

    代码:

    “在发送到您的应用的授权响应中收到的授权码。该代码仅供一次性使用,有效期为五分钟。此参数是授权码验证请求所必需的。”苹果文档

    【讨论】:

      猜你喜欢
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 2016-01-19
      • 2019-06-14
      • 2019-01-01
      • 2018-11-06
      相关资源
      最近更新 更多