Last active
June 7, 2022 13:06
-
-
Save Lzolcsi/9c275953c63c94f8240d59d04e54507d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from '@angular/core'; | |
import {environment} from '../../environments/environment'; | |
import {Device} from '@capacitor/device'; | |
import {Capacitor} from '@capacitor/core'; | |
import {SignInWithApple, SignInWithAppleOptions, SignInWithAppleResponse} from '@capacitor-community/apple-sign-in'; | |
type AppleToken = { | |
token: string; | |
device_name: string; | |
name?: string; | |
}; | |
@Injectable({ | |
providedIn: 'root', | |
}) | |
export class AuthService { | |
constructor() {} | |
async signInWithApple(): Promise<AppleToken> { | |
const deviceId = await Device.getId(); | |
return new Promise((resolve, reject) => { | |
if (Capacitor.getPlatform() === 'web') { | |
const appleSignInAbortController = new AbortController(); | |
const appleSignInWindow = window.open( | |
'https://appleid.apple.com/auth/authorize?' + | |
'client_id=' +environment.appId +'.webapp&redirect_uri=' +environment.appUrl + | |
'&response_type=code id_token&scope=name email&response_mode=web_message', | |
'_blank' | |
); | |
window.addEventListener( | |
'message', | |
(e) => { | |
if (!(e instanceof MessageEvent)) { | |
return; | |
} | |
const data = typeof e.data === 'string' ? JSON.parse(e.data) : e.data; | |
if (typeof data !== 'object' || data === null) { | |
return; | |
} | |
if (data.data.hasOwnProperty('error')) { | |
return; | |
} | |
if (!data.data.hasOwnProperty('authorization') || !data.data.authorization.hasOwnProperty('id_token')) { | |
return; | |
} | |
let returnData = { | |
token: data.data.authorization.id_token, | |
device_name: deviceId.uuid, | |
name: null, | |
}; | |
if ( | |
data.data.hasOwnProperty('user') && | |
data.data.user.hasOwnProperty('name') && | |
data.data.user.name.hasOwnProperty('firstName') && | |
data.data.user.name.hasOwnProperty('lastName') | |
) { | |
returnData.name = data.data.user.name.lastName + ' ' + data.data.user.name.firstName; | |
} | |
appleSignInAbortController.abort(); | |
appleSignInWindow.close(); | |
resolve(returnData); | |
}, | |
{signal: appleSignInAbortController.signal} | |
); | |
return; | |
} | |
const options: SignInWithAppleOptions = { | |
clientId: environment.appId + '.webapp', | |
// eslint-disable-next-line @typescript-eslint/naming-convention | |
redirectURI: '', | |
scopes: 'email name', | |
state: '12345', | |
//nonce: 'nonce', | |
}; | |
SignInWithApple.authorize(options) | |
.then((result: SignInWithAppleResponse) => { | |
let returnData = { | |
token: result.response.identityToken, | |
device_name: deviceId.uuid, | |
name: null, | |
}; | |
if ( | |
result.response.hasOwnProperty('givenName') && | |
result.response.hasOwnProperty('familyName') && | |
result.response.givenName && | |
result.response.familyName | |
) { | |
returnData.name = result.response.familyName + ' ' + result.response.givenName; | |
} | |
resolve(returnData); | |
}) | |
.catch(() => { | |
//console.log(error); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment