garyburgmann / drf-firebase-auth

Firebase backend to receive a user idToken and authenticate via Django REST Framework 'authentication.BaseAuthentication'. Optionally, a new local user can be created in the process.
MIT License
128 stars 62 forks source link

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

Open williamwxz opened 5 years ago

williamwxz commented 5 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 3 years ago

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?