Created
December 16, 2019 21:38
-
-
Save umutyerebakmaz/85c866e3710b20b42726ca034875e919 to your computer and use it in GitHub Desktop.
This file contains 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
import { NgModule } from '@angular/core'; | |
import { RouterModule, Routes } from '@angular/router'; | |
import { CanActivateRouteGuardService } from './services/can-activate-route-guard.service'; | |
import { CantActivateRouteGuardService } from './services/cant-activate-route-guard.service'; | |
const routes: Routes = [ | |
{ | |
path: 'login', | |
loadChildren: './components/login-page/login-page.module#LoginPageModule', | |
canActivate: [CantActivateRouteGuardService] | |
}, | |
{ | |
path: 'admin/publishers', | |
loadChildren: './components/admin-publishers-page/admin-publishers-page.module#AdminPublishersPageModule', | |
canActivate: [CanActivateRouteGuardService] | |
} | |
@NgModule({ | |
imports: [ | |
RouterModule.forRoot(routes) | |
], | |
exports: [RouterModule], | |
providers: [] | |
}) | |
export class AppRoutingModule { } |
This file contains 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
import { Injectable } from '@angular/core'; | |
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router'; | |
import { LoginPageMeGQL } from '@src/generated-types'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class CanActivateRouteGuardService implements CanActivate { | |
constructor( | |
private router: Router, | |
private loginPageMeGQL: LoginPageMeGQL | |
) { } | |
async canActivate( | |
route: ActivatedRouteSnapshot, | |
state: RouterStateSnapshot | |
) { | |
const result = await this.loginPageMeGQL.fetch({}, { | |
fetchPolicy: 'cache-first' | |
}).toPromise(); | |
if (!result.data.me) { | |
this.router.navigateByUrl('/login'); | |
return false; | |
} | |
return true; | |
} | |
} | |
This file contains 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
import { Injectable } from '@angular/core'; | |
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router'; | |
import { LoginPageMeGQL } from '@src/generated-types'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class CantActivateRouteGuardService implements CanActivate { | |
constructor( | |
private router: Router, | |
private loginPageMeGQL: LoginPageMeGQL | |
) { } | |
async canActivate( | |
route: ActivatedRouteSnapshot, | |
state: RouterStateSnapshot | |
) { | |
const result = await this.loginPageMeGQL.fetch({}, { | |
fetchPolicy: 'cache-first' | |
}).toPromise(); | |
if (result.data.me) { | |
this.router.navigateByUrl('/'); | |
return false; | |
} | |
return true; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment