Skip to content

Commit 00b0bf2

Browse files
committed
up
1 parent 44a95bc commit 00b0bf2

File tree

10 files changed

+86
-60
lines changed

10 files changed

+86
-60
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"@angular/platform-browser-dynamic": "^5.2.0",
2424
"@angular/router": "^5.2.0",
2525
"angular-highcharts": "^5.2.13",
26+
"angularfire": "^2.3.0",
2627
"core-js": "^2.4.1",
28+
"firebase": "^5.3.1",
2729
"highcharts": "^6.1.1",
2830
"moment": "^2.22.2",
2931
"ngx-bootstrap": "^2.0.5",

src/app/AlwaysAuthGuard .ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,41 @@
11
import { CanActivate, Router } from '@angular/router';
22
import { Injectable } from '@angular/core';
3+
import { StorageService } from './Providers/storageservice';
4+
import { StorageKey } from './shared/enums/storagekey';
5+
import { ToastrService } from 'ngx-toastr';
36

47
@Injectable()
58
export class AlwaysAuthGuard implements CanActivate {
6-
myvalue = true;
79
constructor(
8-
910
private router: Router,
11+
private storage: StorageService
1012
) { }
11-
canActivate() {
12-
// this.myvalue = true;
1313

14-
// if (this.myvalue) {
15-
// return false;
16-
// } else {
17-
// // window.alert("You don't have permission to view this page");
18-
// // this.router.navigate(['/sign-in']);
19-
// return true;
20-
// }
21-
return true;
14+
canActivate() {
15+
let isLoggedIn = this.storage.getPropertyFromLS(StorageKey.IsLoggedIn);
16+
if(!isLoggedIn){
17+
return true;
18+
}
19+
return false;
2220
}
2321
}
2422

2523

2624
@Injectable()
2725
export class OnlyLoggedInUsersGuard implements CanActivate {
2826
constructor(
29-
3027
private router: Router,
28+
private storage: StorageService,
29+
private toastr: ToastrService
3130
) { }
3231
myvalue = true;
3332
canActivate() {
3433

35-
if (this.myvalue) {
36-
// window.alert('Access');
37-
// this.router.navigate(['/private/home']);
34+
let isLoggedIn = this.storage.getPropertyFromLS(StorageKey.IsLoggedIn);
35+
if(isLoggedIn){
3836
return true;
39-
} else {
40-
// window.alert('You dont have permission to view this page');
41-
this.router.navigate(['/sign-in']);
42-
return false;
4337
}
38+
this.toastr.info("You are not logged in","Info!");
39+
return false;
4440
}
4541
}

src/app/Providers/storageservice.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ export class StorageService {
88
}
99

1010
getPropertyFromLS(key) {
11-
JSON.stringify(window.localStorage.getItem(key));
11+
return JSON.parse(window.localStorage.getItem(key));
1212
}
1313

1414
setPropertyFromLS(key, value) {
15-
window.localStorage.setItem(key, JSON.parse(value));
15+
window.localStorage.setItem(key, JSON.stringify(value));
1616
}
1717

1818
removePropertyFromLS(key){
1919
window.localStorage.removeItem(key);
20+
return true;
2021
}
2122

2223

src/app/app.component.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { ToastrService } from 'ngx-toastr';
22
import { Component } from '@angular/core';
33
import { setTheme } from 'ngx-bootstrap/utils';
4+
import { StorageService } from './Providers/storageservice';
5+
import { StorageKey } from './shared/enums/storagekey';
6+
import { Router } from '@angular/router';
47

58
@Component({
69
selector: 'app-root',
@@ -9,13 +12,22 @@ import { setTheme } from 'ngx-bootstrap/utils';
912
})
1013
export class AppComponent {
1114
title = 'app';
12-
constructor(private toastrService: ToastrService) {
13-
this.addSingle();
15+
constructor(
16+
private toastr: ToastrService,
17+
private storage: StorageService,
18+
private router: Router
19+
) {
1420
setTheme('bs3');
15-
setTimeout(() => {
16-
this.toastrService.success('Hello world!', 'Toastr fun!', { closeButton: true });
17-
}, 1000);
21+
this.redirection()
1822
}
19-
addSingle() {
23+
24+
redirection() {
25+
let isLoggedIn = this.storage.getPropertyFromLS(StorageKey.IsLoggedIn);
26+
if (isLoggedIn) {
27+
this.router.navigate(['/user/home']);
28+
} else {
29+
this.router.navigate(['/sign-in']);
30+
}
2031
}
32+
2133
}

src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { HTTPService } from './Providers/httpservice';
1212
import { PrivateModule } from './private/private.module';
1313
import { AlwaysAuthGuard, OnlyLoggedInUsersGuard } from './AlwaysAuthGuard ';
1414
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
15+
import { StorageService } from './Providers/storageservice';
1516
@NgModule({
1617
declarations: [
1718
AppComponent,
@@ -34,6 +35,7 @@ import { LocationStrategy, HashLocationStrategy } from '@angular/common';
3435
providers: [
3536
HttpClient,
3637
HTTPService,
38+
StorageService,
3739
AlwaysAuthGuard,
3840
OnlyLoggedInUsersGuard,
3941
{ provide: LocationStrategy, useClass: HashLocationStrategy }

src/app/private/header/header.component.html

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
<!-- <header>
2-
<div class="row">
3-
<div class="col-md-4">
4-
<h2><i class="fas fa-home"></i> Admin Panel</h2>
5-
</div>
6-
<div class="col-md-1">
7-
<a href="#">Home</a>
8-
</div>
9-
<div class="col-md-1">
10-
<a href="#">Link2</a>
11-
</div>
12-
<div class="col-md-6"></div>
13-
</div>
14-
</header> -->
15-
161
<nav class="navbar navbar-inverse">
172
<div class="container-fluid">
183
<div class="navbar-header">
@@ -29,25 +14,25 @@ <h2><i class="fas fa-home"></i> Admin Panel</h2>
2914
<div class="collapse navbar-collapse" id="myNavbar">
3015
<ul class="nav navbar-nav">
3116
<li [routerLinkActive]="['active']" [routerLink]="['/user/home']">
32-
<a>Grid</a>
17+
<a href="javascript:;">Grid</a>
3318
</li>
3419
<li [routerLinkActive]="['active']" [routerLink]="['/user/demo-charts']">
35-
<a>Charts</a>
20+
<a href="javascript:;">Charts</a>
3621
</li>
3722
<li [routerLinkActive]="['active']" [routerLink]="['/user/gallery']">
38-
<a>Media</a>
23+
<a href="javascript:;">Media</a>
3924
</li>
4025
<li [routerLinkActive]="['active']" [routerLink]="['/user/about']">
41-
<a>Comming soon</a>
26+
<a href="javascript:;">Comming soon</a>
4227
</li>
4328
<li>
4429
<a href="javascript:;">Contact</a>
4530
</li>
4631
</ul>
4732
<ul class="nav navbar-nav navbar-right">
4833
<li>
49-
<a href="#">
50-
<span class="glyphicon glyphicon-log-in"></span> Login</a>
34+
<a href="javascript:;" (click)="logout()">
35+
<span class="glyphicon glyphicon-log-out"></span> Logout</a>
5136
</li>
5237
</ul>
5338
</div>
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { StorageService } from '../../Providers/storageservice';
3+
import { StorageKey } from '../../shared/enums/storagekey';
4+
import { Router } from '@angular/router';
25

36
@Component({
47
selector: 'app-private-header',
@@ -7,9 +10,17 @@ import { Component, OnInit } from '@angular/core';
710
})
811
export class PrivateHeaderComponent implements OnInit {
912

10-
constructor() { }
13+
constructor(
14+
private storage:StorageService,
15+
private router: Router
16+
) { }
1117

1218
ngOnInit() {
1319
}
1420

21+
logout(){
22+
this.storage.removePropertyFromLS(StorageKey.IsLoggedIn);
23+
this.router.navigate(["/sign-in"]);
24+
}
25+
1526
}

src/app/public/signin/signin.component.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { ToastrService } from 'ngx-toastr';
33
import { Component, OnInit } from '@angular/core';
44
import { User } from '../../shared/models';
55
import { FormGroup, FormBuilder, Validators } from '../../../../node_modules/@angular/forms';
6+
import { StorageService } from '../../Providers/storageservice';
7+
import { StorageKey } from '../../shared/enums/storagekey';
68

79
@Component({
810
selector: 'app-signin',
@@ -15,10 +17,11 @@ export class PublicSigninComponent implements OnInit {
1517

1618
constructor(
1719
private formBuilder: FormBuilder,
18-
private toastrService:ToastrService,
19-
private router: Router
20+
private router: Router,
21+
private toastrService: ToastrService,
22+
private storage: StorageService
2023
) {
21-
this.signInForm = formBuilder.group({
24+
this.signInForm = this.formBuilder.group({
2225
username: ['', Validators.required],
2326
password: ['', Validators.required],
2427
});
@@ -27,14 +30,15 @@ export class PublicSigninComponent implements OnInit {
2730
ngOnInit() {
2831
}
2932

30-
onSubmit(){
31-
let signInData:User = this.signInForm.value;
32-
if(signInData.username.toLowerCase() === 'admin' && signInData.password.toLowerCase() === 'admin' ){
33+
onSubmit() {
34+
let signInData: User = this.signInForm.value;
35+
if (signInData.username.toLowerCase() === 'admin' && signInData.password.toLowerCase() === 'admin') {
36+
this.storage.setPropertyFromLS(StorageKey.IsLoggedIn,true);
3337
this.router.navigate(['user/home']);
34-
}else{
38+
} else {
3539
this.toastrService.error('Username or password is wrong', 'Error!', { closeButton: true });
36-
}
37-
40+
}
41+
3842
}
3943

4044
}

src/app/shared/enums/storagekey.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export enum StorageKey{
2-
'IsLoogedIn' = 0
2+
'IsLoggedIn' = 0
33
}

src/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@
1919

2020
<meta name="viewport" content="width=device-width, initial-scale=1">
2121
<link rel="icon" type="image/x-icon" href="favicon.ico">
22+
<script src="https://www.gstatic.com/firebasejs/5.3.1/firebase.js"></script>
23+
<script>
24+
// Initialize Firebase
25+
var config = {
26+
apiKey: "AIzaSyC8V_b4lKT8tqZsn3KIwvCLxmuLzgFE5zA",
27+
authDomain: "testpushnotification-17d04.firebaseapp.com",
28+
databaseURL: "https://testpushnotification-17d04.firebaseio.com",
29+
projectId: "testpushnotification-17d04",
30+
storageBucket: "testpushnotification-17d04.appspot.com",
31+
messagingSenderId: "336489112481"
32+
};
33+
firebase.initializeApp(config);
34+
</script>
2235
</head>
2336
<body style="margin:0%">
2437
<app-root></app-root>

0 commit comments

Comments
 (0)