Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Return full name with apple sign in #194

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion ios/Plugin/Handlers/AppleProviderHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class AppleProviderHandler: NSObject, ProviderHandler {

var plugin: CapacitorFirebaseAuth? = nil
var currentNonce: String?
var fullName: PersonNameComponents?

func initialize(plugin: CapacitorFirebaseAuth) {
print("Initializing Google Provider Handler")
print("Initializing Apple Provider Handler")

self.plugin = plugin
}
Expand Down Expand Up @@ -47,6 +48,14 @@ class AppleProviderHandler: NSObject, ProviderHandler {
let appleCredential = credential as! OAuthCredential
jsResult["idToken"] = appleCredential.idToken
jsResult["rawNonce"] = currentNonce
jsResult["fullName"] = [
"namePrefix" : fullName?.namePrefix,
"givenName" : fullName?.givenName,
"middleName" : fullName?.middleName,
"familyName" : fullName?.familyName,
"nameSuffix" : fullName?.nameSuffix,
"nickname" : fullName?.nickname
]

return jsResult
}
Expand Down Expand Up @@ -92,6 +101,9 @@ extension AppleProviderHandler: ASAuthorizationControllerDelegate, ASAuthorizati
// Initialize a Firebase credential.
let credential = OAuthProvider.credential(withProviderID: "apple.com", idToken: idTokenString, rawNonce: nonce)

// save the fullName from the appleIDCredential instance because it is not given in the actual credential See: https://github.com/firebase/firebase-ios-sdk/issues/4393
fullName = appleIDCredential.fullName;

// Sign in with Firebase.
self.plugin?.handleAuthCredentials(credential: credential);
}
Expand Down
9 changes: 9 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,17 @@ export class FacebookSignInResult implements SignInResult {
}
}

export type AppleName = {
namePrefix?: string;
givenName?: string;
middleName?: string;
familyName?: string;
nameSuffix?: string;
};

export class AppleSignInResult implements SignInResult {
providerId = firebase.auth.FacebookAuthProvider.PROVIDER_ID;
fullName?: AppleName;
constructor(public idToken: string, public rawNonce: string, public accessToken: string, public secret: string) {
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/facades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
AppleSignInResult, CapacitorFirebaseAuthPlugin, FacebookSignInResult, GoogleSignInResult,
PhoneSignInResult, SignInOptions, TwitterSignInResult
} from './definitions';
import { AppleName } from '.';

export const CapacitorFirebaseAuth = registerPlugin<CapacitorFirebaseAuthPlugin>('CapacitorFirebaseAuth', {
web: () => import('./web').then(m => new m.CapacitorFirebaseAuthWeb()),
Expand Down Expand Up @@ -135,11 +136,11 @@ export const cfaSignInAppleProvider = 'apple.com';
/**
* Call the Apple sign in method on native and sign in on web layer with retrieved credentials.
*/
export const cfaSignInApple = (): Observable<firebase.User> => {
export const cfaSignInApple = (): Observable<firebase.User & { fullName: AppleName }> => {
return new Observable(observer => {
// native sign in
plugin.signIn<AppleSignInResult>({ providerId: cfaSignInAppleProvider }).then((result: AppleSignInResult) => {
const { idToken, rawNonce } = result;
const { idToken, rawNonce, fullName } = result;

const provider = new firebase.auth.OAuthProvider('apple.com');
provider.addScope('email');
Expand All @@ -153,7 +154,7 @@ export const cfaSignInApple = (): Observable<firebase.User> => {
if(!userCredential.user) {
throw new Error('Firebase User was not received.')
}
observer.next(userCredential.user);
observer.next({...userCredential.user, fullName });
observer.complete();
})
.catch((reject: any) => observer.error(reject));
Expand Down