Skip to content

Commit 8c2b1de

Browse files
committed
fix(app-routing): use custom auth guards to implement redirect URLs
Until angular/angularfire#2448 is merged, query parameters can't currently be specified as arguments for `redirect*To` methods from the AngularFire auth guard module.
1 parent 68ec357 commit 8c2b1de

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/app/app-routing.module.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { canActivate, redirectUnauthorizedTo } from '@angular/fire/auth-guard';
21
import { NgModule } from '@angular/core';
32
import { Route, RouterModule } from '@angular/router';
43
import { AboutComponent } from './about/about.component';
@@ -13,9 +12,9 @@ import { TipsComponent } from './tips/tips.component';
1312
import { TodoArchivedComponent, TodoDashboardComponent, TodoHomeComponent, TodoProjectComponent } from './todo';
1413
import { TodoOutletComponent } from './todo/todo-outlet/todo-outlet.component';
1514
import { UserViewerComponent } from './user-viewer/user-viewer.component';
16-
import { DevelopmentGuard } from './development.guard';
1715

18-
const redirectUnauthorizedToLogin = redirectUnauthorizedTo(['login']);
16+
import { AuthGuard } from './auth.guard';
17+
import { DevelopmentGuard } from './development.guard';
1918

2019
// The routes
2120
const routes: Route[] = [
@@ -24,11 +23,11 @@ const routes: Route[] = [
2423
// Account
2524
{ path: 'account', redirectTo: '/settings/account' },
2625
// Chatrooms! Coming soon.
27-
{ path: 'chats', component: ChatsComponent, ...canActivate(redirectUnauthorizedToLogin) },
28-
{ path: 'chats/explore', component: ChatExploreComponent, ...canActivate(redirectUnauthorizedToLogin) },
29-
{ path: 'chats/:id', component: ChatViewerComponent, ...canActivate(redirectUnauthorizedToLogin) },
26+
{ path: 'chats', component: ChatsComponent, canActivate: [AuthGuard] },
27+
{ path: 'chats/explore', component: ChatExploreComponent, canActivate: [AuthGuard] },
28+
{ path: 'chats/:id', component: ChatViewerComponent, canActivate: [AuthGuard] },
3029
// Dashboard
31-
{ path: 'dashboard', component: DashboardComponent, ...canActivate(redirectUnauthorizedToLogin) },
30+
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
3231
// Downloads for the app. Currently a bit empty
3332
{ path: 'downloads', component: AppDownloadsComponent },
3433
// Login page
@@ -42,7 +41,7 @@ const routes: Route[] = [
4241
// Tips page.
4342
{ path: 'tips', component: TipsComponent },
4443
{
45-
path: 'todo', component: TodoOutletComponent, ...canActivate(redirectUnauthorizedToLogin), children: [
44+
path: 'todo', component: TodoOutletComponent, canActivate: [AuthGuard], children: [
4645
// All todos
4746
{ path: 'home', component: TodoHomeComponent },
4847
// Not working as of now

src/app/auth.guard.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Injectable } from '@angular/core';
2+
import { AngularFireAuth } from '@angular/fire/auth';
3+
import { CanActivate, ActivatedRouteSnapshot, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
4+
import { Observable } from 'rxjs';
5+
import { map, take } from 'rxjs/operators';
6+
7+
@Injectable({
8+
providedIn: 'root'
9+
})
10+
export class AuthGuard implements CanActivate {
11+
constructor(private afAuth: AngularFireAuth, private router: Router) {}
12+
13+
canActivate(
14+
_next: ActivatedRouteSnapshot,
15+
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
16+
return this.afAuth.user.pipe(
17+
take(1),
18+
map(user => !!user),
19+
map(loggedIn => !loggedIn ? this.router.createUrlTree(['/login'], { queryParams: { redirectUrl: state.url }}) : loggedIn)
20+
);
21+
}
22+
}

0 commit comments

Comments
 (0)