lecture 12: react-native firebase authentication

38
Kobkrit Viriyayudhakorn, Ph.D. [email protected] http://www.kobkrit.com Completing Chat Room App

Upload: kobkrit-viriyayudhakorn

Post on 29-Jan-2018

625 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Lecture 12: React-Native Firebase Authentication

Kobkrit Viriyayudhakorn, Ph.D. [email protected]

http://www.kobkrit.com

Completing Chat Room App

Page 2: Lecture 12: React-Native Firebase Authentication
Page 3: Lecture 12: React-Native Firebase Authentication

Understand Firebase Pricing

Page 4: Lecture 12: React-Native Firebase Authentication

Very Easyto Fake an user.

• This is bad.

• We need something to check authority of the chatter.

• We need a serious and professional chat app for serious things.

• Let’s do the authentication.

Page 5: Lecture 12: React-Native Firebase Authentication

Firebase Authentication• Firebase Authentication provides backend services, easy-to-use

SDKs, and ready-made UI libraries to authenticate users to your app.

• API: https://firebase.google.com/docs/reference/js/firebase.auth.Auth

• It supports authentication using

• Passwords

• Federated Identity Providers

• Google, Facebook, Twitter, and Github.

Page 6: Lecture 12: React-Native Firebase Authentication

Enable Sign-in Method

Page 7: Lecture 12: React-Native Firebase Authentication

Enable E-mail Password

Page 8: Lecture 12: React-Native Firebase Authentication

Create New Project and Install Firebase

• We will install Firebase

• Open Terminal and change to a working directory

• $|> create-react-native-app firebase2

• $|> cd firebase2

• $|> npm install firebase moment --save

• $|> atom .

Page 9: Lecture 12: React-Native Firebase Authentication

Code From Previous Lecture• http://bit.ly/2zGjgfI

Page 10: Lecture 12: React-Native Firebase Authentication

Adding Firebase Authentication to Chat App

1.js

Page 11: Lecture 12: React-Native Firebase Authentication

1.js

Page 12: Lecture 12: React-Native Firebase Authentication

• Firebase gives us a reactive method for listening for authentication. That is we can set up a listener to call a method when the user logs in and out.

• When user is logout, the login modal will be shown up and reset the name back to the “Anonymous”

• When user is login, the name text label at the header of the app is changed.

1.js

Page 13: Lecture 12: React-Native Firebase Authentication

• To sign a user in with their email and password, use the signInWithEmailAndPassword() function. It accepts two parameters, the user's email and password.

• After user successfully login, it will hide the login modal, and trigger the onAuthStateChanged() [in the previous slide] to change the name.

• If login failed, it will show alert popup with the error message.

1.js

Page 14: Lecture 12: React-Native Firebase Authentication

• We can create a user by calling the createUserWithEmailAndPassword() function. It accepts two parameters, an email and a password.

• After an user successfully created, it will hide the login modal, and trigger the onAuthStateChanged() [in the previous 2 slides] to change the name.

• If register failed, it will show alert popup with the error message.

1.js

Page 15: Lecture 12: React-Native Firebase Authentication

• To sign the current user out, use the signOut() method. It accepts no parameters

• After an user successfully sign out, it will trigger the onAuthStateChanged() [in the previous 3 slides] to show the login modal and change the name back to “Anonymous”

• Why? We can’t set it here.. It is because sometime we can kick users out of the system from firebase console or simply timeout expire.

• If we set the showing login modal code here, the kicking out case will not trigger the modal show up.

• It is better to handle everything in onAuthStateChanged().

1.js

Page 16: Lecture 12: React-Native Firebase Authentication

GetCurrentUser()• Not used in the Chat App, but it is helpful in other apps.

• Although you can get the current user using the getCurrentUser() method, it's better to use this from within the callback function provided by onAuthStateChanged().

• However, if you need to get the current user, call the getCurrentUser() method:

Page 17: Lecture 12: React-Native Firebase Authentication

Login UI

1.js

Page 18: Lecture 12: React-Native Firebase Authentication

Register UI

1.js

Page 19: Lecture 12: React-Native Firebase Authentication

• Sign Out UI

• And putting them in render() method.

1.js

Page 20: Lecture 12: React-Native Firebase Authentication

Login Screen Register Screen 1.js

Page 21: Lecture 12: React-Native Firebase Authentication

Register Successfully Register Failed: Using Duplicated E-mail

1.js

Page 22: Lecture 12: React-Native Firebase Authentication

Login Successfully Wrong E-mail Wrong Password

1.js

Page 23: Lecture 12: React-Native Firebase Authentication

Sign Out Button Clicked.

1.js

Page 24: Lecture 12: React-Native Firebase Authentication

Manage Users in Firebase Console

Page 25: Lecture 12: React-Native Firebase Authentication

E-mail Templates

Page 26: Lecture 12: React-Native Firebase Authentication

Password Resethttps://firebase.google.com/docs/reference/js/firebase.auth.Auth#sendPasswordResetEmail

Page 27: Lecture 12: React-Native Firebase Authentication

Confirm Password Reset

https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#confirmPasswordReset

Page 28: Lecture 12: React-Native Firebase Authentication

Verify Password Reset• https://firebase.google.com/docs/reference/js/

firebase.auth.Auth.html#verifyPasswordResetCode

Page 29: Lecture 12: React-Native Firebase Authentication

Making UI for Password Reset

• Change "isShowLogin" boolean state variable to "show" string state variable

• show == "login", will show login dialog.

• show == "register", will show register dialog.

• show == "passwordReset", will show password reset dialog

• show == 'passwordConfirm', will show password confirm dialog.

Page 30: Lecture 12: React-Native Firebase Authentication

2 Additional Dialog UIs• Password Reset Dialog needs

• E-mail

• Password Confirmation Dialog needs

• Code

• New Password

Page 31: Lecture 12: React-Native Firebase Authentication

Facebook Login With Firebase and Expo

• We can do Facebook with help of the Firebase and Expo quite easily.

• https://docs.expo.io/versions/latest/sdk/facebook.html

• You need an Facebook Developer ID, and create a new Facebook app.

• https://developers.facebook.com/

Page 32: Lecture 12: React-Native Firebase Authentication

Adding the following code..2.js

Page 33: Lecture 12: React-Native Firebase Authentication

For Facebook Login in iOS

https://developers.facebook.com/docs/facebook-login/ios

Facebook Developer App Setting

Page 34: Lecture 12: React-Native Firebase Authentication

For Facebook Login in Android

Facebook Developer App Setting

Page 35: Lecture 12: React-Native Firebase Authentication

UI for Facebook Login2.js

Page 36: Lecture 12: React-Native Firebase Authentication

2.js

Page 37: Lecture 12: React-Native Firebase Authentication

2.js

Page 38: Lecture 12: React-Native Firebase Authentication

Q/A