Pass additonalUserInfo object to User model from FirebaseAuthService class in Flutter - firebase

I am developing a flutter application following this architecture which can be visualised here. There is a requirement that I need to use a different on-boarding flow when the User signs up for the first time apart from the usual flow.
So, after digging around a bit, I found that Firebase provides a bool variable isNewUser in the additionalUserInfo object of the AuthResult class. So, naturally my first thought was to pass this bool into the User model so that I can then verify whether the user has signed up for the first time and pass the corresponding route in the AuthWidget class like this.
class AuthWidget extends StatelessWidget {
const AuthWidget({Key key, #required this.userSnapshot}) : super(key: key);
final AsyncSnapshot<User> userSnapshot;
#override
Widget build(BuildContext context) {
if (userSnapshot.connectionState == ConnectionState.active) {
if (userSnapshot.hasData) {
return userSnapshot.data.isNew ? OnBoardingPage() : RootWidget();
} else {
return SignInPageBuilder();
}
}
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
));
}
}
So while passing the boolean variable into the user model from my FirebaseAuthService class, I ran into some trouble. This is what I thought of doing in the FirebaseAuthService class. I have left comments inside the code for better understanding.
class FirebaseAuthService {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
User _userFromFirebase(FirebaseUser user, AdditionalUserInfo additionalUserInfo) {
if(user == null){
return null;
}
return User(
uid: user.uid,
email: user.email,
photoUrl: user.photoUrl,
displayName: user.displayName,
isNew: additionalUserInfo.isNewUser // Passing this new variable from the AdditionalUserInfo var we passed as an argument
);
}
Stream<User> get onAuthStateChanged {
// This is where the first trouble is. The linter shows me the error :
// The argument type 'User Function(FirebaseUser, AdditionalUserInfo)' can't be assigned to the parameter type 'User Function(FirebaseUser)'.
// So I guess no other arguments can be passed to the _userFromFirebase function
return _firebaseAuth.onAuthStateChanged.map(_userFromFirebase);
}
Future<User> signInUsingGoogle() async {
final GoogleSignIn googleSignIn = GoogleSignIn();
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
idToken: googleSignInAuthentication.idToken,
accessToken: googleSignInAuthentication.accessToken
);
final AuthResult authResult = await _firebaseAuth.signInWithCredential(credential);
// passing the additionalUserInfo variable
return _userFromFirebase(authResult.user, authResult.additionalUserInfo);
}
Future<User> signInUsingFacebook() async {
final FacebookLogin facebookLogin = FacebookLogin();
final FacebookLoginResult facebookLoginResult = await facebookLogin.logIn(['email']);
switch(facebookLoginResult.status) {
case FacebookLoginStatus.cancelledByUser:
print("Cancelled by user");
break;
case FacebookLoginStatus.error:
print("error sigining using facebook");
break;
case FacebookLoginStatus.loggedIn:
print("Logged In with facebook");
break;
}
final accessToken = facebookLoginResult.accessToken.token;
if(facebookLoginResult.status == FacebookLoginStatus.loggedIn) {
final facebookAuthcred = FacebookAuthProvider.getCredential(accessToken: accessToken);
final AuthResult authResult = await _firebaseAuth.signInWithCredential(facebookAuthcred);
// Same as Google login flow
return _userFromFirebase(authResult.user, authResult.additionalUserInfo);
}
return null;
}
Future<User> currentUser() async {
final FirebaseUser user = await _firebaseAuth.currentUser();
// This is the second confusion.
// Since we don't have an AuthResult object, how do I pass that variable.
// I could set the default value for the variable to null/false but I don't think that will be a scalable solution
return _userFromFirebase(user);
}
Future<void> signOut() async {
return _firebaseAuth.signOut();
}
}
Please do suggest if there is a better way to implement this. Thanks!

Related

Google authentication problem (Flutter and Firebase)

I am flutter-newbie and I have one problem: I want to add Google authentication to my flutter app with firebase. This is my code for login screen:
import 'package:PixiCall/resources/firebase_repository.dart';
import 'package:PixiCall/screens/home_screen.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class LoginScreen extends StatefulWidget {
#override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
FirebaseRepository _repository = FirebaseRepository();
#override
Widget build(BuildContext context) {
return Scaffold(
body: loginButton(),
);
}
Widget loginButton() {
return FlatButton(
padding: EdgeInsets.all(35),
child: Text(
'Login',
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.w900,
letterSpacing: 1.2,
),
),
onPressed: () => performLogin,
);
}
void performLogin() {
_repository.signIn().then((User user) {
if (user != null) {
authenticateUser(user);
} else {
print('There was an error');
}
});
}
void authenticateUser(User user, BuildContext context) {
_repository.authenticateUser(user).then((isNewUser) {
if (isNewUser) {
_repository.addDataToDb(user).then((value) {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) {
return HomeScreen();
}));
});
} else {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) {
return HomeScreen();
}));
}
});
}
}
I have this error here:
lib/screens/login_screen.dart:39:25: Error: Too few positional arguments: 2 required, 1 given.
authenticateUser(user);
What is the other parameter which I have to add?
Also I think that I have one more mistake in other file. This is the code from other file:
import 'package:PixiCall/models/user.dart';
import 'package:PixiCall/utils/utilities.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
class FirebaseMethods {
final FirebaseAuth _auth = FirebaseAuth.instance;
GoogleSignIn _googleSignIn = GoogleSignIn();
static final FirebaseFirestore firestore = FirebaseFirestore.instance;
//user class
User1 user = User1();
Future<User> getCurrentUser() async {
User currentUser;
currentUser = await _auth.currentUser;
return currentUser;
}
Future<User> signIn() async {
GoogleSignInAccount _signInAccount = await _googleSignIn.signIn();
GoogleSignInAuthentication _signInAuthentication =
await _signInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: _signInAuthentication.accessToken,
idToken: _signInAuthentication.idToken,
);
User user = await _auth.signInWithCredential(credential);
return user;
}
Future<bool> authenicateUser(User user) async {
QuerySnapshot result = await firestore
.collection('users')
.where('email', isEqualTo: user.email)
.get();
final List<DocumentSnapshot> docs = result.docs;
//if user is registered then length of list > 0 or else less than 0
return docs.length == 0 ? true : false;
}
Future<void> addDataToDb(User currentUser) async {
String username = Utils.getUsername(currentUser.email);
user = User1(
uid: currentUser.uid,
email: currentUser.email,
name: currentUser.displayName,
profilePhoto: currentUser.photoURL,
username: username);
firestore.collection('users').doc(currentUser.uid).set(user.toMap(user));
}
}
This is the mistake in console:
lib/resources/firebase_methods.dart:32:17: Error: A value of type 'UserCredential' can't be assigned to a variable of type 'User'.
Sorry if I confused you, as I said, I am newbie. If you want any other informations please ask here.
For the first mistake , you defined a function void authenticateUser(User user, BuildContext context) so when you use it, it expects 2 arguments a User type object and a BuildContext object
Then you are calling this function in
void performLogin() {
_repository.signIn().then((User user) {
if (user != null) {
authenticateUser(user);
} else {
print('There was an error');
}
});
}
You are passing only the User object, missing the BuildContext
For GoogleSingIn this was my solution:
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
//create user object based on FB json
User _userFromFirebaseUser(FirebaseUser user) {
return user != null
? User(uid: user.uid, emailVerified: user.isEmailVerified)
: null;
}
//auth change user stream
Stream<User> get user {
return _auth.onAuthStateChanged.map(_userFromFirebaseUser);
}
Future<User> singInUsingGoogle() async {
int age;
String email;
String name;
String lastname;
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
idToken: googleAuth.idToken, accessToken: googleAuth.accessToken);
final result = await _auth.signInWithCredential(credential);
FirebaseUser user = result.user;
print(result.user.providerData);
await DatabaseService(uid: user.uid).createUserData(
//user info
)

How to check whether the user email id exists?

I am using the below code for google sign in flutter app with firebase, which is working successfully.
How should I check that whether the email id used is already existing in the firebase authentication?
This I need to ensure that I update the user information in the firestore database accordingly.
Future<User> _handleSignIn() async {
User user;
bool userSignedIn = await _googleSignIn.isSignedIn();
setState(() {
isUserSignedIn = userSignedIn;
});
if (isUserSignedIn) {
user = _auth.currentUser;
}
else {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
user = (await _auth.signInWithCredential(credential)).user;
userSignedIn = await _googleSignIn.isSignedIn();
setState(() {
isUserSignedIn = userSignedIn;
});
}
return user;
}
Please guide me for this
There is a Generic exception related to Firebase Authentication. which is the class FirebaseAuthException. It comes with codes related to errors including email already exists so you would write something like:
try {
//your signIn method here
//i assumed handleSignIn
handleSignIn();
} on FirebaseAuthException catch (e) {
//exception that occurs if e-mail already exists
if (e.code == 'email-already-in-use') {
// the rest of your code here
}
}
Then this is the method you should use I have used to solve my problem
this is my auth.dart file
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:gfd_official/User/User.dart';
import 'package:gfd_official/services/database.dart';
import 'package:google_sign_in/google_sign_in.dart';
String photoUrl;
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
// create user obj based on firebase user
Userdat _userFromFirebaseUser(User user) {
return user != null ? Userdat(uid: user.uid) : null;
}
// auth change user stream
Stream<Userdat> get user {
return _auth
.authStateChanges()
//.map((FirebaseUser user) => _userFromFirebaseUser(user));
.map(_userFromFirebaseUser);
}
Future signInWithGoogle() async {
GoogleSignIn googleSignIn = GoogleSignIn();
final acc = await googleSignIn.signIn();
final auth = await acc.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: auth.accessToken, idToken: auth.idToken);
try {
final res = await _auth.signInWithCredential(credential);
User user = res.user;
photoUrl = user.photoURL;
UserHelper.saveUser(user);
return _userFromFirebaseUser(user);
} catch (e) {
print(e.toString());
return null;
}
}
Future logOut() {
try {
GoogleSignIn().signOut();
return _auth.signOut();
} catch (e) {
print(e.toString());
return null;
}
}
}
class UserHelper {
static FirebaseFirestore _db = FirebaseFirestore.instance;
static saveUser(User user) async {
Map<String, dynamic> userData = {
"name": user.displayName,
"email": user.email,
"role": "basic",
};
try {
final userRef = _db.collection("users").doc(user.uid);
if ((await userRef.get()).exists) {
await userRef.update({});
} else {
await _db
.collection("users")
.doc(user.uid)
.set(userData, SetOptions(merge: true));
}
} catch (e) {
print(e.toString());
}
}
}
In this the save user function will create database for new user and if user already exist then it will let it remain same as it is.
I am creating a document for every user with his/her uid.

Flutter App stuck at splash screen not moving to login page or homepage

I tried to add splash screen to add splash screen to my app but it gets stuck at splash screen itsalf and does not move to next screens
I have added the code here:-
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
void initState() {
super.initState();
getUserInfo();
}
Future getUserInfo() async {
await getUser();
setState(() {});
print(uid);
navigateUser();
}
navigateUser()
{
if(uid!=null && authSignedIn != false)
{
Timer(Duration(seconds: 2),
()=>Navigator.pushReplacementNamed(context, "/toprofilepage")
);
}
else{
Timer(Duration(seconds: 2),
()=>Navigator.pushReplacementNamed(context, "/tologinpage")
);
}
}
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Login',
initial route:'/',
routes: {
'/toprofilepage':(context)=>FirstScreen(),
'/tologinpage':(context)=>LoginPage(),
},
home: Scaffold(
body: Center(
child: Text("Saraswat",style: TextStyle(fontSize: 40,fontWeight: FontWeight.bold,fontStyle: FontStyle.italic),),
),
)
);
}
}
I am getting this following message in console also:-
E/flutter ( 5947): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.
Code for sign in:-
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
bool authSignedIn;
String uid;
String name;
String imageUrl;
Future getUser() async {
// Initialize Firebase
await Firebase.initializeApp();
SharedPreferences prefs = await SharedPreferences.getInstance();
bool authSignedIn = prefs.getBool('auth') ?? false;
final User user = _auth.currentUser;
if (authSignedIn == true) {
if (user != null) {
uid = user.uid;
name = user.displayName;
imageUrl = user.photoURL;
}
}
}
Future<String> signInWithGoogle() async {
// Initialize Firebase
await Firebase.initializeApp();
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final UserCredential userCredential = await _auth.signInWithCredential(credential);
final User user = userCredential.user;
if (user != null) {
// Checking if email and name is null
assert(user.uid != null);
assert(user.displayName != null);
assert(user.photoURL != null);
uid = user.uid;
name = user.displayName;
imageUrl = user.photoURL;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final User currentUser = _auth.currentUser;
assert(user.uid == currentUser.uid);
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('auth', true);
return 'Google sign in successful, User UID: ${user.uid}';
}
return null;
}
void signOutGoogle() async {
await googleSignIn.signOut();
await _auth.signOut();
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('auth', false);
uid = null;
name = null;
imageUrl = null;
print("User signed out of Google account");
}
I tried a lot of things but did not get any solution pls help!.Should I place the navigator function anywhere else or is there some other error pls help!.
Try doing the flow conditions in initialRoute only
Since the firebase has updated the way we check the user is logged in or not. Its not a async task so you can use directly in the MyApp class.
initialRoute: FirebaseAuth.instance.currentUser != null
? HomeScreen.route_name
: AuthScreen.route_name
Or you can use the listener for auth change
FirebaseAuth.instance.onAuthStateChanged.listen((firebaseUser) {
// do whatever you want based on the firebaseUser state
});
so when the auth is changed it will re-direct it to the page you want to, like this
home: StreamBuilder(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (streamContext, userSnapshot) {
if (userSnapshot.connectionState == ConnectionState.waiting)
return SplashScreen();
if (userSnapshot.hasData) {
return HomeScreen();
}
return AuthScreen();
},
),

Firebase auth flutter

For user logged in
Future<String> signInWithGoogle() async {
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final AuthResult authResult = await _auth.signInWithCredential(credential);
final FirebaseUser user = authResult.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
if(authResult.additionalUserInfo.isNewUser == true){
InsertNewUserDetails(user.displayName,user.email,user.photoUrl,user.phoneNumber);
}
if(authResult.user != null){
email = user.email;
name = user.displayName;
imageUrl =user.photoUrl;
globalInstance.isLoggedIn = true;
print(globalInstance.isLoggedIn);
}
return 'signInWithGoogle succeeded: $user';
}
This first time sign in Loginpage checking for is logged account
class MyApp extends StatelessWidget {
#override
String strinbggg;
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal,
body: StreamBuilder<FirebaseUser>(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (BuildContext context, snapshot) {
if (snapshot.hasData && (!snapshot.data.isAnonymous)) {
// return Text('${snapshot.hasData}');
return Homepage();
} else {
// return SafeArea(
// maintainBottomViewPadding: true,
// child: Text('aasdasdfasfdh : ${snapshot.data.email}',style: TextStyle(color: Colors.black54),));
return LoginPage();
}
},
),
),
);
}
}
When first time login with google its working. When i was close application and start again it will redirecting to the homepage fine but user profile details like displayName , email are returning null value
signInwithgoogle() is use for the first time login that store the user details in firestore and user details set from here
MyApp is starting point of application that will check is user is already logged in
I would suggest, you check the user is already signed in or not using the method
bool isSignedIn = await _googleSignIn.isSignedIn();
and if the user is signed in, then get the credentials using
user = await _auth.currentUser();
when you start the app. In case you are not logged in, show login page. Any example would be something like below
final auth = FirebaseAuth.instance;
class MyApp extends StatelessWidget {
#override
String strinbggg;
Widget build(BuildContext context) {
return MaterialApp(
home: (_isSignedIn() == true ? new Homepage() : new LoginPage()));
}
bool _isSignedIn() async {
bool isSignedIn = await _googleSignIn.isSignedIn();
if(isSignedIn){
GoogleSignInAccount user = await auth.currentUser;
//You can store this in your state.
}
return !(user == null && auth.currentUser == null);
}
}
Please note this is a in editor composed code, not tested, you will have to make adjustments.

Check if user exists on FIrestore

I have this function that use SignInWithGogle to Sign In/Up an user
void _signInWithGoogle() async {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;
assert(user.email != null);
assert(user.displayName != null);
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
print('user email here $user.email');
setState(() {
if (user != null) {
_success = true;
_userID = user.uid;
final userToSubmit = User(
email: user.email,
id: user.uid,
name: user.displayName,
owner: false,
favorites: null);
print('USER ID USER ID USER ID ${user.uid}');
DocumentReference dbRef =
Firestore.instance.collection('users').document(user.uid);
dbRef.get().then((value) {
print('VALUE EXISTS? ${value.exists} y VALUE $value');
value.exists ?
_goToHomeScreen(user)
:
dbRef
.collection('prf')
.document('profile')
.setData(userToSubmit.toJson())
.then(_goToHomeScreen(user));
});
} else {
_success = false;
}
});
}
The problem is that when the user logOut and then login it sets the new Data and the user lost everything they have like favorites and so on... I thought that checking if the document exists should resolve the problem, but despite the document is there exists returns false.
This is the piece of code I'm talking about
DocumentReference dbRef =
Firestore.instance.collection('users').document(user.uid);
dbRef.get().then((value) {
print('VALUE EXISTS? ${value.exists} y VALUE $value');
value.exists ?
_goToHomeScreen(user)
:
dbRef
.collection('prf')
.document('profile')
.setData(userToSubmit.toJson())
.then(_goToHomeScreen(user));
});
}
Why it returns false even if the user.uid and the document exists? What am I doing wrong?
The final solution I implemented is this:
FirebaseUser firebaseUser;
try {
firebaseUser = (await FirebaseAuth.instance.signInWithCredential(credential)).user;
} catch(e) {
print(e.toString());
} finally {
final userToSubmit = User(
email: firebaseUser.email,
id: firebaseUser.uid,
name: firebaseUser.displayName,
owner: false,
restauranteAdded: false);
DocumentReference dbRef =
Firestore.instance.collection('users').document(firebaseUser.uid).collection('nameOfTheCollection').document('documentName');
dbRef.get().then((data) {
data.exists ? _goToHomeScreen(user) :
_createUser();
});
This way I check if user document exists in my Firestore Database and if not it creates it.
There is a lot going on. In general, you're not updating any state object with the setState() method. setState is used to tell a widget that some state has changed--and it's called synchronously with the change, but here where you're just checking to see if a document exists and nothing is changing, it's not needed.
Also it doesn't appear that you're actually returning a user object at all, so this code as I read it will always return false.
You should remove setState here altogether (not needed for what you're doing)
Run a sign in method that returns the user object you want do do things with.
Access your db once you know you have a signed in user (i.e. w/ uid of signed in user)
Here is an option to try:
//Create a User class so that you don't have to call FireBase in your methods.
class User {
User({
#required this.uid,
this.email,
this.displayName,
});
final String uid;
final String email;//these are whatever you want
final String displayName;
}
//put sign in methods into an abstract class. Don't have to, but keeps code flexible if you don't stay with firebase
abstract class AuthBase {
//add in other signin methods as needed
Future<User> signInWithGoogle();
}
//Implement AuthBase in class Auth
class Auth implements AuthBase {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
//Get a User from Firebase User
User _userFromFirebase(FirebaseUser user) {
if (user == null) {
return null;
}
return User(
uid: user.uid,
displayName: user.displayName,
email: user.email,
);
}
//your signinWithGoogle method
Future<User> signInWithGoogle() async {
//where User is the class you've created
GoogleSignIn googleSignIn = GoogleSignIn();
GoogleSignInAccount googleUser = await googleSignIn.signIn();
if (googleUser != null) {
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
if (googleAuth.idToken != null && googleAuth.accessToken != null) {
final authResult = await _firebaseAuth.signInWithCredential( //await your instance of FirebaseAuth
GoogleAuthProvider.getCredential(
idToken: googleAuth.idToken,
accessToken: googleAuth.accessToken,
),
);
return _userFromFirebase(authResult.user); //your signed in firebase user that you can do stuff with uid, email, displayname, etc.
} else {
throw PlatformException(
code: 'ERROR_MISSING_GOOGLE_AUTH_TOKEN',
message: '',
);
}
} else {
throw PlatformException(
code: 'ERROR_ABORTED_BY_USER',
message: '',
);
}
}

Resources