We trying to implement social sign in with apple in a project which has a .Net Core Web API and a Reactjs SPA. Due to the response type and response mode limitations, from the frontend, we can only obtain an authorization code from apple on a successful sign in.
We can validate this authorization code in the backend with the following logic. Also in the response, it contains id_token where we can access some other info, but not the Email. What we are planning to do is, if the authorization code is valid and a user with the same email exists (email is the username), it authenticates the user and returns Auth JWT Token as per inbuilt authentication/authorization engine which has been implemented using asp.net core Identity framework.
var clientSecret = CreateJwtToken();
var data = new Dictionary<string, string>
{
{ "client_id" , "xxx.xxxxx.com" },
{ "code" , authCode },
{ "scope" , "email" },
{ "grant_type" , "authorization_code" },
{ "redirect_uri", "http://6c52b7b4xx.ngrok.io/login/apple" },
{ "client_secret", clientSecret }
};
var formdata = new FormUrlEncodedContent(data);
var res = await client.PostAsync("https://appleid.apple.com/auth/token", formdata);
var authToken = await res.Content.ReadAsStringAsync();
var responseJSON = JObject.Parse(authToken);
var id_token = responseJSON.GetValue("id_token").ToString();
I'm not sure I'm missing something here. Is there a way that we can access the user's email in the backend? If not, is there a better way of implementing authentication with apple?
Thanks