Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of Firebase Auth with Email and Password on Flutter?

First time implementing Firebase Auth, also new to Flutter dev, and I'm looking to use email and passwords, not Google sign-in. The only examples I see of Firebase Auth being used are anonymously or with Google. Is there a resource / tutorial that shows how to properly set up the calls with the signInWithEmailAndPassword method?

Many thanks!

like image 496
javalow Avatar asked Jun 15 '18 19:06

javalow


People also ask

How do I get Firebase Auth Email from Flutter?

If you haven't already, follow the steps in the Get started guide. Enable Email/Password sign-in: In the Firebase console's Authentication section, open the Sign in method page. From the Sign in method page, enable the Email/password sign-in method and click Save.


1 Answers

In my app I have an auth class because I don't like to litter my widget classes with non-widget code, but you can plop this code inside your widget class if you want.

In auth.dart I have:

import 'package:firebase_auth/firebase_auth.dart';

class Auth {
  final FirebaseAuth auth = FirebaseAuth.instance;

  Future<User> handleSignInEmail(String email, String password) async {
    UserCredential result =
        await auth.signInWithEmailAndPassword(email: email, password: password);
    final User user = result.user!;

    return user;
  }

  Future<User> handleSignUp(email, password) async {
    UserCredential result = await auth.createUserWithEmailAndPassword(
        email: email, password: password);
    final User user = result.user!;

    return user;
  }
}

Then in my login/register screen I create an instance of my auth class:

var authHandler = new Auth();

Finally, in my login buttons onPressed callback I have:

onPressed: () {
    authHandler.handleSignInEmail(emailController.text, passwordController.text)
    .then((FirebaseUser user) {
         Navigator.push(context, new MaterialPageRoute(builder: (context) => new HomePage()));
   }).catchError((e) => print(e));
} 
like image 142
azdisp88 Avatar answered Sep 29 '22 08:09

azdisp88