SlideShare a Scribd company logo
1 of 28
Advance Mobile Application
Development
Firebase Auth
Dr. Mazin Alkathiri
IT Department
Seiyun University
2023-2024
Firebase Authentication
• Most apps need to know the identity of a user. Knowing a user's
identity allows an app to securely save user data in the cloud and
provide the same personalized experience across all of the user's
devices.
• Firebase Authentication provides backend services, easy-to-use SDKs,
and ready-made UI libraries to authenticate users to your app.
• It supports authentication using passwords, phone numbers, popular
federated identity providers like Google, Facebook and Twitter, and
more.
Why firebase Auth
• Being able to authenticate our users securely, it offers a customized
experience to them based on their interests and preferences.
• We can ensure that they have no problems accessing their private
data while using our app from multiple devices.
• Firebase Authentication provides all the server-side stuff for
authenticating the user. Firebase Authentication becomes easy with
SDK. It makes API easy to use.
• Firebase Authentication also provides some user interface libraries
which enable screens for us when we are logging it.
Cont.
• Firebase authentication supports authentication using a password, phone
numbers, popular identity provider like Google, Facebook, and Twitter, etc.
• We can sign in users to our app by using the FirebaseUI.
• It handles the UI flows for signing in user with an email address and password, phone
numbers, and popular providers, including Google sign-In and Facebook Login.
• It can also handle cases like account recovery.
• It is not required to design a UI since it is already provided for us. It means we don't
have to write the activities.
• We can also sign-in users using the Firebase Authentication SDK to
integrate one or several sign-in methods into our app manually.
How Authentication Works?
• We first get authentication credentials from the user to sign a user into our app.
• Credentials can be the user's email address and password.
• The credential can be an OAuth token from an identity provider.
• We then pass these credentials to the Firebase Authentication SDK. Backend
services will then verify those credentials and return a response to the client.
• After a successful sign in:
• We can access the user's access to data stored in other Firebase products.
• We can access the user's basic profile information.
• We can use the provided authentication token to verify the identity of users in our own
backend services.
• An authenticated user can read and write data to the Firebase Real-time
Database and Cloud Storage.
• We can control the access of those authenticated users by modifying the Firebase Database
Rules and Storage Security Rules.
Users
• A Firebase User object represents the account of a user who has signed up to the app in Firebase
project. Apps have many registered users, and every app in a Firebase project shares a user data
base.
• AFirebase User instance is independent of a Firebase Auth instance. It means we can have
several references to different users within the same context and still call any of their method
• A Firebase User has a fixed set of basic properties such as Unique ID, Primary email address,
Name, and a photo URL.
• Firstly, a user signs up to the app. The user's profile data is populated with the primary email
address if using email/password auth, account information available by the provider if using
identity auth, and anything we want if using custom auth.
• The user becomes the current user of the Auth instance when a user signs up or signs in.
• The Auth instance stops to keep a reference to the User object. And no longer persists it states
when a user signs out:
• No current user
• The user instance continues to be completely functional
• If we keep a reference to it, we can still access and update the user's data.
• Using listeners is the recommended way to track the current state of the Auth instance.
• An Auth listener gets notified any time when something relevant happens to the Auth object.
User Lifecycle
• An Auth listener gets notified in the following situation
• The Auth object finishes initializing, and a user was already signed in
from a previous session or has been redirected from an identity
provider's sign-in flow
• A user signs in.
• A user signs out.
• The current user's access token is refreshed:
• The access token expires.
• The user changes their password.
• The user re-authenticates
Firebase: Google Sign-In Authentication
• As we have discussed earlier, Firebase provides different types of Authentication
methods. In our previous section, we learned how authentication is done using
Firebase UI and Firebase SDK. In this section, we will learn another method, i.e.,
Google Sign-in Authentication. It is pretty easy to do.
• Starting steps are the same as we have done with other authentication methods,
which are as follows:
• Creating an Android project.
• Creating a Firebase project.
• Adding Firebase to the Android project or application, either manually or Firebase
Assistance.
• Adding the required libraries and JSON files.
• Command to get debug SHA-1 (for Windows) where password is Android
keytool -list -v -alias androiddebugkey -keystore C:Usershp.androiddebug.keystore
Coding part
• Starting by Adding the dependencies to pubspec.yaml file under
dependency section.run the following cmd for add latest version
dependencies.
• flutter pub add firebase_auth
• flutter pub add google_sign_in
Building the login UI
• Creating a login.dart
• With the class GoogleSignInScreen with the help of ValueListenableBuilder
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:tryingfirebase/home.dart';
• t is an amazing widget. It builds the widget every time the valueListenable
value changes. Its values remain synced with there listeners i.e. whenever
the values change the ValueListenable listen to it. It updates the UI without
using setState() or any other state management technique.
• Properties:
• valueListenable:
• builder:
• child:
• These are the three properties of ValueListenableBuilder. bulder build
widget depending upon the valueListenable value. valueListenable is an
instance of ValueNotifier . child property is optional, it can be null if
valueListenable value entirely depends upon the builder widget.
body: ValueListenableBuilder(
valueListenable: userCredential,
builder: (context, value, child) {
return (userCredential.value == '' ||
userCredential.value == null)
? Center(
child: Card(
…..……………
),
)
: Center(
child: Column(
……………………
),
);
})
? Center(
child: Card(
elevation: 5,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
child: IconButton(
iconSize: 1,
icon: Image.asset('assets/images/google_icon.png',),
onPressed: () async {
userCredential.value = await signInWithGoogle();
if (userCredential.value != null)
print(userCredential.value.user!.email);
Navigator.push(context, MaterialPageRoute(
builder: (context) => HomeMenu(userCredential),
), ); }, ), ), )
children: [
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
width: 1.5, color: Colors.black54)),
child: Image.network(userCredential.value.user!.photoURL.toString()), ),
const SizedBox(height: 20),
Text(userCredential.value.user!.displayName.toString()),
const SizedBox(height: 20),
Text(userCredential.value.user!.email.toString()),
const SizedBox(height: 30),
ElevatedButton(
onPressed: () async {
bool result = await signOutFromGoogle();
if (result) userCredential.value = '';
},
child: const Text('Logout'))
],
Future<dynamic> signInWithGoogle() async {
try {
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
return await FirebaseAuth.instance.signInWithCredential(credential);
} on Exception catch (e) {
// TODO
print('exception->$e');
}
}
Future<bool> signOutFromGoogle() async {
try {
await FirebaseAuth.instance.signOut();
return true;
} on Exception catch (_) {
return false;
}
}
}
Change the rules in Firestore Database
Change the rules in firebase Storage

More Related Content

Similar to Advance Mobile Application Development class 07

Similar to Advance Mobile Application Development class 07 (20)

Firebase .pptx
Firebase .pptxFirebase .pptx
Firebase .pptx
 
React Native Firebase Realtime Database + Authentication
React Native Firebase Realtime Database + AuthenticationReact Native Firebase Realtime Database + Authentication
React Native Firebase Realtime Database + Authentication
 
Amazon Cognito Deep Dive
Amazon Cognito Deep DiveAmazon Cognito Deep Dive
Amazon Cognito Deep Dive
 
Five Things You Didn't Know About Firebase Auth
Five Things You Didn't Know About Firebase AuthFive Things You Didn't Know About Firebase Auth
Five Things You Didn't Know About Firebase Auth
 
Full Angular 7 Firebase Authentication System
Full Angular 7 Firebase Authentication SystemFull Angular 7 Firebase Authentication System
Full Angular 7 Firebase Authentication System
 
Delivering Mobile Apps Using AWS Mobile Services
Delivering Mobile Apps Using AWS Mobile ServicesDelivering Mobile Apps Using AWS Mobile Services
Delivering Mobile Apps Using AWS Mobile Services
 
Discover Google Firebase Platform
Discover Google Firebase PlatformDiscover Google Firebase Platform
Discover Google Firebase Platform
 
Token Management using Stormpath inside Mule
Token Management using Stormpath inside MuleToken Management using Stormpath inside Mule
Token Management using Stormpath inside Mule
 
ACDKOCHI19 - Enterprise grade security for web and mobile applications on AWS
ACDKOCHI19 - Enterprise grade security for web and mobile applications on AWSACDKOCHI19 - Enterprise grade security for web and mobile applications on AWS
ACDKOCHI19 - Enterprise grade security for web and mobile applications on AWS
 
Deep Dive on User Sign-up Sign-in with Amazon Cognito - AWS Online Tech Talks
Deep Dive on User Sign-up Sign-in with Amazon Cognito - AWS Online Tech TalksDeep Dive on User Sign-up Sign-in with Amazon Cognito - AWS Online Tech Talks
Deep Dive on User Sign-up Sign-in with Amazon Cognito - AWS Online Tech Talks
 
7 Deadly Sins in Azure AD App Development
7 Deadly Sins in Azure AD App Development7 Deadly Sins in Azure AD App Development
7 Deadly Sins in Azure AD App Development
 
Managing Identity and Securing Your Mobile and Web Applications with Amazon C...
Managing Identity and Securing Your Mobile and Web Applications with Amazon C...Managing Identity and Securing Your Mobile and Web Applications with Amazon C...
Managing Identity and Securing Your Mobile and Web Applications with Amazon C...
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & Databases
 
Raleigh DevDay 2017: Managing User Onboarding, Sign-up, Sign-in, Identity and...
Raleigh DevDay 2017: Managing User Onboarding, Sign-up, Sign-in, Identity and...Raleigh DevDay 2017: Managing User Onboarding, Sign-up, Sign-in, Identity and...
Raleigh DevDay 2017: Managing User Onboarding, Sign-up, Sign-in, Identity and...
 
Securing SharePoint Apps with OAuth
Securing SharePoint Apps with OAuthSecuring SharePoint Apps with OAuth
Securing SharePoint Apps with OAuth
 
Azure AD B2C Webinar Series: Custom Policies Part 1
Azure AD B2C Webinar Series: Custom Policies Part 1Azure AD B2C Webinar Series: Custom Policies Part 1
Azure AD B2C Webinar Series: Custom Policies Part 1
 
Creating a Sign On with Open id connect
Creating a Sign On with Open id connectCreating a Sign On with Open id connect
Creating a Sign On with Open id connect
 
Deep Dive on Amazon Cognito - DevDay Los Angeles 2017
Deep Dive on Amazon Cognito - DevDay Los Angeles 2017Deep Dive on Amazon Cognito - DevDay Los Angeles 2017
Deep Dive on Amazon Cognito - DevDay Los Angeles 2017
 
Microsoft Azure Identity and O365
Microsoft Azure Identity and O365Microsoft Azure Identity and O365
Microsoft Azure Identity and O365
 
Microsoft Teams community call - February 2020
Microsoft Teams community call - February 2020Microsoft Teams community call - February 2020
Microsoft Teams community call - February 2020
 

More from Dr. Mazin Mohamed alkathiri

More from Dr. Mazin Mohamed alkathiri (20)

OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Advance Mobile Application Development class 05
Advance Mobile Application Development class 05Advance Mobile Application Development class 05
Advance Mobile Application Development class 05
 
ESSENTIAL of (CS/IT/IS) class 05 (Software concepts)
ESSENTIAL of (CS/IT/IS) class 05 (Software concepts)ESSENTIAL of (CS/IT/IS) class 05 (Software concepts)
ESSENTIAL of (CS/IT/IS) class 05 (Software concepts)
 
OS-operating systems- ch03
OS-operating systems- ch03OS-operating systems- ch03
OS-operating systems- ch03
 
OS-operating systems - ch02 - part-2-2024
OS-operating systems - ch02 - part-2-2024OS-operating systems - ch02 - part-2-2024
OS-operating systems - ch02 - part-2-2024
 
Advance Mobile Application Development class 04
Advance Mobile Application Development class 04Advance Mobile Application Development class 04
Advance Mobile Application Development class 04
 
Advance Mobile Application Development class 03
Advance Mobile Application Development class 03Advance Mobile Application Development class 03
Advance Mobile Application Development class 03
 
Advance Mobile Application Development class 02-B
Advance Mobile Application Development class 02-BAdvance Mobile Application Development class 02-B
Advance Mobile Application Development class 02-B
 
OS-ch02-part-1-2024.ppt
OS-ch02-part-1-2024.pptOS-ch02-part-1-2024.ppt
OS-ch02-part-1-2024.ppt
 
Advance Mobile Application Development class 02
Advance Mobile Application Development class 02Advance Mobile Application Development class 02
Advance Mobile Application Development class 02
 
ESSENTIAL of (CS/IT/IS) class 03-04 (NUMERIC SYSTEMS)
ESSENTIAL of (CS/IT/IS) class 03-04 (NUMERIC SYSTEMS)ESSENTIAL of (CS/IT/IS) class 03-04 (NUMERIC SYSTEMS)
ESSENTIAL of (CS/IT/IS) class 03-04 (NUMERIC SYSTEMS)
 
Advance Mobile Application Development class 01
Advance Mobile Application Development class 01Advance Mobile Application Development class 01
Advance Mobile Application Development class 01
 
ESSENTIAL of (CS/IT/IS) class 02 (HCI)
ESSENTIAL of (CS/IT/IS) class 02 (HCI)ESSENTIAL of (CS/IT/IS) class 02 (HCI)
ESSENTIAL of (CS/IT/IS) class 02 (HCI)
 
Seminar
SeminarSeminar
Seminar
 
ESSENTIAL of (CS/IT/IS)
ESSENTIAL of (CS/IT/IS)ESSENTIAL of (CS/IT/IS)
ESSENTIAL of (CS/IT/IS)
 
OS-ch01-2024.ppt
OS-ch01-2024.pptOS-ch01-2024.ppt
OS-ch01-2024.ppt
 
Mobile Application Development class 008
Mobile Application Development class 008Mobile Application Development class 008
Mobile Application Development class 008
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 

Advance Mobile Application Development class 07

  • 1. Advance Mobile Application Development Firebase Auth Dr. Mazin Alkathiri IT Department Seiyun University 2023-2024
  • 2. Firebase Authentication • Most apps need to know the identity of a user. Knowing a user's identity allows an app to securely save user data in the cloud and provide the same personalized experience across all of the user's devices. • Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. • It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more.
  • 3. Why firebase Auth • Being able to authenticate our users securely, it offers a customized experience to them based on their interests and preferences. • We can ensure that they have no problems accessing their private data while using our app from multiple devices. • Firebase Authentication provides all the server-side stuff for authenticating the user. Firebase Authentication becomes easy with SDK. It makes API easy to use. • Firebase Authentication also provides some user interface libraries which enable screens for us when we are logging it.
  • 4. Cont. • Firebase authentication supports authentication using a password, phone numbers, popular identity provider like Google, Facebook, and Twitter, etc. • We can sign in users to our app by using the FirebaseUI. • It handles the UI flows for signing in user with an email address and password, phone numbers, and popular providers, including Google sign-In and Facebook Login. • It can also handle cases like account recovery. • It is not required to design a UI since it is already provided for us. It means we don't have to write the activities. • We can also sign-in users using the Firebase Authentication SDK to integrate one or several sign-in methods into our app manually.
  • 5. How Authentication Works? • We first get authentication credentials from the user to sign a user into our app. • Credentials can be the user's email address and password. • The credential can be an OAuth token from an identity provider. • We then pass these credentials to the Firebase Authentication SDK. Backend services will then verify those credentials and return a response to the client. • After a successful sign in: • We can access the user's access to data stored in other Firebase products. • We can access the user's basic profile information. • We can use the provided authentication token to verify the identity of users in our own backend services. • An authenticated user can read and write data to the Firebase Real-time Database and Cloud Storage. • We can control the access of those authenticated users by modifying the Firebase Database Rules and Storage Security Rules.
  • 6. Users • A Firebase User object represents the account of a user who has signed up to the app in Firebase project. Apps have many registered users, and every app in a Firebase project shares a user data base. • AFirebase User instance is independent of a Firebase Auth instance. It means we can have several references to different users within the same context and still call any of their method • A Firebase User has a fixed set of basic properties such as Unique ID, Primary email address, Name, and a photo URL. • Firstly, a user signs up to the app. The user's profile data is populated with the primary email address if using email/password auth, account information available by the provider if using identity auth, and anything we want if using custom auth. • The user becomes the current user of the Auth instance when a user signs up or signs in. • The Auth instance stops to keep a reference to the User object. And no longer persists it states when a user signs out: • No current user • The user instance continues to be completely functional • If we keep a reference to it, we can still access and update the user's data. • Using listeners is the recommended way to track the current state of the Auth instance. • An Auth listener gets notified any time when something relevant happens to the Auth object.
  • 7. User Lifecycle • An Auth listener gets notified in the following situation • The Auth object finishes initializing, and a user was already signed in from a previous session or has been redirected from an identity provider's sign-in flow • A user signs in. • A user signs out. • The current user's access token is refreshed: • The access token expires. • The user changes their password. • The user re-authenticates
  • 8. Firebase: Google Sign-In Authentication • As we have discussed earlier, Firebase provides different types of Authentication methods. In our previous section, we learned how authentication is done using Firebase UI and Firebase SDK. In this section, we will learn another method, i.e., Google Sign-in Authentication. It is pretty easy to do. • Starting steps are the same as we have done with other authentication methods, which are as follows: • Creating an Android project. • Creating a Firebase project. • Adding Firebase to the Android project or application, either manually or Firebase Assistance. • Adding the required libraries and JSON files.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15. • Command to get debug SHA-1 (for Windows) where password is Android keytool -list -v -alias androiddebugkey -keystore C:Usershp.androiddebug.keystore
  • 16.
  • 17.
  • 18.
  • 19. Coding part • Starting by Adding the dependencies to pubspec.yaml file under dependency section.run the following cmd for add latest version dependencies. • flutter pub add firebase_auth • flutter pub add google_sign_in
  • 20. Building the login UI • Creating a login.dart • With the class GoogleSignInScreen with the help of ValueListenableBuilder import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:google_sign_in/google_sign_in.dart'; import 'package:tryingfirebase/home.dart';
  • 21. • t is an amazing widget. It builds the widget every time the valueListenable value changes. Its values remain synced with there listeners i.e. whenever the values change the ValueListenable listen to it. It updates the UI without using setState() or any other state management technique. • Properties: • valueListenable: • builder: • child: • These are the three properties of ValueListenableBuilder. bulder build widget depending upon the valueListenable value. valueListenable is an instance of ValueNotifier . child property is optional, it can be null if valueListenable value entirely depends upon the builder widget.
  • 22. body: ValueListenableBuilder( valueListenable: userCredential, builder: (context, value, child) { return (userCredential.value == '' || userCredential.value == null) ? Center( child: Card( …..…………… ), ) : Center( child: Column( …………………… ), ); })
  • 23. ? Center( child: Card( elevation: 5, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10)), child: IconButton( iconSize: 1, icon: Image.asset('assets/images/google_icon.png',), onPressed: () async { userCredential.value = await signInWithGoogle(); if (userCredential.value != null) print(userCredential.value.user!.email); Navigator.push(context, MaterialPageRoute( builder: (context) => HomeMenu(userCredential), ), ); }, ), ), )
  • 24. children: [ Container( decoration: BoxDecoration( shape: BoxShape.circle, border: Border.all( width: 1.5, color: Colors.black54)), child: Image.network(userCredential.value.user!.photoURL.toString()), ), const SizedBox(height: 20), Text(userCredential.value.user!.displayName.toString()), const SizedBox(height: 20), Text(userCredential.value.user!.email.toString()), const SizedBox(height: 30), ElevatedButton( onPressed: () async { bool result = await signOutFromGoogle(); if (result) userCredential.value = ''; }, child: const Text('Logout')) ],
  • 25. Future<dynamic> signInWithGoogle() async { try { final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn(); final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication; final credential = GoogleAuthProvider.credential( accessToken: googleAuth?.accessToken, idToken: googleAuth?.idToken, ); return await FirebaseAuth.instance.signInWithCredential(credential); } on Exception catch (e) { // TODO print('exception->$e'); } }
  • 26. Future<bool> signOutFromGoogle() async { try { await FirebaseAuth.instance.signOut(); return true; } on Exception catch (_) { return false; } } }
  • 27. Change the rules in Firestore Database
  • 28. Change the rules in firebase Storage