Git Product home page Git Product logo

firebase-otp's Introduction

firebase-otp

Authenticate with Firebase with a Phone Number Using JavaScript

You can use Firebase Authentication to sign in a user by sending an SMS message to the user's phone. The user signs in using a one-time code contained in the SMS message.

FirebaseUI

The easiest way to add phone number sign-in to your app is to use FirebaseUI, which includes a drop-in sign-in widget that implements sign-in flows for phone number sign-in, as well as password-based and federated sign-in. This document describes how to implement a phone number sign-in flow using the Firebase SDK. URL : https://opensource.google.com/projects/firebaseui

Enable phone number

To sign in users by SMS, you must first enable the Phone Number sign-in method for your Firebase project: 1)In the Firebase console, open the Authentication section. Firebase console URL : https://console.firebase.google.com/u/0/ 2)On the Sign-in Method page, enable the Phone Number sign-in method. 3)On the same page, if the domain that will host your app isn't listed in the OAuth redirect domains section, add your domain.

read documentation

Read all detail documentaion in detail on google firebase URL : https://firebase.google.com/docs/auth/web/phone-auth

My Code Explanation

1) Include there library and Initialize Firebase

<script src="https://www.gstatic.com/firebasejs/5.10.1/firebase.js"></script>
<script>
    // Initialize Firebase
    var config = {
        apiKey: "<****Used your API key********>",
        authDomain: "<***Domain Name****>.firebaseapp.com",
        databaseURL: "<****Firebase database url*******>",
        projectId: "<*****Project id********>",
        storageBucket: "<***domain****>.appspot.com",
        messagingSenderId: "<*****Sender id******>"
    };
    firebase.initializeApp(config);
</script>

2) Make Div for recaptcha

<div id="recaptcha-container"></div>

3)Set inivisible recaptcha & Send OTP to mobile number & Enter OTP to varification

  <script>
   firebase.auth().languageCode = 'en';
      window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container', {
      'size': 'invisible',
      'callback': function(response) {
          // reCAPTCHA solved, allow signInWithPhoneNumber.
          onSignInSubmit();
      }
      });
      // window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container');
      recaptchaVerifier.render().then(function(widgetId) {
          window.recaptchaWidgetId = widgetId;
      });
     
  //Send OTP to mobile number
      var phoneNumber = '+91<****Enter mobile number*****>';
      var appVerifier = window.recaptchaVerifier;
      firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
          .then(function (confirmationResult) {
              console.log(confirmationResult);
              window.confirmationResult = confirmationResult;
              // SMS sent. Prompt user to type the code from the message, then sign the
          // user in with confirmationResult.confirm(code).
              // setotp(confirmationResult);

          }).catch(function (error) {
          // Error; SMS not sent
          // ...
          console.log(error);
          });
    </script>
    
 //Enter OTP to varification
         var code = '123456';
          window.confirmationResult.confirm(code).then(function (result) {
            // User signed in successfully.
            console.log('User signed in successfully');
            var user = result.user;
            // ...
            }).catch(function (error) {
            // User couldn't sign in (bad verification code?)
            // ...
            });
    </script>

firebase-otp's People

Contributors

ashusalvi avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.