signInWithApple method
Future<AuthResponse<T> >
signInWithApple({
- OAuthAuthenticator? authenticator,
- bool storeToken = false,
- Object? args,
- String? id,
- bool notifiable = true,
Implementation
Future<AuthResponse<T>> signInWithApple({
OAuthAuthenticator? authenticator,
bool storeToken = false,
Object? args,
String? id,
bool notifiable = true,
}) async {
emit(
args: args,
id: id,
notifiable: notifiable,
const AuthResponse.loading(Provider.apple, AuthType.oauth),
);
try {
final hasAnonymous = this.hasAnonymous;
final response = await delegate.signInWithApple();
final raw = response.data;
if (raw == null || raw.credential == null) {
return emit(
args: args,
id: id,
notifiable: notifiable,
AuthResponse.failure(
response.error,
provider: Provider.apple,
type: AuthType.oauth,
),
);
}
final current = await delegate.signInWithCredential(raw.credential!);
if (!current.isSuccessful) {
return emit(
args: args,
id: id,
notifiable: notifiable,
AuthResponse.failure(
current.error,
provider: Provider.apple,
type: AuthType.oauth,
),
);
}
final result = current.data;
if (result == null) {
return emit(
args: args,
id: id,
notifiable: notifiable,
AuthResponse.failure(
msg.authorization,
provider: Provider.apple,
type: AuthType.oauth,
),
);
}
final user = (authenticator ?? Authenticator.oauth()).update(
id: Modifier(result.uid),
accessToken:
storeToken ? Modifier(raw.accessToken) : Modifier.nullable(),
idToken: storeToken ? Modifier(raw.idToken) : Modifier.nullable(),
anonymous: Modifier(raw.isAnonymous ?? result.isAnonymous),
email: Modifier(raw.email ?? result.email),
name: Modifier(raw.displayName ?? result.displayName),
phone: Modifier(raw.phoneNumber ?? result.phoneNumber),
photo: Modifier(raw.photoURL ?? result.photoURL),
provider: Modifier(Provider.apple),
loggedIn: Modifier(true),
loggedInTime: Modifier(EntityHelper.generateTimeMills),
verified: Modifier(true),
);
final value = await _update(
id: user.id,
hasAnonymous: hasAnonymous,
initials: user.filtered,
updates: {
...authenticator?.extra ?? {},
AuthKeys.i.loggedIn: true,
AuthKeys.i.loggedInTime: EntityHelper.generateTimeMills,
AuthKeys.i.anonymous: raw.isAnonymous ?? result.isAnonymous,
AuthKeys.i.email: raw.email ?? result.email,
AuthKeys.i.name: raw.displayName ?? result.displayName,
AuthKeys.i.password: authenticator?.password,
AuthKeys.i.phone: raw.phoneNumber ?? result.phoneNumber,
AuthKeys.i.photo: raw.photoURL ?? result.photoURL,
AuthKeys.i.provider: Provider.apple.id,
AuthKeys.i.username: authenticator?.username,
AuthKeys.i.verified: raw.emailVerified ?? result.emailVerified,
},
);
return emit(
args: args,
id: id,
notifiable: notifiable,
AuthResponse.authenticated(
value,
msg: msg.signInWithApple.done,
provider: Provider.apple,
type: AuthType.oauth,
),
);
} catch (error) {
return emit(
args: args,
id: id,
notifiable: notifiable,
AuthResponse.failure(
msg.signInWithApple.failure ?? error,
provider: Provider.apple,
type: AuthType.oauth,
),
);
}
}