nacho
nacho

Reputation: 651

How can I implement passport with apple authentication

I have an iOS app and nodeJS backend. Currently I have implemented passport-facebook strategy. From the app I get the facebook token, and I send it to backend where I authorise the user.

// 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)
}
));

And the middleware

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);
}

Now I need to implement apple login. I tried using this library passport-apple But I can not make it work. I am receiving the token from the app, send it to the back, but I only get

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

I don't know if this is the correct approach. Should I get the user info from the app, send it to the backend and assign a JWT token to the created user? Or how can I do the same as I did with facebook?

Upvotes: 2

Views: 3889

Answers (1)

Kiruel
Kiruel

Reputation: 11

After several try I find the solution thanks to this documentation https://developer.apple.com/documentation/sign_in_with_apple/generate_and_validate_tokens

You need to send that in your body POST:

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

code:

"The authorization code received in an authorization response sent to your app. The code is single-use only and valid for five minutes. This parameter is required for authorization code validation requests." Apple Documentation

Upvotes: 1

Related Questions