Setting up Sign in with Apple

Hi everyone!

I have a couple of questions about the server setup on how to support Sign in with Apple

First I’m going to show you my server’s setup;

var api = new ParseServer({
    auth: {
        'apple': {
            'id': 'user',
            'token': 'the identity token for the user'
         }
    }
});

What do I have to put to id and token ?

This is how I’m trying to login from my iOS app;

var authData = [String : String]()
    
authData["access_token"] = String(decoding: appleIDCredential.authorizationCode!, as: UTF8.self)
authData["expiration_date"] = Date().description
    
// -----------------------------------------
    
PFUser.logInWithAuthType(inBackground: "apple", authData: authData).continueWith(block: { (task) -> Any? in
    if task.result != nil {
        if PFUser.current() != nil {
            // present home screen
        }
    }
        
    return nil
})

And this is how I’m trying to sign up;

var authData = [String : String]()
    
let userId = appleIDCredential?.user ?? passwordCredential!.user
    
if let appleIDCredential = appleIDCredential {
    authData["token"] = String(data: appleIDCredential.identityToken!, encoding: .utf8)
    authData["id"] = userId
}
    
// -----------------------------------------
    
let userEmail = appleIDCredential?.email
let name: String
    
if let givenName = appleIDCredential?.fullName?.givenName, let familyName = appleIDCredential?.fullName?.familyName {
    name = ("\(givenName) \(familyName)")
}
else {
    name = passwordCredential!.user
}
    
PFUser.logInWithAuthType(inBackground: "apple", authData: authData).continueWith(block: { (task) -> Any? in
    if task.result != nil {
        if PFUser.current() != nil {
            PFUser.current()?["username"] = name
            PFUser.current()?.saveInBackground(block: nil)
        }
        
        return nil
    })
}

I’ve also added this on AppDelegate ;

PFUser.register(AuthDelegate(), forAuthType: "facebook")
PFUser.register(AuthDelegate(), forAuthType: "apple")

Lastly, as far as Apple’s concerned I’ve added the Sign in with Apple capability on Xcode.

I’ve managed to get all the authentication data I need and create the user, but I fail to login!
Any help will be much appreciated!!!

Hey there @CastAsHuman !
Inside the apple block, you should have the ID as your bundle ID. Mine is like this:

"auth": {
    "apple": {
      "client_id": "com.back4app.app"
    }
}

Your logInWIthAuthType seems correct, but mine is slightly different:

PFUser.logInWithAuthType(inBackground: "apple", authData: ["token":tokenString, "id": user]).continueWith { task -> Any? in
    if ((task.error) != nil){
        print("ERROR: \(task.error?.localizedDescription)")
    }
    return task

    if let userObject = task.result {
        print("LOGGED IN PARSE")
    } else {
        // Failed to log in.
        print("ERROR LOGGING IN IN PARSE: \(task.error?.localizedDescription)")
    }
    return nil
}

So I’m guessing the only part keeping your code from working is the ParseServer setup.

1 Like

Do I need to do anything to the appstore Connect account?

There are a few steps you should do in the Appstore Connect.
This tutorial is a bit outdated as we work on a new one, but it should give you an idea of the steps you should check if you did:

Sign In with Apple Tutorial

All steps 3, 4 & 5 are necessary for an iOS app, or for a web app?

It used to be necessary for domain association but it was brought to my attention that Apple is not providing the domain association file (txt file) anymore, so the process has changed.

1 Like

This is actually deprecated in the newest version of Parse Server. The correct key name is clientId, see this issue.

That’s why I said it’s outdated :blush:

I am not referring to any 3rd party tutorial, I am referring to the code for Parse Server that was mentioned above.

1 Like