Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

could you provide an example how to use it? #11

Open
williamwxz opened this issue Nov 29, 2019 · 3 comments
Open

could you provide an example how to use it? #11

williamwxz opened this issue Nov 29, 2019 · 3 comments

Comments

@williamwxz
Copy link

Maybe just me, could you provide an example how to use it?

For example, do you still need to import firebase_admin, and verifyIdToken(), in every API calls?

@Mohindharan
Copy link

Flutter implementation if anyone needs it

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

import 'package:google_sign_in/google_sign_in.dart';
import 'package:flutter_facebook_login/flutter_facebook_login.dart' ;
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
final FacebookLogin fbLogin = new FacebookLogin();
String name;
String email;
String imageUrl;
String token;

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;

  // Checking if email and name is null
  assert(user.email != null);
  assert(user.displayName != null);
  assert(user.photoUrl != null);
  print(user.displayName);
  name = user.displayName;
  email = user.email;
  imageUrl = user.photoUrl;
 

  // Only taking the first part of the name, i.e., First Name
  if (name.contains(" ")) {
    name = name.substring(0, name.indexOf(" "));
  }

  assert(!user.isAnonymous);
  assert(await user.getIdToken() != null);

  final FirebaseUser currentUser = await _auth.currentUser();
  assert(user.uid == currentUser.uid);
  imageUrl = user.photoUrl;
  currentUser.getIdToken().then((result) {
    token = result.token;
    //save key and call backend for creating
  });
  return 'signInWithGoogle succeeded: $user';
}

void signOutGoogle() async {
  await googleSignIn.signOut();

  print("User Sign Out");
}




Future<String> signInWithfb(BuildContext context) async {
  FirebaseUser currentUser;
  // fbLogin.loginBehavior = FacebookLoginBehavior.webViewOnly;
  // if you remove above comment then facebook login will take username and pasword for login in Webview
  try {
    final FacebookLoginResult facebookLoginResult =
    await fbLogin.logIn(['email', 'public_profile']);
    if (facebookLoginResult.status == FacebookLoginStatus.loggedIn) {
      FacebookAccessToken facebookAccessToken =
          facebookLoginResult.accessToken;
      final AuthCredential credential = FacebookAuthProvider.getCredential(
          accessToken: facebookAccessToken.token);
      final AuthResult authResult = await _auth.signInWithCredential(credential);
      final FirebaseUser user = authResult.user;
      assert(user.email != null);
      assert(user.displayName != null);
      assert(!user.isAnonymous);
      assert(user.photoUrl!= null);
      print(user.displayName);
      name = user.displayName;
      email = user.email;
      imageUrl = user.photoUrl;
      assert(await user.getIdToken() != null);
      user.getIdToken().then((result) {
        token = result.token;
        //save key and call backend for creating
      });
      currentUser = await _auth.currentUser();
      assert(user.uid == currentUser.uid);
      return 'signInWithfb succeeded: $currentUser';
    }
  } catch (e) {
    print(e);
  }
  return 'signInWithfb succeeded: $currentUser';
}

Future<bool> facebookLoginout() async {
  await _auth.signOut();
  await fbLogin.logOut();
  return true;
}

@Mohindharan
Copy link

Flutter implementation if anyone needs it

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

import 'package:google_sign_in/google_sign_in.dart';
import 'package:flutter_facebook_login/flutter_facebook_login.dart' ;
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
final FacebookLogin fbLogin = new FacebookLogin();
String name;
String email;
String imageUrl;
String token;

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;

  // Checking if email and name is null
  assert(user.email != null);
  assert(user.displayName != null);
  assert(user.photoUrl != null);
  print(user.displayName);
  name = user.displayName;
  email = user.email;
  imageUrl = user.photoUrl;
 

  // Only taking the first part of the name, i.e., First Name
  if (name.contains(" ")) {
    name = name.substring(0, name.indexOf(" "));
  }

  assert(!user.isAnonymous);
  assert(await user.getIdToken() != null);

  final FirebaseUser currentUser = await _auth.currentUser();
  assert(user.uid == currentUser.uid);
  imageUrl = user.photoUrl;
  currentUser.getIdToken().then((result) {
    token = result.token;
    //save key and call backend for creating
  });
  return 'signInWithGoogle succeeded: $user';
}

void signOutGoogle() async {
  await googleSignIn.signOut();

  print("User Sign Out");
}




Future<String> signInWithfb(BuildContext context) async {
  FirebaseUser currentUser;
  // fbLogin.loginBehavior = FacebookLoginBehavior.webViewOnly;
  // if you remove above comment then facebook login will take username and pasword for login in Webview
  try {
    final FacebookLoginResult facebookLoginResult =
    await fbLogin.logIn(['email', 'public_profile']);
    if (facebookLoginResult.status == FacebookLoginStatus.loggedIn) {
      FacebookAccessToken facebookAccessToken =
          facebookLoginResult.accessToken;
      final AuthCredential credential = FacebookAuthProvider.getCredential(
          accessToken: facebookAccessToken.token);
      final AuthResult authResult = await _auth.signInWithCredential(credential);
      final FirebaseUser user = authResult.user;
      assert(user.email != null);
      assert(user.displayName != null);
      assert(!user.isAnonymous);
      assert(user.photoUrl!= null);
      print(user.displayName);
      name = user.displayName;
      email = user.email;
      imageUrl = user.photoUrl;
      assert(await user.getIdToken() != null);
      user.getIdToken().then((result) {
        token = result.token;
        //save key and call backend for creating
      });
      currentUser = await _auth.currentUser();
      assert(user.uid == currentUser.uid);
      return 'signInWithfb succeeded: $currentUser';
    }
  } catch (e) {
    print(e);
  }
  return 'signInWithfb succeeded: $currentUser';
}

Future<bool> facebookLoginout() async {
  await _auth.signOut();
  await fbLogin.logOut();
  return true;
}

Needed to brag took me two days to figure this out , Just started with flutter

@yvdjee
Copy link

yvdjee commented Jul 16, 2021

Flutter implementation if anyone needs it

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

import 'package:google_sign_in/google_sign_in.dart';
import 'package:flutter_facebook_login/flutter_facebook_login.dart' ;
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
final FacebookLogin fbLogin = new FacebookLogin();
String name;
String email;
String imageUrl;
String token;

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;

  // Checking if email and name is null
  assert(user.email != null);
  assert(user.displayName != null);
  assert(user.photoUrl != null);
  print(user.displayName);
  name = user.displayName;
  email = user.email;
  imageUrl = user.photoUrl;
 

  // Only taking the first part of the name, i.e., First Name
  if (name.contains(" ")) {
    name = name.substring(0, name.indexOf(" "));
  }

  assert(!user.isAnonymous);
  assert(await user.getIdToken() != null);

  final FirebaseUser currentUser = await _auth.currentUser();
  assert(user.uid == currentUser.uid);
  imageUrl = user.photoUrl;
  currentUser.getIdToken().then((result) {
    token = result.token;
    //save key and call backend for creating
  });
  return 'signInWithGoogle succeeded: $user';
}

void signOutGoogle() async {
  await googleSignIn.signOut();

  print("User Sign Out");
}




Future<String> signInWithfb(BuildContext context) async {
  FirebaseUser currentUser;
  // fbLogin.loginBehavior = FacebookLoginBehavior.webViewOnly;
  // if you remove above comment then facebook login will take username and pasword for login in Webview
  try {
    final FacebookLoginResult facebookLoginResult =
    await fbLogin.logIn(['email', 'public_profile']);
    if (facebookLoginResult.status == FacebookLoginStatus.loggedIn) {
      FacebookAccessToken facebookAccessToken =
          facebookLoginResult.accessToken;
      final AuthCredential credential = FacebookAuthProvider.getCredential(
          accessToken: facebookAccessToken.token);
      final AuthResult authResult = await _auth.signInWithCredential(credential);
      final FirebaseUser user = authResult.user;
      assert(user.email != null);
      assert(user.displayName != null);
      assert(!user.isAnonymous);
      assert(user.photoUrl!= null);
      print(user.displayName);
      name = user.displayName;
      email = user.email;
      imageUrl = user.photoUrl;
      assert(await user.getIdToken() != null);
      user.getIdToken().then((result) {
        token = result.token;
        //save key and call backend for creating
      });
      currentUser = await _auth.currentUser();
      assert(user.uid == currentUser.uid);
      return 'signInWithfb succeeded: $currentUser';
    }
  } catch (e) {
    print(e);
  }
  return 'signInWithfb succeeded: $currentUser';
}

Future<bool> facebookLoginout() async {
  await _auth.signOut();
  await fbLogin.logOut();
  return true;
}

Thanks man, how about login using sms?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants