|
| 1 | +import { Component, OnInit } from '@angular/core'; |
| 2 | +import { Router } from '@angular/router'; |
| 3 | +import * as firebase from 'firebase'; |
| 4 | + |
| 5 | +import { WindowService, PhoneNumber } from '../../shared'; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + selector: 'app-phone-signin', |
| 9 | + templateUrl: './phone-signin.component.html', |
| 10 | + styleUrls: ['./phone-signin.component.scss'] |
| 11 | +}) |
| 12 | +export class PhoneSigninComponent implements OnInit { |
| 13 | + |
| 14 | + phoneNumber = new PhoneNumber() |
| 15 | + |
| 16 | + token: string; |
| 17 | + windowRef: any; |
| 18 | + verificationCode: string; |
| 19 | + currentUser: any; |
| 20 | + |
| 21 | + constructor(private win: WindowService, |
| 22 | + private router: Router) { } |
| 23 | + |
| 24 | + ngOnInit() { |
| 25 | + this.windowRef = this.win.windowRef |
| 26 | + this.windowRef.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container') |
| 27 | + |
| 28 | + this.windowRef.recaptchaVerifier.render() |
| 29 | + } |
| 30 | + |
| 31 | + sendLoginCode() { |
| 32 | + const appVerifier = this.windowRef.recaptchaVerifier; |
| 33 | + const num = this.phoneNumber.e164; |
| 34 | + firebase.auth().signInWithPhoneNumber(num, appVerifier) |
| 35 | + .then(result => { |
| 36 | + this.windowRef.confirmationResult = result; |
| 37 | + }) |
| 38 | + .catch( error => console.log(error) ); |
| 39 | + } |
| 40 | + |
| 41 | + verifyLoginCode() { |
| 42 | + this.windowRef.confirmationResult |
| 43 | + .confirm(this.verificationCode) |
| 44 | + .then( result => { |
| 45 | + this.currentUser = result.user; |
| 46 | + }) |
| 47 | + .then(response => { |
| 48 | + this.router.navigate(['/']); |
| 49 | + firebase.auth().currentUser.getIdToken() |
| 50 | + .then( |
| 51 | + (token: string) => this.token = token |
| 52 | + ); |
| 53 | + }) |
| 54 | + .catch( error => console.log(error, "Incorrect code entered?")); |
| 55 | + } |
| 56 | +} |
0 commit comments