loginOrRegister function
Login or register
Creates a user account if it's not existing.
email
is the email of the user.
password
is the password of the user.
photoUrl
is the photo url of the user. If it's null, then it will be the default photo url.
displayName
is the display name of the user. If it's null, then it will be the same as the email.
You can put empty string if you want to save it an empty stirng.
Logic: Try to login with email and password. -> If it's successful, return the user. -> If it fails, create a new user with email and password. -> If a new account is created, then update the user's display name and photo url. -> And return the user. -> If it's failed (to create a new user), throw an error.
final email = "${randomString()}@gmail.com";
final randomUser = await Test.loginOrRegister(
TestUser(
displayName: email,
email: email,
photoUrl: 'https://picsum.photos/id/1/200/200'
),
);
Return the user object of firebase auth and whether the user is registered or not.
Implementation
Future<({fb.User user, bool register})> loginOrRegister({
required String email,
required String password,
String? photoUrl,
String? displayName,
}) async {
fb.UserCredential? cred;
try {
// login
cred = await fb.FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: password,
);
return (user: cred.user!, register: false);
} catch (e) {
// create
cred = await fb.FirebaseAuth.instance
.createUserWithEmailAndPassword(email: email, password: password);
// update display name and photo url
final user = cred.user!;
final userModel = await User.get(user.uid);
if (userModel == null) {
final model = User.fromUid(user.uid);
await model.update(
displayName: displayName ?? email.split('@').first,
photoUrl: photoUrl,
);
}
return (user: user, register: true);
}
}