Firebase Auth with email for Flutter - email

I'm currently struggling to implement email sign up and log in in my app. I can't use google sign-in or other alternative methods for business reasons.
How to implement Firebase Authentication with Email in Flutter? Is there a way to do it with the Firebase Auth plugin?

This should do what you want:
import 'package:firebase_auth/firebase_auth.dart' show FirebaseAuth;
...
final firebaseUser = await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password);

Related

Is there a way to add providers to an auth existing user? - Supabase - Flutter

With Firebase, I was able to do the following:
final credential = GoogleAuthProvider.credential(idToken: idToken);
//OR OTHER PROVIDERS
final credential = EmailAuthProvider.credential(email: emailAddress, password: password);
//ADD PROVIDER TO CURRENT USER
FirebaseAuth.instance.currentUser?.linkWithCredential(credential);
With Supabase I know these two things; users can be created with OAuth providers and, if the email of that provider user matches an existing user email, the provider is automatically added to that existing user. What I don't know is how to add providers to a user manually.
Example 1:
App requires every user to have an Email & Password
User signs up with phone auth or OAth
User needs to add Email & Password provider to their profile
Example 2:
User signs up with an Email & Password provider
User wants to be able to sign in with OAuth or phone auth
User needs to sign in to OAuth then that provider should be added to the existing Email & Password account
Currently in Supabase email address is the unique identifier of users. If a user that has signed up with email and password also signs up with their Google account using OAuth with the same email address, those two accounts will automatically be merged.

firebase_dart automatically sign in

I am coding an app running on iOS, Android, macOS, Linux and Windows which uses the Firebase Auth and Firebase Realtime Database (Or Firestore Database). Because Windows is not yet supported by the official firebase packages, I am using firebase_dart.
After implementing the sign in, I found out that if I restart the app, I have to sign in again. I think this is because the package does not store the user identity / token / state.
So I need to manually store it but I don't know what to store.
final FirebaseAuth _auth;
...
// These are the credentials I receive from the api call
final credentials = await _auth.signInWithEmailAndPassword(
email: email,
password: password,
);
Does someone know how to stay signed in with this package?
Firebase automatically saves your user session when you sign in the regular way (with email/password or through other credentials), and you can use the userChanges() listener to see if the user is signed in. It will be fired with the user or null depending whether the user has signed in before (and you have no explicitly called signOut().
_auth.userChanges().listen((user) {
debugPrint('user changed $user');
if (user != null) {
// do stuff with user data
} else {
// if you need to handle no user on start up
}
});

best way to keep token for login section in flutter

I wrote the registry system with http library in flutter and its database is mysql
I now want to write it login but I do not know what is the best way
For example, in React, we use Local Storage for storage data
but I do not know what is the best way to store token in the flutter
Before use this add shared_preferences from https://pub.dev/packages/shared_preferences
Try this solution to store your token to local preference.
Save the data in shared-preference
SharedPreferences pref = await SharedPreferences.getInstance();
String token = '54dsfdsfsdfsdf5ad4f5sdafsd25f45';
pref.setString('token', token);
Retrive data from shared-preference
SharedPreferences pref = await SharedPreferences.getInstance();
String token = pref.getString('token')
As mentioned by #Chirag,
shared_preference is a nice dependency to store data like user preferences.
But to store auth token or any other token I would prefer to use flutter_secure_storage so the token is encrypted and secured (it support all platforms)

How to add user details to Firestore DB after email verification in Flutter?

I am trying to add users information like email, name etc. to Firestore DB but after email successful verification. How can I do it..?
Thanks
you should implement Firebase authentication to your app.
the flow should be something like this:
on firebase console go under authentication tab and enable email authentication
in your app request user to register/login with firebase authentication by email
after succesfull authentication your user can now write on firebase db
done
now you have to create a new user in the users "table" with a reference to the authentication user id (not mandatory but nice)

Github OAuth Provider - Get username or id

I'm using the Firebase Authentication services to authenticate users using Github as the provider. The OAuth response returns the access-token for the user along with some other information (e.g. email, name...). For persistence-purposes I need to save the authenticated users username or id, but I can't seem to find that information in the OAuth callback.
Am I missing something or is the user data just not provided using OAuth?
It is not clear which SDK you are using but Firebase auth returns additionalUserInfo form OAuth providers. For example, with the JS SDK, you can get the additionalUserInfo as follows:
firebase.auth().signInWithPopup(new firebase.auth.GithubAuthProvider())
.then(function(result) {
console.log(result.additionalUserInfo.username);
console.log(reuslt.additionalUserInfo.profile);
})
.catch(function(error) {
// Some error occurred.
});