Skip to content

Commit 6d079e5

Browse files
committed
Phonenumber authentication almost finished
1 parent 26fcb30 commit 6d079e5

13 files changed

+156
-29
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Run `npm start` for a dev server. Navigate to `http://localhost:4200/`. The app
4747
* Misc page
4848
* 404 page
4949
* Save email (keep in touch)
50-
* Authentication (Email, Google, Twitter, Facebook, Github and Anonymous)
50+
* Authentication (Email, Google, Twitter, Facebook, Github, Phonenumber and Anonymous)
5151
* Verification email after signup
5252
* Auth Guard
5353
* Profile
@@ -93,7 +93,7 @@ Run `npm start` for a dev server. Navigate to `http://localhost:4200/`. The app
9393

9494
## To Finish List (known issues)
9595
### Auth
96-
* Cellphone authentication
96+
* Cellphone authentication (optimizing)
9797
* Optimizing alerts
9898
* Google, Facebook, Twitter & Github signin/signup
9999
* Error messages: Email already exists, login failed, registration failed

src/app/app.module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import {
2626
DataStorageService,
2727
AlertService,
2828
AuthGuardService,
29-
AuthService
29+
AuthService,
30+
WindowService
3031
} from './components/shared';
3132

3233
// Main
@@ -77,7 +78,8 @@ import { EmailMeComponent } from './components/email-me/email-me.component';
7778
DataStorageService,
7879
AlertService,
7980
AuthGuardService,
80-
AuthService
81+
AuthService,
82+
WindowService
8183
],
8284
bootstrap: [AppComponent]
8385
})

src/app/components/auth/auth.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import { UsersRouting } from './users.routing'
1111
// Components
1212
import { SignupComponent } from './signup.component';
1313
import { SigninComponent } from './signin.component';
14+
import { PhoneSigninComponent } from './phone-signin/phone-signin.component';
1415

1516
@NgModule({
1617
declarations: [
1718
SignupComponent,
1819
SigninComponent,
20+
PhoneSigninComponent
1921
],
2022
imports: [
2123
CommonModule,
@@ -31,7 +33,8 @@ import { SigninComponent } from './signin.component';
3133
],
3234
exports: [
3335
SignupComponent,
34-
SigninComponent
36+
SigninComponent,
37+
PhoneSigninComponent
3538
]
3639
})
3740
export class AuthModule {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<md-card>
2+
<div [hidden]="currentUser">
3+
<h3>Phone Number login</h3>
4+
5+
<br/>+
6+
<md-input-container>
7+
<input mdInput type="text" [(ngModel)]="phoneNumber.country" class="input" placeholder="31" maxlength="2">
8+
</md-input-container>
9+
<md-input-container>
10+
<input mdInput type="text" [(ngModel)]="phoneNumber.area" class="input" placeholder="612" maxlength="3">
11+
</md-input-container>
12+
<md-input-container>
13+
<input mdInput type="text" [(ngModel)]="phoneNumber.prefix" class="input" placeholder="345" maxlength="3">
14+
</md-input-container>
15+
<md-input-container>
16+
<input mdInput type="text" [(ngModel)]="phoneNumber.line" class="input" placeholder="678" maxlength="4">
17+
</md-input-container>
18+
19+
<div id="recaptcha-container"></div><br/>
20+
21+
<button md-raised-button (click)="sendLoginCode()">SMS Text Login Code</button>
22+
23+
<div *ngIf="windowRef.confirmationResult">
24+
<hr>
25+
<label for="code">Enter your Verification Code Here</label><br>
26+
<input type="text" name="code" [(ngModel)]="verificationCode">
27+
28+
<button md-raised-button (click)="verifyLoginCode()">Verify</button>
29+
</div>
30+
31+
</div>
32+
</md-card>
33+
34+
<div *ngIf="currentUser">
35+
You have successfully logged in with your phone number!
36+
37+
UserId: {{ currentUser?.uid }}
38+
39+
</div>

src/app/components/auth/phone-signin/phone-signin.component.scss

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { PhoneSigninComponent } from './phone-signin.component';
4+
5+
describe('PhoneSigninComponent', () => {
6+
let component: PhoneSigninComponent;
7+
let fixture: ComponentFixture<PhoneSigninComponent>;
8+
9+
beforeEach(async(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [ PhoneSigninComponent ]
12+
})
13+
.compileComponents();
14+
}));
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(PhoneSigninComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should be created', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}

src/app/components/auth/signin.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
<!-- Progress bar -->
44
<md-progress-bar *ngIf="loading" mode="indeterminate"></md-progress-bar>
55

6+
<!-- TEMPORARY -->
7+
<app-phone-signin></app-phone-signin>
8+
69
<md-card>
710
<h3>Social login</h3>
811
<form (ngSubmit)="onSignInGoogle(g)" #g="ngForm">
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './profile.model';
22
export * from './user.model';
33
export * from './contact.model';
4+
export * from './phone-number.model';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class PhoneNumber {
2+
country: string;
3+
area: string;
4+
prefix: string;
5+
line: string;
6+
7+
get e164() {
8+
const num = this.country + this.area + this.prefix + this.line
9+
return `+${num}`
10+
}
11+
}

0 commit comments

Comments
 (0)