login method
Validates a Firebase ID token and either logs in the associated user or creates a new user account if the Firebase account ID is not yet known.
If a new user is created an associated UserProfile is also created.
Implementation
Future<AuthSuccess> login(
final Session session, {
required final String idToken,
final Transaction? transaction,
}) async {
return await DatabaseUtil.runInTransactionOrSavepoint(
session.db,
transaction,
(final transaction) async {
final account = await utils.authenticate(
session,
idToken: idToken,
transaction: transaction,
);
final image = account.details.image;
if (account.newAccount) {
try {
await _userProfiles.createUserProfile(
session,
account.authUserId,
UserProfileData(
fullName: account.details.fullName?.trim(),
email: account.details.email,
),
transaction: transaction,
imageSource: image != null ? UserImageFromUrl(image) : null,
);
} catch (e, stackTrace) {
session.log(
'Failed to create user profile for new Firebase user.',
level: LogLevel.error,
exception: e,
stackTrace: stackTrace,
);
}
} else if (image != null) {
try {
final user = await UserProfile.db.findFirstRow(
session,
where: (final t) => t.authUserId.equals(account.authUserId),
transaction: transaction,
);
if (user != null && user.image == null) {
await _userProfiles.setUserImageFromUrl(
session,
account.authUserId,
image,
transaction: transaction,
);
}
} catch (e, stackTrace) {
session.log(
'Failed to update user profile image for existing Firebase user.',
level: LogLevel.error,
exception: e,
stackTrace: stackTrace,
);
}
}
return _tokenIssuer.issueToken(
session,
authUserId: account.authUserId,
transaction: transaction,
method: method,
scopes: account.scopes,
);
},
);
}