Skip to content

Commit 7401b59

Browse files
committed
15.15 Adding a Reactive Logout System
1 parent 9b3561d commit 7401b59

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

src/app/app.component.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnDestroy, OnInit } from '@angular/core';
22
import { AuthService } from './auth/auth.service';
33
import { Router } from '@angular/router';
44

55
import { SplashScreen } from '@capacitor/splash-screen';
66
import { Platform } from '@ionic/angular'
7+
import { Subscription } from 'rxjs';
78

89
@Component({
910
selector: 'app-root',
1011
templateUrl: 'app.component.html',
1112
styleUrls: ['app.component.scss'],
1213
})
13-
export class AppComponent {
14+
export class AppComponent implements OnInit, OnDestroy {
15+
private authSub: Subscription;
16+
private prevAuthState = false;
1417
constructor(private auth: AuthService, private router: Router, private platform: Platform) {
1518
this.initializeApp()
1619
}
17-
20+
21+
22+
ngOnInit() {
23+
this.authSub = this.auth.userIsAuthenticated.subscribe(isAuth => {
24+
if(!isAuth && this.prevAuthState !== isAuth){
25+
this.router.navigateByUrl('auth');
26+
}
27+
this.prevAuthState = isAuth;
28+
})
29+
}
30+
31+
ngOnDestroy() {
32+
if(this.authSub) {
33+
this.authSub.unsubscribe();
34+
}
35+
}
1836
initializeApp () {
1937
this.platform.ready().then(async () => {
2038
await SplashScreen.hide();
2139
})
2240
}
2341
onLogout(){
2442
this.auth.logout();
25-
this.router.navigateByUrl('/auth');
2643
}
2744
}

src/app/auth-routing.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NgModule } from '@angular/core';
22
import { Routes, RouterModule } from '@angular/router';
33

4-
import { AuthPage } from './auth.page';
4+
import { AuthPage } from './auth/auth.page';
55

66
const routes: Routes = [
77
{

src/app/auth/auth.page.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export class AuthPage implements OnInit {
6060
const password = form.value.password;
6161

6262
this.authenticate(email, password);
63+
form.reset()
6364
}
6465

6566
private showAlert(message: string) {

src/app/auth/auth.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export class AuthService {
9898

9999
logout() {
100100
this._user.next(null);
101+
Preferences.remove({key: 'authData'});
101102
}
102103

103104
private setUserData(userData: AuthResponseData) {

src/app/bookings/booking.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export class BookingService {
9696

9797
fetchBookings() {
9898
return this.auth.userId.pipe(
99+
take(1),
99100
switchMap((userId) => {
100101
if (!userId) {
101102
throw new Error('User not found!');

src/app/places/discover/place-detail/place-detail.page.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ActionSheetController, AlertController, LoadingController, ModalControl
44
import { CreateBookingComponent } from '../../../bookings/create-booking/create-booking.component';
55
import { Place } from '../../place.model';
66
import { PlacesService } from '../../places.service';
7-
import { Subscription, switchMap } from 'rxjs';
7+
import { Subscription, switchMap, take } from 'rxjs';
88
import { BookingService } from '../../../bookings/booking.service';
99
import { AuthService } from '../../../auth/auth.service';
1010

@@ -39,7 +39,9 @@ export class PlaceDetailPage implements OnInit, OnDestroy {
3939
return;
4040
}
4141
let fetchedUserId: string;
42-
this.auth.userId.pipe(switchMap(userId => {
42+
this.auth.userId.pipe(
43+
take(1),
44+
switchMap(userId => {
4345
if(!userId) {
4446
throw new Error('Found no user!');
4547
}

0 commit comments

Comments
 (0)