Fireship · @fireship
Firebase Phone Authentication in Angular 4
Uploaded July 19, 2017 · Length 4:33
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.
- 0:00
fire base recently rolled out a new
- 0:02
phone authentication system it's not
- 0:04
currently supported in angularfire - but
- 0:06
we're going to use the SDK directly to
- 0:08
implement phone off into our angular 4
- 0:11
app it works by having to use a verified
- 0:13
CAPTCHA then sends a text message with a
- 0:16
verification code to their number the
- 0:19
user then enters its gratification code
- 0:21
and they're off state will be updated in
- 0:23
the app it's important to point out that
- 0:25
phone auth is less secure than other
- 0:26
methods primarily because phone numbers
- 0:28
can be transferred between different
- 0:30
people it does however work very well
- 0:32
with two-factor authentication where you
- 0:34
use an additional sign-in method and
- 0:35
link the two accounts before getting
- 0:38
started make sure you have phone
- 0:39
authentication enabled in firebase in
- 0:41
order to use the CAPTCHA we'll need to
- 0:44
interact with the window object in the
- 0:45
browser it's considered a bad practice
- 0:47
to work with global browser objects such
- 0:50
as window directly in the component so
- 0:52
we'll extract it out into its own
- 0:53
injectable service it's just a simple
- 0:56
type script getter that returns the
- 0:57
window object now we can start building
- 1:00
the phone login component first we'll
- 1:03
import the window service as well as the
- 1:06
firebase SDK to make phone number
- 1:09
validation a little easier I'm going to
- 1:11
extract it into its own class and break
- 1:13
it into four different parts when we
- 1:15
send the number to firebase it needs to
- 1:17
be an e 164 format which is just a
- 1:20
string that starts with a plus sign
- 1:21
followed by the country code and then
- 1:23
the full phone number with no spaces or
- 1:25
dashes from there we'll set a variable
- 1:28
to hold the window reference and another
- 1:32
variable to hold the phone number
- 1:33
another one for the verification code
- 1:35
after the user receives the tax and
- 1:38
another for the authenticated user
- 1:40
during ng on init we reference the
- 1:43
window and then we initialized that
- 1:45
captcha on the window itself the
- 1:48
reCAPTCHA container should just be an
- 1:49
empty div with that ID in the template
- 1:51
kollene render will replace that div
- 1:54
with the reCAPTCHA content after the
- 1:57
user passes the CAPTCHA and enters their


