Sign in with Apple and get email address

I integrated sign in with Apple but probably Apple not return Always email address and user info.

My app needs of email ti complete login flow.

I get jwt token. How can i decoder It and get user informazioni?

Can you help me?

Thanks

JWTs only contain what’s put in them. If you make the JWT, then put the email address in there. If you don’t, you are at the mercy of whoever does.

So I would recommend asking the user for an email address directly from your app, if you really need it.

apple gives you back email, and user info only the first time you try to login.
After that, you must revoke the access to your app, from settings, etc.
This is the procedure:

In most cases, you want to test the process a few times, but after going through the initial dialog once, you won’t see it again, even if you uninstall the app. But here’s a fix to reset your data that allows you to perform everything fresh again: Inside your iOS device, open the settings app and navigate to Apple ID → Password & Security → Apple ID logins. In this screen, you can see all the apps where you used Apple Sign In, and you can simply delete the entry for your own app.

And please, use a real device otherwise it won’t work

Thanks @ciccilleju

I know the procedure and have read that apple returns the email address only the first time but my authentication flow needs the email address all the time. I read that from the returned token (JWT) I can extrapolate the email address.

How can I decode JWT token from app?

Thanks
L

I will give you AN example within two hours

  async generateEmailFromJwt(idToken): Promise<any> {

    let base64Url = idToken.split('.')[1];
    let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
    let jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
      return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));
    const response = JSON.parse(jsonPayload);
    return response;
  }

of course you will need the idToken, which is generated (all the time) when you call the apple sign in service

1 Like

Used this approach:

import { JwtHelperService } from '@auth0/angular-jwt';
  getEmailfromJWT(myRawToken) {
    const helper = new JwtHelperService();
    const decodedToken = helper.decodeToken(myRawToken);
    return decodedToken.email;
  }

It is very strang. Facebook, Google use different scenario. how can i retrieve the user’s name later? Is there any service?

Thanks
L

You can’t, you could save to your db the idToken and link to that token an email address, first name and surname and them., since the id won’t change in the time, you can get those values.

facebook and google always give you back all the infos, apple only the first time, that’s why you should save them to your db the first time on a row

for example
your table AppleAuth contains values like this:
image
and your table User contains
image
and

so, you know that when the email “xxxx@email.com” tries to sign in, with that id token and that userId, it has that name and that surname

1 Like

The scenario is clear but for me is not dinamic. Apple always is different. bha

You need to contact Apple support to make your Apple ID by providing the code you were sent. If you request account deactivation, you’ll receive a unique alphanumeric access code.