Created
April 30, 2019 12:47
-
-
Save mateusduraes/3d268329604daf6cb7b157724bfe2b03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Routes | |
const routes: Routes = [ | |
{ | |
canActivate: [UnauthenticatedGuard], | |
path: 'signin', | |
loadChildren: './pages/signin/signin.module#SigninPageModule', | |
}, | |
{ | |
canActivate: [AuthenticatedGuard], | |
path: 'channel-detail', | |
loadChildren: | |
'./pages/channel-detail/channel-detail.module#ChannelDetailPageModule', | |
}, | |
] | |
// Auth Guard | |
@Injectable({ | |
providedIn: 'root', | |
}) | |
export class AuthenticatedGuard implements CanActivate { | |
constructor( | |
private store: Store<any>, | |
private navCtrl: NavController, | |
) {} | |
canActivate( | |
next: ActivatedRouteSnapshot, | |
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { | |
return this.store.pipe(select(getIsLogged)) | |
.pipe( | |
tap((isLogged) => { | |
if (!isLogged) { | |
this.navCtrl.navigateRoot('/signin'); | |
} | |
}), | |
); | |
} | |
} | |
// Unauthenticated Guard | |
@Injectable({ | |
providedIn: 'root', | |
}) | |
export class UnauthenticatedGuard implements CanActivate { | |
constructor( | |
private store: Store<any>, | |
private navCtrl: NavController, | |
) {} | |
canActivate( | |
next: ActivatedRouteSnapshot, | |
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { | |
return this.store.pipe(select(getIsNotLogged)) | |
.pipe( | |
tap((isNotLogged) => { | |
if (!isNotLogged) { | |
this.navCtrl.navigateRoot('/channel-detail'); | |
} | |
}), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment