Skip to content

Commit 0609a7e

Browse files
committed
15.11 Using userId in AuthGuard and fetchbookings
1 parent 59b6656 commit 0609a7e

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

src/app/auth/auth.guard.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { ActivatedRouteSnapshot, CanLoad, Route, Router, RouterStateSnapshot, UrlSegment, UrlTree } from '@angular/router';
3-
import { Observable } from 'rxjs';
3+
import { Observable, take, tap } from 'rxjs';
44
import { AuthService } from './auth.service';
55

66
@Injectable({
@@ -9,10 +9,11 @@ import { AuthService } from './auth.service';
99
export class AuthGuard implements CanLoad {
1010
constructor(private authService: AuthService, private router: Router){}
1111
canLoad(route: Route, segments: UrlSegment[]): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> {
12-
if(!this.authService.userIsAuthenticated){
13-
this.router.navigateByUrl('/auth');
14-
}
15-
return this.authService.userIsAuthenticated;
12+
return this.authService.userIsAuthenticated.pipe(take(1), tap(isAuthenticated => {
13+
if(!isAuthenticated){
14+
this.router.navigateByUrl('/auth');
15+
}
16+
}));
1617
}
1718

1819
}

src/app/bookings/booking.service.ts

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,36 +95,40 @@ export class BookingService {
9595
}
9696

9797
fetchBookings() {
98-
return this.http
99-
.get<{ [key: string]: BookingData }>(
100-
`https://ionic-angular-backend-66c35-default-rtdb.asia-southeast1.firebasedatabase.app/bookings.json?orderBy="userId"&equalTo="${this.auth.userId}"`
101-
)
102-
.pipe(
103-
map((bookingData) => {
104-
const bookings = [];
105-
for (const key in bookingData) {
106-
if (bookingData.hasOwnProperty(key)) {
107-
bookings.push(
108-
new Booking(
109-
key,
110-
bookingData[key].placeId,
111-
bookingData[key].userId,
112-
bookingData[key].placeTitle,
113-
bookingData[key].placeImage,
114-
bookingData[key].firstName,
115-
bookingData[key].lastName,
116-
bookingData[key].guestnumber,
117-
new Date(bookingData[key].bookedFrom),
118-
new Date(bookingData[key].bookedTo)
119-
)
120-
);
121-
}
98+
return this.auth.userId.pipe(
99+
switchMap((userId) => {
100+
if (!userId) {
101+
throw new Error('User not found!');
102+
}
103+
return this.http.get<{ [key: string]: BookingData }>(
104+
`https://ionic-angular-backend-66c35-default-rtdb.asia-southeast1.firebasedatabase.app/bookings.json?orderBy="userId"&equalTo="${userId}"`
105+
);
106+
}),
107+
map((bookingData) => {
108+
const bookings = [];
109+
for (const key in bookingData) {
110+
if (bookingData.hasOwnProperty(key)) {
111+
bookings.push(
112+
new Booking(
113+
key,
114+
bookingData[key].placeId,
115+
bookingData[key].userId,
116+
bookingData[key].placeTitle,
117+
bookingData[key].placeImage,
118+
bookingData[key].firstName,
119+
bookingData[key].lastName,
120+
bookingData[key].guestnumber,
121+
new Date(bookingData[key].bookedFrom),
122+
new Date(bookingData[key].bookedTo)
123+
)
124+
);
122125
}
123-
return bookings;
124-
}),
125-
tap((bookings) => {
126-
this._bookings.next(bookings);
127-
})
128-
);
126+
}
127+
return bookings;
128+
}),
129+
tap((bookings) => {
130+
this._bookings.next(bookings);
131+
})
132+
);
129133
}
130134
}

0 commit comments

Comments
 (0)