Fireship · @fireship

Firebase Phone Authentication in Angular 4

Uploaded July 19, 2017 · Length 4:33

Firebase Phone Authentication in Angular 4

Summary

Implement phone authentication in Angular 4 using Firebase Phone Authentication SDK and reCAPTCHA.

  • firebase phone authentication
  • angular 4
  • recaptcha
  • phone login
  • firebase sdk

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    fire base recently rolled out a new

  2. 0:02

    phone authentication system it's not

  3. 0:04

    currently supported in angularfire - but

  4. 0:06

    we're going to use the SDK directly to

  5. 0:08

    implement phone off into our angular 4

  6. 0:11

    app it works by having to use a verified

  7. 0:13

    CAPTCHA then sends a text message with a

  8. 0:16

    verification code to their number the

  9. 0:19

    user then enters its gratification code

  10. 0:21

    and they're off state will be updated in

  11. 0:23

    the app it's important to point out that

  12. 0:25

    phone auth is less secure than other

  13. 0:26

    methods primarily because phone numbers

  14. 0:28

    can be transferred between different

  15. 0:30

    people it does however work very well

  16. 0:32

    with two-factor authentication where you

  17. 0:34

    use an additional sign-in method and

  18. 0:35

    link the two accounts before getting

  19. 0:38

    started make sure you have phone

  20. 0:39

    authentication enabled in firebase in

  21. 0:41

    order to use the CAPTCHA we'll need to

  22. 0:44

    interact with the window object in the

  23. 0:45

    browser it's considered a bad practice

  24. 0:47

    to work with global browser objects such

  25. 0:50

    as window directly in the component so

  26. 0:52

    we'll extract it out into its own

  27. 0:53

    injectable service it's just a simple

  28. 0:56

    type script getter that returns the

  29. 0:57

    window object now we can start building

  30. 1:00

    the phone login component first we'll

  31. 1:03

    import the window service as well as the

  32. 1:06

    firebase SDK to make phone number

  33. 1:09

    validation a little easier I'm going to

  34. 1:11

    extract it into its own class and break

  35. 1:13

    it into four different parts when we

  36. 1:15

    send the number to firebase it needs to

  37. 1:17

    be an e 164 format which is just a

  38. 1:20

    string that starts with a plus sign

  39. 1:21

    followed by the country code and then

  40. 1:23

    the full phone number with no spaces or

  41. 1:25

    dashes from there we'll set a variable

  42. 1:28

    to hold the window reference and another

  43. 1:32

    variable to hold the phone number

  44. 1:33

    another one for the verification code

  45. 1:35

    after the user receives the tax and

  46. 1:38

    another for the authenticated user

  47. 1:40

    during ng on init we reference the

  48. 1:43

    window and then we initialized that

  49. 1:45

    captcha on the window itself the

  50. 1:48

    reCAPTCHA container should just be an

  51. 1:49

    empty div with that ID in the template

  52. 1:51

    kollene render will replace that div

  53. 1:54

    with the reCAPTCHA content after the

  54. 1:57

    user passes the CAPTCHA and enters their