Skip to content

Commit e15f0ae

Browse files
committed
15.4 Signup and Refactor Authentication code
1 parent 555c562 commit e15f0ae

File tree

3 files changed

+53
-30
lines changed

3 files changed

+53
-30
lines changed

src/app/auth/auth.page.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
<!-- <div class="ion-text-center">
3535
<ion-spinner color="primary" *ngIf="isLoading"></ion-spinner>
3636
</div> -->
37-
<ion-button type="button" color="primary" fill="clear" expand="block" (click)="onSwitchAuthMode()">Switch to {{ isLogin ? 'Signup' : 'Login'}}</ion-button>
38-
<ion-button type="submit" color="primary" (click)="onLogin()" expand="block" [disabled]="!f.valid">Login</ion-button>
37+
<ion-button type="button" color="primary" fill="clear" expand="block" (click)="isLogin = !isLogin">Switch to {{ isLogin ? 'Signup' : 'Login'}}</ion-button>
38+
<ion-button type="submit" color="primary" expand="block" [disabled]="!f.valid">{{ isLogin ? 'Login' : 'Signup'}}</ion-button>
3939
</ion-col>
4040
</ion-row>
4141
</ion-grid>

src/app/auth/auth.page.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import { AuthService } from './auth.service';
33
import { Router } from '@angular/router';
4-
import { LoadingController } from '@ionic/angular';
4+
import { AlertController, LoadingController } from '@ionic/angular';
55
import { NgForm } from '@angular/forms';
66

77
@Component({
@@ -16,12 +16,13 @@ export class AuthPage implements OnInit {
1616
constructor(
1717
private authService: AuthService,
1818
private router: Router,
19-
private loadingCtrl: LoadingController
19+
private loadingCtrl: LoadingController,
20+
private alrtCtrl: AlertController
2021
) {}
2122

2223
ngOnInit() {}
2324

24-
onLogin() {
25+
authenticate(email: string, password: string) {
2526
this.isLoading = true;
2627
this.authService.login();
2728
this.loadingCtrl
@@ -31,33 +32,36 @@ export class AuthPage implements OnInit {
3132
})
3233
.then((loadingEl) => {
3334
loadingEl.present();
34-
setTimeout(() => {
35+
this.authService.signup(email, password).subscribe((resData) => {
3536
this.isLoading = false;
3637
loadingEl.dismiss();
3738
this.router.navigateByUrl('/places/tabs/discover');
38-
}, 1500);
39+
}, errRes => {
40+
loadingEl.dismiss();
41+
this.showAlert(errRes.error.error.message);
42+
});
3943
});
4044
}
4145

42-
onSubmit(form: NgForm){
43-
if(form.invalid){
46+
onSubmit(form: NgForm) {
47+
if (form.invalid) {
4448
return;
4549
}
4650
const email = form.value.email;
4751
const password = form.value.password;
4852

49-
console.log(email, password);
50-
51-
52-
if(!this.isLogin) {
53-
// Send a request to login endpoint
54-
}
55-
else {
56-
// Send a request to signup endpoint
57-
}
53+
this.authenticate(email, password);
5854
}
5955

60-
onSwitchAuthMode(){
61-
this.isLogin = !this.isLogin;
56+
private showAlert(message: string) {
57+
this.alrtCtrl
58+
.create({
59+
header: 'Authentication Failed!',
60+
message: message,
61+
buttons: ['Okay'],
62+
})
63+
.then((alertEl) => {
64+
alertEl.present();
65+
});
6266
}
6367
}

src/app/auth/auth.service.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
1+
import { HttpClient } from '@angular/common/http';
12
import { Injectable } from '@angular/core';
3+
import { environment } from '../../environments/environment';
4+
5+
interface AuthResponseData {
6+
kind: string;
7+
idToken: string;
8+
email: string;
9+
refreshToken: string;
10+
localId:string;
11+
expiresIn: string;
12+
registered?: boolean;
13+
}
214

315
@Injectable({
4-
providedIn: 'root'
16+
providedIn: 'root',
517
})
618
export class AuthService {
7-
private _userIsAuthenticated = true;
8-
private _userId = 'abc';
19+
private _userIsAuthenticated = false;
20+
private _userId = null;
921

10-
get userIsAuthenticated() {
11-
return this._userIsAuthenticated;
12-
}
13-
get userId() {
14-
return this._userId;
15-
}
16-
constructor() { }
22+
get userIsAuthenticated() {
23+
return this._userIsAuthenticated;
24+
}
25+
get userId() {
26+
return this._userId;
27+
}
28+
constructor(private http: HttpClient) {}
29+
30+
signup(email: string, password: string) {
31+
return this.http.post<AuthResponseData>(
32+
`https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=${environment.firebaseAPIKey}`,
33+
{ email: email, password: password, returnSecureToken: true }
34+
);
35+
}
1736

1837
login() {
1938
this._userIsAuthenticated = true;

0 commit comments

Comments
 (0)