Manage social user details in Google Flutter application

3,326

Solution 1

By using the built-in sign in using Firebase Authentication SDK (Official Guide), you will be able to obtain login information for different platforms like Google, Twitter, Facebook and even Github.

You do not need to code your own login/signup, everything is handle hassle free by using the right function from Firebase.

After that you will have the login information like so (this is from email login but Facebook, Google, Twitter and Github is available):

enter image description here

You can then link either the identifier or the User UID to the information in your database.

enter image description here

Oh and last but not least, do remember to set the persistence setting for the login provider to keep user login after they close their apps.

Solution 2

Your proposed solution is good enough. The returned id is unique for your app.

Use the id returned to your app as the identifier for users in your app.

Checkout this answer on unique id

Just leaving the social_login plugin here

// Import package
import 'package:social_login/social_login.dart';

// Instantiate it
 final socialLogin = SocialLogin();

//Before calling any methods, set the configuration
socialLogin.setConfig(SocialConfig(
      facebookAppId: FACEBOOK_APP_ID,
      googleWebClientId: GOOGLE_WEB_CLIENT_ID, /*In case a Google tokenId is needed*/
      twitterConsumer: TWITTER_CONSUMER_KEY,
      twitterSecret: TWITTER_CONSUMER_SECRET,
    ));

// Get current logged user
 final FacebookUser facebookUser = await socialLogin.getCurrentFacebookUser();
 final GoogleUser googleUser = await socialLogin.getCurrentGoogleUser();
 final TwitterUser twitterUser = await socialLogin.getCurrentTwitterUser();

//Log in social networks
 final FacebookUser facebookUser = await socialLogin.logInFacebookWithPermissions(FacebookPermissions.DEFAULT);
 final GoogleUser googleUser = await socialLogin.logInGoogle();
 final TwitterUser twitterUser = await socialLogin.logInTwitter();

//Log out from social networks
 await socialLogin.logOutFacebook();
 await socialLogin.logOutGoogle();
 await socialLogin.logOutTwitter();

Solution 3

You should split Flutter app into 2 parts:

  1. The "main" part of the app. It shows user related data.
  2. Sign in/Sign up part.

The logic is quite simple. On the app start you check if the user is authenticated. If yes, navigate him to the main screen. If not, present him a sign up screen. Here's how it can be achieved using Firebase as a backend:

final currentUser = await FirebaseAuth.instance.currentUser();
if (currentUser != null){
  // We're good: the user is authenticated.
  // Show him the main screen.
  Navigator.of(context).pushReplacement(MaterialPageRoute(
    builder: (context) => HomeScreen()
  ));
} else {
  // The user is not logged in.
  // Show him the sign in/sign up screen.
  Navigator.of(context).pushReplacement(MaterialPageRoute(
    builder: (context) => SignInScreen()
  ));
}

Solution 4

Maybe this example meet your needs:

https://github.com/instaflutter/flutter-login-screen-firebase-auth-facebook-login

If you need some help to understand, i can help you!

Solution 5

you should go with the token logic (which is used by google and facebook).

you should generate a token to each user and store it in your data base . through this token you can mange every thing about the user . when you call facebook authtinicating it will give you back a token for specific user . this token when you send it again to facebook or google they will respond back with the details of the user . so you can work like them .. create a token for the user and recall it for the info from your data base .. and you can store the token responded from facebook and match it in the future .. the purpose of my answer to go and search for token concept cause it will give you a lot of benefits .

Share:
3,326
Ragesh S
Author by

Ragesh S

Welcome, my name is Ragesh P R. I am a software developer from Cochin India who loves to write software to build great products and help businesses succeed with their goals. I really encourage good design and I am seeing its importance more than ever in today's apps, websites, and products. I have been working professionally in software development since 2010 in the web and mobile spaces with experience across various domains. Skype : ragesh.sl

Updated on November 29, 2022

Comments

  • Ragesh S
    Ragesh S over 1 year

    I am a beginner in developing Flutter application, and try to create a sample application for educational purpose. Last few weeks I decided to do a sample application in flutter that have no inbuilt login or register section because it's have social login options like Facebook and Google. I searched the web and got many code examples for how to implement Facebook and Google authentication in Flutter application.

    I have a doubt about this social login implementation maybe it's happening because of lack of working experience in mobile application architecture level. At this point I am asking a question to myself "How to manage social login users in applications"

    I have a solution like if the user can login ed successfully, store the user details into our (Firebase) db check the user email exists in database if there is no matching entries in db it will create a new user in database or if exists update the last login date time of that particular user. Because this application shows some user related data in other forms, it's work on the basis of logined userid, so I need to store the logined user details in the database.