Search code examples
authenticationswiftuiapple-sign-insign-in-with-applesocial-authentication

Implementing SignInWithApple in a native iOS app


I am trying to implement SignInWithApple in a SwiftUI iOS app which sends/receives requests to/from an external API, which in turn, interacts with a MySQL database. I have read through all the documentation provided by Apple and as many articles as I can find on the matter, however I am still unsure what to do.

My question is if I am only implementing this functionality in a native iOS app, once I have received the ASAuthorizationAppleIDCredential, do I need to keep track of the authorizationCode and identityToken, or is it enough to just store the user (unique identifier) value in my database. I already issue my own JWT access/refresh tokens as I also allow users to create an account directly with me (via email and password), so I am unsure what to do with those provided by apple.

Any help would be greatly appreciated!


Solution

  • The pattern you are implementing is similar to when an app uses an authorization server (AS) to manage Sign in with Apple:

    • App runs a code flow to the AS
    • AS runs a second code flow to Apple
    • User signs in with Apple credentials
    • AS receives Apple tokens
    • AS creates an account if required, containing its own unique user identifier
    • AS creates a linked account if required, containing the subject claim from the Apple ID token
    • AS issues its own tokens to the app
    • If required, the storage means the same user can also use other login methods

    In your case you should only need to permanently store the Apple subject claim from the ID token. This could be stored as either a main account ID or a linked account.

    If you find later that you need to hold onto an Apple token, eg for revocation purposes, you can use an embedded token approach. This would involve storing an Apple token as a custom claim within your own JWT.