Skip to content

Commit 115142c

Browse files
committed
Updated
1 parent 59da8db commit 115142c

File tree

11 files changed

+116
-23
lines changed

11 files changed

+116
-23
lines changed

src/app/app-routing.module.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import { NgModule } from '@angular/core';
22
import { Routes, RouterModule } from '@angular/router';
33

4+
import { AuthGuardService } from './services/auth-guard.service';
5+
46
const routes: Routes = [
5-
{ path: '', redirectTo: 'home', pathMatch: 'full' },
6-
{ path: 'home', loadChildren: './home/home.module#HomePageModule' },
7+
// { path: '', redirectTo: 'home', pathMatch: 'full' },
8+
{ path: '', redirectTo: 'login', pathMatch: 'full' },
9+
// { path: 'home', loadChildren: './home/home.module#HomePageModule' },
710
{ path: 'login', loadChildren: './public/login/login.module#LoginPageModule' },
811
{ path: 'register', loadChildren: './public/register/register.module#RegisterPageModule' },
9-
{ path: 'dashboard', loadChildren: './members/dashboard/dashboard.module#DashboardPageModule' },
12+
{
13+
path: 'members',
14+
canActivate: [AuthGuardService],
15+
loadChildren: './members/member-routing.module#MemberRoutingModule'
16+
},
17+
// { path: 'dashboard', loadChildren: './members/dashboard/dashboard.module#DashboardPageModule' },
1018
];
1119

1220
@NgModule({

src/app/app.component.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { Platform } from '@ionic/angular';
44
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
55
import { StatusBar } from '@ionic-native/status-bar/ngx';
66

7+
import { Router } from '@angular/router';
8+
import { AuthenticationService } from './services/authentication.service';
9+
710
@Component({
811
selector: 'app-root',
912
templateUrl: 'app.component.html'
@@ -12,7 +15,9 @@ export class AppComponent {
1215
constructor(
1316
private platform: Platform,
1417
private splashScreen: SplashScreen,
15-
private statusBar: StatusBar
18+
private statusBar: StatusBar,
19+
private router: Router,
20+
private authenticationService: AuthenticationService
1621
) {
1722
this.initializeApp();
1823
}
@@ -21,6 +26,14 @@ export class AppComponent {
2126
this.platform.ready().then(() => {
2227
this.statusBar.styleDefault();
2328
this.splashScreen.hide();
29+
30+
this.authenticationService.authenticationState.subscribe(state => {
31+
if (state) {
32+
this.router.navigate(['members', 'dashboard']);
33+
} else {
34+
this.router.navigate(['login']);
35+
}
36+
});
2437
});
2538
}
2639
}

src/app/app.module.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ import { StatusBar } from '@ionic-native/status-bar/ngx';
99
import { AppComponent } from './app.component';
1010
import { AppRoutingModule } from './app-routing.module';
1111

12+
import { IonicStorageModule } from '@ionic/storage';
13+
1214
@NgModule({
1315
declarations: [AppComponent],
1416
entryComponents: [],
15-
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
17+
imports: [
18+
BrowserModule,
19+
IonicModule.forRoot(),
20+
AppRoutingModule,
21+
IonicStorageModule.forRoot()
22+
],
1623
providers: [
1724
StatusBar,
1825
SplashScreen,
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
<ion-header>
22
<ion-toolbar>
3-
<ion-title>dashboard</ion-title>
3+
<ion-buttons slot="end">
4+
<ion-button (click)="logout()">
5+
<ion-icon slot="icon-only" name="log-out"></ion-icon>
6+
</ion-button>
7+
</ion-buttons>
8+
<ion-title>My Dashboard</ion-title>
49
</ion-toolbar>
510
</ion-header>
6-
11+
712
<ion-content padding>
8-
9-
</ion-content>
13+
14+
</ion-content>
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import { Component, OnInit } from '@angular/core';
22

3+
import { AuthenticationService } from '../../services/authentication.service';
4+
35
@Component({
46
selector: 'app-dashboard',
57
templateUrl: './dashboard.page.html',
68
styleUrls: ['./dashboard.page.scss'],
79
})
810
export class DashboardPage implements OnInit {
911

10-
constructor() { }
12+
constructor(private authenticationService: AuthenticationService) { }
1113

1214
ngOnInit() {
1315
}
1416

17+
logout() {
18+
this.authenticationService.logout();
19+
}
20+
1521
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { NgModule } from '@angular/core';
2-
import { CommonModule } from '@angular/common';
2+
import { Routes, RouterModule } from '@angular/router';
3+
4+
const routes: Routes = [
5+
{ path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardPageModule' }
6+
];
37

48
@NgModule({
5-
imports: [
6-
CommonModule
7-
],
9+
imports: [RouterModule.forChild(routes)],
10+
exports: [RouterModule],
811
declarations: []
912
})
1013
export class MemberRoutingModule { }
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<ion-header>
22
<ion-toolbar>
3-
<ion-title>login</ion-title>
3+
<ion-title>Login</ion-title>
44
</ion-toolbar>
55
</ion-header>
66

77
<ion-content padding>
8-
8+
<ion-button (click)="login()" expand="block">Login</ion-button>
9+
<ion-button expand="block" color="secondary" href="/register" routerDirection="forward">Register</ion-button>
910
</ion-content>

src/app/public/login/login.page.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import { Component, OnInit } from '@angular/core';
22

3+
import { AuthenticationService } from '../../services/authentication.service';
4+
35
@Component({
46
selector: 'app-login',
57
templateUrl: './login.page.html',
68
styleUrls: ['./login.page.scss'],
79
})
810
export class LoginPage implements OnInit {
911

10-
constructor() { }
12+
constructor(private authenticationService: AuthenticationService) { }
1113

1214
ngOnInit() {
1315
}
1416

17+
login() {
18+
this.authenticationService.login();
19+
}
20+
1521
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<ion-header>
22
<ion-toolbar>
3-
<ion-title>register</ion-title>
3+
<ion-buttons slot="start">
4+
<ion-back-button defaultHref="/login"></ion-back-button>
5+
</ion-buttons>
6+
<ion-title>Register</ion-title>
47
</ion-toolbar>
58
</ion-header>
6-
9+
710
<ion-content padding>
8-
11+
912
</ion-content>
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { Injectable } from '@angular/core';
2+
import { CanActivate } from '@angular/router';
3+
import { AuthenticationService } from './authentication.service';
24

35
@Injectable({
46
providedIn: 'root'
57
})
6-
export class AuthGuardService {
8+
export class AuthGuardService implements CanActivate {
79

8-
constructor() { }
10+
constructor(private authenticationService: AuthenticationService) { }
11+
12+
canActivate(): boolean {
13+
return this.authenticationService.isAuthenticated();
14+
}
915
}

0 commit comments

Comments
 (0)