How to add authentication in firebase


Steps to add authentication in firebase

A) Add firebase to your app.

    1. Create firebase project from firebase console. If you have Google project associated to your android app then just click on import Google project option otherwise proceed with new project option in firebase console.
    2. If you are clicking on import Google project option then everything will be configured  by firebase, you just need to follow steps and lastly download the config file.
    3. If you click on new project option, then new project will be created. Your application is  recognized by package name entered in project name field in create project wizard.
    4. At the end download google-services.json file and paste this file in your android project's app folder.

B) Add Firebase SDK.

    1. In your root level build.gradle file add classpath in dependencies section :
         dependencies {
                 classpath 'com.google.gms:google-services:3.0.0'
         }

    2. Then add apply plugin:'com.google.gms.google-services' at the bottom of the file in your app level build.gradle file.
    3. Add compile 'com.google.firebase:firebase-core:9.2.0' in your app  level dependencies.

C) Authenticate FireBase using Password-Based accounts


    1. Add the dependency for Firebase Authentication to your app-level build.gradle file:
                  compile 'com.google.firebase:firebase-auth:9.2.0'
    2. Enable Email/Password in
        1. In firebase console open auth section
        2. On sign in method tab, enable email id/ password tab and click save.
    3. For signing up new user an instance of FirebaseAuth needs to be declare in onCreate() method of activity handling sign up operation.
    4. Set up an AuthStateListener that responds to changes in the user's sign-in state:
    5. When a new user signs up using your app's sign-up form, complete any new account validation steps that your app requires, such as verifying that the new account's password was correctly typed and meets your complexity requirements.
    6.  Create a new account by passing the new user's email address and password.

D) Sign in user with Email ID and Password


    1. In onCreate() method of activity handling sign-in, get the shared instance of FirebaseAuth object.
    2. Set up AuthStateChangeListener
    3. When user signs in to your app, pass the user email address and password to signInWithEmalAdressAndPassword.
    4. If sign-in succeed ,the AuthStateListener runs the AuthStateChanged callback. In callback you can get the current user using getCurrentUser().

E) Google Sign In

    1. For integrating Google sign in to your app follow step A) and step B)
    2. Add the dependencies for firebase authentication and Google sign-in to your app-level build.gradle file:
           Compile 'com.google.firebase:firebase-auth:9.2.0'
           Compile 'com.google.android.gms:play-services-auth:9.2.0'
    3. Connect your app to firebase project
    4. Enable Google sign in in the firebase console
        1. In firebase console open auth section
        2. On sign in methods tab enable Google sign in
    5. Integrate google sign-in into app
    6. In onCreate() method of activity handling sign-in, configure google sign-in to request the data.
    7. Create googleSignInOption with default_sign_in parameters.
    8. In onCreate() mehtod of activity handling sign-in, create googleApicClient object with access to Google sign-in api.
    9. Add Google sign-in button to your app in your layout xml file. In onClick method start the sign in intent with startActivityForResult. For capturing sign in result from sign in activity. In the activity's onActivityResult method, retrieve the sign-in result with getSignInResultFromIntent. If you retrieve the sign-in result you can check weather user signs in or not. When you configure the GoogleSignInOptions object call requestIdToken(serverId).
    10. In onCreate() method of your activity an object of FirebaseAuth. Set Up an authStateListener that responds to changes in the user's sign-in state. To get credentials from firebase use ID as a token from googleSignInAccount object and exchange it with firebase, and authenticate with firebase with firebase credentials.
                  If sign in with credentials succeed then  in that callback you can get user information.

F) Facebook sign in

    1. Follow step A) and step B)
    2. Add compile 'com.google.firebase:firebase-auth:9.2.0' this app level dependency in your gradle file.
    3. On the facebook developer site, get the app id and an app secret for your app
    4. Enable facebook sign in firebase
        1. Go to firebase console open auth section
        2. Click on sign in method tab and select facebook sign in and enter your app id and secret there.
    5. Integrate facebook login and once integrated login configuration with loginbutton, you will get registercallback with success or failure of facebook login result.
    6. In activity oncreate() get the instance of firebaseauth object
    7. Set up authstatelistener.
    8. After successful sign in , in register call back method get user token and exchange it for firebase token, authenticate it with firebase credentials.
    9. If the call to signinWithCredential succeeds, the authStateListener runs the onAuthStateChanged callback. In the callback, you can use the getCurrentUser method to get the user's account data.

G) Twitter sign in

    1. Follow step  A) and step  B)
    2. Add compile 'com.google.firebase:firebase-auth:9.2.0' dependency to your app level gradle file.
    3. Register your app as a developer application on twitter and get your app's api key and api secret.
    4. Enable twitter login
        1. Go to auth section in firebase console
        2. In sign in method tab enable twitter sign button. And enter app id and secrete
    5. In activity onCreate() get the instance of firebaseaAuth object
    6. Set up authStateListener.

    7. After successful sign in, in register call back method get user token and exchange it for firebase token, authenticate it with firebase credentials.
    8. If the call to signInwithCredential succeeds, the authStateListener runs the onAuthStateChanged callback. In the callback, you can use the getCurrentUser method to get the user's account data.

Podcast

Michael Patterson sat down with the CEO of Boston Byte, Mustapha Shaikh to discuss the significance and rapid digitization of the healthcar...