Skip to content

Commit fe31191

Browse files
feat(auth-guard): add support for specifying a string to redirect to (#2448)
* feat(auth-guard): add support for specifying a `string` to redirect to Closes #2287, #2144 * refactor(auth-guard): make ternary easier to read Co-authored-by: James Daniels <[email protected]>
1 parent dbf31d9 commit fe31191

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

docs/auth/router-guards.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ import { customClaims } from '@angular/fire/auth-guard';
8080

8181
// This pipe will only allow users with the editor role to access the route
8282
// { path: 'articles/:id/edit', component: ArticleEditComponent, ...canActivate(editorOnly) }
83-
const editorOnly = () => pipe(customClaims, map(claims => claims.role === "editor"));
83+
const editorOnly = () => pipe(customClaims, map(claims => claims.role === 'editor'));
8484
```
8585

8686
### Using router state
@@ -98,5 +98,5 @@ const onlyAllowSelf = (next) => map(user => !!user && next.params.userId === use
9898

9999
// Only allow navigation to the route if the user has a custom claim matching :accountId
100100
// { path: 'accounts/:accountId/billing', component: BillingDetailsComponent, ...canActivate(accountAdmin) }
101-
const accountAdmin = (next) => pipe(customClaims, map(claims => claims[`account-${next.params.accountId}-role`] === "admin"));
101+
const accountAdmin = (next) => pipe(customClaims, map(claims => claims[`account-${next.params.accountId}-role`] === 'admin'));
102102
```

src/auth-guard/auth-guard.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from '@angular/fire';
1515

1616
export type AuthPipeGenerator = (next: ActivatedRouteSnapshot, state: RouterStateSnapshot) => AuthPipe;
17-
export type AuthPipe = UnaryFunction<Observable<firebase.User|null>, Observable<boolean|any[]>>;
17+
export type AuthPipe = UnaryFunction<Observable<firebase.User|null>, Observable<boolean|string|any[]>>;
1818

1919
export const loggedIn: AuthPipe = map(user => !!user);
2020

@@ -54,14 +54,23 @@ export class AngularFireAuthGuard implements CanActivate {
5454
return this.authState.pipe(
5555
take(1),
5656
authPipeFactory(next, state),
57-
map(can => typeof can === 'boolean' ? can : this.router.createUrlTree(can as any[]))
57+
map(can => {
58+
if (typeof can === 'boolean') {
59+
return can;
60+
} else if (Array.isArray(can)) {
61+
return this.router.createUrlTree(can);
62+
} else {
63+
// TODO(EdricChan03): Add tests
64+
return this.router.parseUrl(can);
65+
}
66+
})
5867
);
5968
}
6069

6170
}
6271

6372
export const canActivate = (pipe: AuthPipeGenerator) => ({
64-
canActivate: [ AngularFireAuthGuard ], data: { authGuardPipe: pipe }
73+
canActivate: [ AngularFireAuthGuard ], data: { authGuardPipe: pipe }
6574
});
6675

6776

@@ -71,7 +80,7 @@ export const emailVerified: AuthPipe = map(user => !!user && user.emailVerified)
7180
export const customClaims = pipe(idTokenResult, map(idTokenResult => idTokenResult ? idTokenResult.claims : []));
7281
export const hasCustomClaim: (claim: string) => AuthPipe =
7382
(claim) => pipe(customClaims, map(claims => claims.hasOwnProperty(claim)));
74-
export const redirectUnauthorizedTo: (redirect: any[]) => AuthPipe =
83+
export const redirectUnauthorizedTo: (redirect: string|any[]) => AuthPipe =
7584
(redirect) => pipe(loggedIn, map(loggedIn => loggedIn || redirect));
76-
export const redirectLoggedInTo: (redirect: any[]) => AuthPipe =
85+
export const redirectLoggedInTo: (redirect: string|any[]) => AuthPipe =
7786
(redirect) => pipe(loggedIn, map(loggedIn => loggedIn && redirect || true));

0 commit comments

Comments
 (0)