Created
May 12, 2020 07:45
-
-
Save theindianappguy/2d385c0064674f612b84261487bd259f to your computer and use it in GitHub Desktop.
auth.dart file for Chat App Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:chatapp/models/user.dart'; | |
import 'package:chatapp/views/chat.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:google_sign_in/google_sign_in.dart'; | |
class AuthService { | |
final FirebaseAuth _auth = FirebaseAuth.instance; | |
User _userFromFirebaseUser(FirebaseUser user) { | |
return user != null ? User(uid: user.uid) : null; | |
} | |
Future signInWithEmailAndPassword(String email, String password) async { | |
try { | |
AuthResult result = await _auth.signInWithEmailAndPassword( | |
email: email, password: password); | |
FirebaseUser user = result.user; | |
return _userFromFirebaseUser(user); | |
} catch (e) { | |
print(e.toString()); | |
return null; | |
} | |
} | |
Future signUpWithEmailAndPassword(String email, String password) async { | |
try { | |
AuthResult result = await _auth.createUserWithEmailAndPassword( | |
email: email, password: password); | |
FirebaseUser user = result.user; | |
return _userFromFirebaseUser(user); | |
} catch (e) { | |
print(e.toString()); | |
return null; | |
} | |
} | |
Future resetPass(String email) async { | |
try { | |
return await _auth.sendPasswordResetEmail(email: email); | |
} catch (e) { | |
print(e.toString()); | |
return null; | |
} | |
} | |
Future<FirebaseUser> signInWithGoogle(BuildContext context) async { | |
final GoogleSignIn _googleSignIn = new GoogleSignIn(); | |
final GoogleSignInAccount googleSignInAccount = | |
await _googleSignIn.signIn(); | |
final GoogleSignInAuthentication googleSignInAuthentication = | |
await googleSignInAccount.authentication; | |
final AuthCredential credential = GoogleAuthProvider.getCredential( | |
idToken: googleSignInAuthentication.idToken, | |
accessToken: googleSignInAuthentication.accessToken); | |
AuthResult result = await _auth.signInWithCredential(credential); | |
FirebaseUser userDetails = result.user; | |
if (result == null) { | |
} else { | |
Navigator.push(context, MaterialPageRoute(builder: (context) => Chat())); | |
} | |
} | |
Future signOut() async { | |
try { | |
return await _auth.signOut(); | |
} catch (e) { | |
print(e.toString()); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment