Updated; Apple sing-in

This commit is contained in:
2023-09-17 12:00:09 +08:00
parent c72679738c
commit 1f4ca7831f
6 changed files with 229 additions and 42 deletions
+83 -10
View File
@@ -7,7 +7,7 @@ import { environment} from "../../../environments/environment";
import { AlertController } from '@ionic/angular';
import { Preferences } from '@capacitor/preferences';
import { Capacitor } from '@capacitor/core';
import { shajs } from 'sha.js';
import jwt_decode from "jwt-decode";
import {
SignInWithApple,
SignInWithAppleResponse,
@@ -108,6 +108,7 @@ getBlogData(){
moveToStartScan(){
this.router.navigate(['startscan']);
}
onLoginApple() {
// -> 'web', 'ios' or 'android'
@@ -143,31 +144,103 @@ getBlogData(){
nonce: nonce,
};
// https://developer.okta.com/blog/2019/06/04/what-the-heck-is-sign-in-with-apple
SignInWithApple.authorize(options)
.then((result: SignInWithAppleResponse) => {
// Handle user information
console.log("response", result.response);
/*
{
"identityToken":"eyJraWQiOiJZdXlYb1kiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiY29tLndyZW5jaGJvYXJkLnVzZXJzIiwiZXhwIjoxNjk0MzkzMzE3LCJpYXQiOjE2OTQzMDY5MTcsInN1YiI6IjAwMTgxMC4yMGU3NTAyOGQ0OWM0MmQ4YjQzMGI0MmQxZDc0ODdmMy4xMTU3Iiwibm9uY2UiOiIxLnB1ZmJwZGFhMzYiLCJjX2hhc2giOiJQRE1PNXNGa2pjOEtPclNNZDRjUGZnIiwiZW1haWwiOiJhY2lkdW1pcmFlQGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjoidHJ1ZSIsImF1dGhfdGltZSI6MTY5NDMwNjkxNywibm9uY2Vfc3VwcG9ydGVkIjp0cnVlLCJyZWFsX3VzZXJfc3RhdHVzIjoyfQ.fEnzaVgB2JaOZbQm2fkpC_J8if5tIIfAphQptZ9Bxp2wrjnDsGvN1b0gvB3KBswv7d6n4A1U46GpaGpShJDhN-e2lKw_lzgFJlRi9-1B-Fudp_gzK61r6W8JzzbPiD0GZgfp_ITPDdxdC7hzixVx4TW8djrV4TIXYjus0b5XnzqRVz2t3ed4Et55s_SZ53SVhM9qnDruVB-KgeGrvDVUks71iM1etuP2vO3xsSFoiKfMEcankX3JUJaCNRSdvVVeUBqH9TfhvmHrCopjnlJ8N2B8o8RDhbnt99OAJtE_dw_Qt5YAAg2ITYQVLBb2dniJ5FBsOBBwbiA0-W1hPJ3RpQ","authorizationCode":"ce0061e5858a34e468a373c3ea7344cdb.0.rryrq.rK6JjdqlVVi6S9coEcTgVQ",
"email":null,
"givenName":null,
"familyName":null,
"user":"001810.20e75028d49c42d8b430b42d1d7487f3.1157"
}
*/
// result.response.user
// result.response.identityToken
// result.response.email: null
// result.response.givenName: null
// result.response.familyName: null
// Validate token with server and create new session
this.showAlert("Error", "Token validation handling is not implemented!");
var decoded = jwt_decode(result.response.identityToken);
// this.sendOauthConnectRequest(OauthProvider.apple, result.response.identityToken).subscribe();
// https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/authenticating_users_with_sign_in_with_apple
console.log(decoded);
/*
{
"iss": "https://appleid.apple.com",
"aud": "com.wrenchboard.users",
"exp": 1694393317,
"iat": 1694306917,
"sub": "001810.20e75028d49c42d8b430b42d1d7487f3.1157",
"nonce": "1.pufbpdaa36",
"c_hash": "PDMO5sFkjc8KOrSMd4cPfg",
"email": "acidumirae@gmail.com",
"email_verified": "true",
"auth_time": 1694306917,
"nonce_supported": true,
"real_user_status": 2
}
*/
// real_user_status => The possible values are: 0 (or Unsupported), 1 (or Unknown), 2 (or LikelyReal).
const oauth2Data = {
"auth_type": "APPLE",
"identityToken": result.response.identityToken,
"email":result.response.email,
"givenName":result.response.givenName,
"familyName":result.response.familyName,
"user":result.response.user,
"authorizationCode":result.response.authorizationCode,
"sub": decoded["sub"],
"jwt_email": decoded["email"],
"email_verified": decoded["email_verified"],
"real_user_status": decoded["real_user_status"]
};
//console.log(oauth2Data);
this.username = decoded["email"] ?? result.response.email;
// Validate token with server and create new session
this.wrenchService.authStart(oauth2Data).subscribe(
loginResult => {
console.log(loginResult);
this.loginResult = loginResult;
console.log("INTERNAL RETURN->" + this.loginResult.internal_return);
/*
localStorage.setItem("member_id", `${loginResult.data.member_id}`);
localStorage.setItem("uid", `${loginResult.data.uid}`);
localStorage.setItem("session_token", `${loginResult.data.session}`);
dispatch(updateUserDetails({...loginResult.data}));
*/
if (loginResult != null && loginResult.internal_return == 100 && this.sessionDataProviderService.ConstructGlobalSessionData(this.loginResult) == true) {
const setName = async () => {
await Preferences.set({
key: 'username',
value: this.username,
});
};
this.getBlogData();
this.router.navigate(['tabs/tab1']);
}
else{
this.showAlert("Error","Invalid username/password");
}
}
);
})
.catch(error => {
// Handle error
.catch((error) => {
this.showAlert("Error", error);
console.log(error);
});
console.log("onLoginApple() out");
}
/*
{"identityToken":"eyJraWQiOiJZdXlYb1kiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiY29tLndyZW5jaGJvYXJkLnVzZXJzIiwiZXhwIjoxNjk0MzkzMzE3LCJpYXQiOjE2OTQzMDY5MTcsInN1YiI6IjAwMTgxMC4yMGU3NTAyOGQ0OWM0MmQ4YjQzMGI0MmQxZDc0ODdmMy4xMTU3Iiwibm9uY2UiOiIxLnB1ZmJwZGFhMzYiLCJjX2hhc2giOiJQRE1PNXNGa2pjOEtPclNNZDRjUGZnIiwiZW1haWwiOiJhY2lkdW1pcmFlQGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjoidHJ1ZSIsImF1dGhfdGltZSI6MTY5NDMwNjkxNywibm9uY2Vfc3VwcG9ydGVkIjp0cnVlLCJyZWFsX3VzZXJfc3RhdHVzIjoyfQ.fEnzaVgB2JaOZbQm2fkpC_J8if5tIIfAphQptZ9Bxp2wrjnDsGvN1b0gvB3KBswv7d6n4A1U46GpaGpShJDhN-e2lKw_lzgFJlRi9-1B-Fudp_gzK61r6W8JzzbPiD0GZgfp_ITPDdxdC7hzixVx4TW8djrV4TIXYjus0b5XnzqRVz2t3ed4Et55s_SZ53SVhM9qnDruVB-KgeGrvDVUks71iM1etuP2vO3xsSFoiKfMEcankX3JUJaCNRSdvVVeUBqH9TfhvmHrCopjnlJ8N2B8o8RDhbnt99OAJtE_dw_Qt5YAAg2ITYQVLBb2dniJ5FBsOBBwbiA0-W1hPJ3RpQ","authorizationCode":"ce0061e5858a34e468a373c3ea7344cdb.0.rryrq.rK6JjdqlVVi6S9coEcTgVQ","email":null,"givenName":null,"familyName":null,
"user":"001810.20e75028d49c42d8b430b42d1d7487f3.1157"}
*/
appleTokenReceived(event: any) {
console.log(event);
this.appleSignInAbortController.abort();
+8 -4
View File
@@ -89,11 +89,15 @@ export class WrenchService {
loginUser(loginData) {
return this.getPostData('userlogin',loginData);
}
//
qrLoginUser(loginData) {
return this.getPostData('qrlogin',loginData);
}
authStart(authData) {
return this.getPostData("authstart", authData);
}
qrLoginUser(loginData) {
return this.getPostData('qrlogin',loginData);
}
getBlogData(blogReq){
//const blogReq=[];
return this.getPostData('blogdata',blogReq);