Skip to content

Commit 9eeba6c

Browse files
committed
feat(auth-guard): add support for specifying a string to redirect to
Closes #2287, #2144
1 parent 2de8501 commit 9eeba6c

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-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

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

1515
export type AuthPipeGenerator = (next: ActivatedRouteSnapshot, state: RouterStateSnapshot) => AuthPipe;
16-
export type AuthPipe = UnaryFunction<Observable<User|null>, Observable<boolean|any[]>>;
16+
export type AuthPipe = UnaryFunction<Observable<User|null>, Observable<boolean|string|any[]>>;
1717

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

@@ -49,14 +49,16 @@ export class AngularFireAuthGuard implements CanActivate {
4949
return this.authState.pipe(
5050
take(1),
5151
authPipeFactory(next, state),
52-
map(can => typeof can === 'boolean' ? can : this.router.createUrlTree(can as any[]))
52+
map(can => typeof can === 'boolean' ? can :
53+
Array.isArray(can) ? this.router.createUrlTree(can) : this.router.parseUrl(can)
54+
)
5355
);
5456
}
5557

5658
}
5759

5860
export const canActivate = (pipe: AuthPipeGenerator) => ({
59-
canActivate: [ AngularFireAuthGuard ], data: { authGuardPipe: pipe }
61+
canActivate: [ AngularFireAuthGuard ], data: { authGuardPipe: pipe }
6062
});
6163

6264

@@ -65,5 +67,5 @@ export const idTokenResult = switchMap((user: User|null) => user ? user.getIdTok
6567
export const emailVerified: AuthPipe = map(user => !!user && user.emailVerified);
6668
export const customClaims = pipe(idTokenResult, map(idTokenResult => idTokenResult ? idTokenResult.claims : []));
6769
export const hasCustomClaim = (claim: string) => pipe(customClaims, map(claims => claims.hasOwnProperty(claim)));
68-
export const redirectUnauthorizedTo = (redirect: any[]) => pipe(loggedIn, map(loggedIn => loggedIn || redirect));
69-
export const redirectLoggedInTo = (redirect: any[]) => pipe(loggedIn, map(loggedIn => loggedIn && redirect || true));
70+
export const redirectUnauthorizedTo = (redirect: string|any[]) => pipe(loggedIn, map(loggedIn => loggedIn || redirect));
71+
export const redirectLoggedInTo = (redirect: string|any[]) => pipe(loggedIn, map(loggedIn => loggedIn && redirect || true));

0 commit comments

Comments
 (0)