Skip to content

Commit 72bd38b

Browse files
committed
AuthService and guard changes
1 parent d1a1ef1 commit 72bd38b

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

.angular-cli.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"styles.css"
2525
],
2626
"scripts": [
27-
"../node_modules/jquery/dist/jquery.min.js"
27+
"../node_modules/jquery/dist/jquery.min.js",
28+
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
2829
],
2930
"environmentSource": "environments/environment.ts",
3031
"environments": {

src/app/auth/auth.guard.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@ import { Injectable } from '@angular/core';
22
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
33
import { Observable } from 'rxjs/Observable';
44

5+
import { AuthService } from './auth.service';
6+
import { Router } from '@angular/router';
7+
58
@Injectable()
69
export class AuthGuard implements CanActivate {
10+
11+
constructor(
12+
private authService: AuthService,
13+
private router: Router
14+
) {}
15+
716
canActivate(
817
next: ActivatedRouteSnapshot,
918
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
19+
20+
if (!this.authService.isLoggedIn) {
21+
this.router.navigate(['/home']);
22+
return false;
23+
}
24+
1025
return true;
1126
}
1227
}

src/app/auth/auth.service.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
import { Injectable } from '@angular/core';
2+
import { environment } from './../../environments/environment';
3+
import { Router } from '@angular/router';
24

35
@Injectable()
46
export class AuthService {
57

6-
constructor() { }
8+
authenticated: boolean;
79

10+
constructor(private router: Router) {
11+
this.checkIfLoggedin();
12+
}
13+
14+
//let options = new RequestOptions({ headers: headers, withCredentials: true });
15+
//this.http.post(this.connectUrl, <stringified_data> , options)
16+
17+
checkIfLoggedin(){
18+
//ask api
19+
//to-do use environment.apiUrl+'/isLoggedIn' returns yes, no
20+
this.authenticated = false;
21+
}
22+
23+
get isLoggedIn(): boolean {
24+
// Check if current date is greater
25+
// than expiration and user is logged in
26+
//const expiresAt = JSON.parse(localStorage.getItem('expires_at'));
27+
//return Date.now() < expiresAt && this.authenticated;
28+
return this.authenticated;
29+
}
830
}

0 commit comments

Comments
 (0)