0% found this document useful (0 votes)
3 views26 pages

12 App Mobile Lab Authentication 2

The document outlines the implementation of Firebase Authentication with a focus on email verification, detailing user data fields and the steps to send verification emails. It also covers the Firebase SDK Admin for managing users and provides a hands-on code review for practical application. Additionally, it emphasizes the necessity of integrating the Firebase SDK to access certain user data functionalities.

Uploaded by

jixiaolong2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views26 pages

12 App Mobile Lab Authentication 2

The document outlines the implementation of Firebase Authentication with a focus on email verification, detailing user data fields and the steps to send verification emails. It also covers the Firebase SDK Admin for managing users and provides a hands-on code review for practical application. Additionally, it emphasizes the necessity of integrating the Firebase SDK to access certain user data functionalities.

Uploaded by

jixiaolong2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Applications for Mobile Devices

Authentication 2

Dr. Vítor Luiz da Silva Verbel


[email protected]

Course: 105025-2425
Contents

Firebase Authentication using Email Verification

Firebase Admin SDK

Hands on

Applications for Mobile Devices


Firebase Authentication using Email
Verification

Applications for Mobile Devices


Firebase User Data
Firebase authentication user data have the following fields:

Field Description

uid Unique ID for the user in your Firebase project.

email User’s email address.


displayName User’s display name (if set).

photoUrl URL of the user's profile photo (if available).

isEmailVerified true if the user has verified their email address.

phoneNumber User’s phone number (if authenticated with phone).

providerId The authentication provider used (e.g., password, google.com).

metadata Contains creation and last sign-in timestamps. Problem: Firebase console only shows five fields: email,
provider, creation date, last access and UID.
getIdToken() Returns a token to authenticate securely with your backend server.

https://firebase.google.com/docs/auth/admin/manage-users?hl=es-419#create_a_user

Applications for Mobile Devices


Firebase User Data
Firebase authentication user data have the following fields:
Field Description

uid Unique ID for the user in your Firebase project.


private fun sendEmailVerification() {
email User’s email address.
val user = auth.currentUser
displayName User’s display name (if set).
user!!.sendEmailVerification()
photoUrl URL of the user's profile photo (if available). .addOnCompleteListener { task ->
if (task.isSuccessful) {
isEmailVerified true if the user has verified their email address.
Log.d(TAG, "Email sent.")
phoneNumber User’s phone number (if authenticated with phone). }
}
providerId The authentication provider used (e.g., password, google.com). }
metadata Contains creation and last sign-in timestamps.

getIdToken() Returns a token to authenticate securely with your backend server.

https://firebase.google.com/docs/auth/admin/manage-users?hl=es-419#create_a_user

Applications for Mobile Devices


Firebase Email Verification
How to implement
1. Create a function to send verification email
2. Modify the signup function to send email instead of log-in
3. Modify the login function to only accept if the email is verified
4. Implement firebase SDK to control user data and manage users with verified emails*

• Firebase integrates email verification; the problem is


that to see this field you must implement Firebase SDK.

Note that there is no isEmailVerified column present.

Applications for Mobile Devices


Firebase Email Verification (Steps 1 and 2)

• Firebase integrates email verification; the problem is that to see this field you must implement Firebase SDK.

fun signup(email : String,password : String){


private fun sendEmailVerification() {
if(email.isEmpty() || password.isEmpty()){
val user = auth.currentUser _authState.value = AuthState.Error("Email or password can't be empty")
user!!.sendEmailVerification() return
.addOnCompleteListener { task -> }
_authState.value = AuthState.Loading
if (task.isSuccessful) { auth.createUserWithEmailAndPassword(email,password)
Log.d(TAG, "Email sent.") .addOnCompleteListener{task->
} if (task.isSuccessful){
sendEmailVerification()
} _authState.value = AuthState.Error("Please, confirm your email")
} }else{
_authState.value = AuthState.Error(task.exception?.message?:"Something went wrong")
(1) Send verification email function }
}
}

(2) Sending an email when signup

Applications for Mobile Devices


Firebase Email Verification (Step 3)

• Firebase integrates email verification; the problem is that to see this field you must implement Firebase SDK.

_authState.value = AuthState.Loading
auth.signInWithEmailAndPassword(email,password)
.addOnCompleteListener{task->
if (task.isSuccessful) {
val user = auth.currentUser

if (user != null && user.isEmailVerified) {


_authState.value = AuthState.Authenticated
} else {
_authState.value = AuthState.Error("Email address not verified. " +
"Please check your email before continuing.")
// sendEmailVerification() // opcional: send another verification email
auth.signOut() // opcional: cerrar sesión si no está verificado
}
} else {
_authState.value = AuthState.Error(task.exception?.message ?: "Something went wrong")
}
}

(3) Send an email when signup

Applications for Mobile Devices


Firebase Email Verification (Step 4)

• Implement Firebase SDK admin to manage users. Don’t worry this step will be explained in the following slides…

Applications for Mobile Devices


Firebase SDK Admin

Applications for Mobile Devices


What is Firebase SDK Admin

https://firebase.google.com/docs/auth/admin

Applications for Mobile Devices


Firebase SDK Admin – Manage Users

https://firebase.google.com/docs/auth/admin/manage-users

Applications for Mobile Devices


Firebase SDK Admin – Manage Users
It can be implemented in Node.js, Java,
Python, Go and C#.

It allows to manage firebase users exposing


many functions such as:
- get_user
- get_user_by_email
- list_users()

https://firebase.google.com/docs/auth/admin/manage-users

Applications for Mobile Devices


Hands On

Applications for Mobile Devices


Verification Email APP - Code Review
Verification Email APP
• Download /source examples/AuthenticationVerifyEmail.zip

Applications for Mobile Devices


Firebase SDK admin - Code Review
Firebase SDK implementation
• Step1: Download /source examples/firebase-userlist.zip
• Step2: Configure Firebase credentials
• Step3: Replace file and run docker container
• Step4: Open 127.0.0.1:5000

Applications for Mobile Devices


Firebase SDK admin - Code Review (Step1)

Firebase SDK implementation


• Step1: Download /source examples/firebase-userlist.zip

Applications for Mobile Devices


Firebase SDK admin - Code Review (Step2)
Firebase SDK implementation
• Step2: Configure Firebase credentials
• Go to firebase console https://console.firebase.google.com/
• Project Configuration > Service Account >

Applications for Mobile Devices


Firebase SDK admin - Code Review (Step2)
Firebase SDK implementation
• Step2: Configure Firebase credentials
• Generate new private key -> It will download a new json file
• Rename this json file to “google-services.json”

Applications for Mobile Devices


Firebase SDK admin - Code Review (Step2)
Firebase SDK implementation
• Step2: Configure Firebase credentials
• Open this file, you should see something like that:

Applications for Mobile Devices


Firebase SDK admin - Code Review (Step3)
Firebase SDK implementation
• Step3: Replace file and run docker container
• Replace the file “google-services.json” inside the firebase-userlist project by
your new file.
• Install docker and run it using the following commands:
• docker build -t firebase-userlist .
• docker run -p 5000:5000 firebase-userlist

Applications for Mobile Devices


Firebase SDK admin - Code Review (Step4)
Firebase SDK implementation
• Step4: Open 127.0.0.1:5000
• You should be able to see all users from your firebase project:

Applications for Mobile Devices


Sprint 04

Applications for Mobile Devices


Sprint 04 – Authentication

Applications for Mobile Devices 3


Project – Travel Planner

*Delays will incur a 30% penalty. The last delivery cannot be delayed.

Applications for Mobile Devices 3


Applications for Mobile Devices

That’s all

Full Name 2022 March 27


Week IV
Course: 105025-2425

You might also like