Skip to content

Commit

Permalink
Add first-visit guard
Browse files Browse the repository at this point in the history
Contributes to #28
  • Loading branch information
salah3x committed Jul 30, 2019
1 parent f6dc575 commit 98d1e5d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';

import { FirstVisitGuard } from './shared/first-visit.guard';

const routes: Routes = [
{
path: '',
loadChildren: () => import('./tabs/tabs.module').then(m => m.TabsPageModule)
loadChildren: () =>
import('./tabs/tabs.module').then(m => m.TabsPageModule),
canActivate: [FirstVisitGuard]
},
{
path: 'auth',
loadChildren: () => import('./auth/auth.module').then(m => m.AuthPageModule)
loadChildren: () =>
import('./auth/auth.module').then(m => m.AuthPageModule),
canActivate: [FirstVisitGuard]
}
];
@NgModule({
Expand Down
30 changes: 30 additions & 0 deletions src/app/shared/first-visit.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot,
RouterStateSnapshot,
UrlTree,
CanActivate,
Router
} from '@angular/router';
import { Plugins } from '@capacitor/core';
import { Observable } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class FirstVisitGuard implements CanActivate {
constructor(private router: Router) {}

canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
):
| boolean
| UrlTree
| Observable<boolean | UrlTree>
| Promise<boolean | UrlTree> {
return Plugins.Storage.get({ key: 'visited' }).then(value =>
!!value.value ? true : this.router.createUrlTree(['welcome'])
);
}
}

0 comments on commit 98d1e5d

Please sign in to comment.