Skip to content

Commit

Permalink
AuthService and guard changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kotsis committed Apr 6, 2018
1 parent d1a1ef1 commit 72bd38b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .angular-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"styles.css"
],
"scripts": [
"../node_modules/jquery/dist/jquery.min.js"
"../node_modules/jquery/dist/jquery.min.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
],
"environmentSource": "environments/environment.ts",
"environments": {
Expand Down
15 changes: 15 additions & 0 deletions src/app/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@ import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';

import { AuthService } from './auth.service';
import { Router } from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {

constructor(
private authService: AuthService,
private router: Router
) {}

canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {

if (!this.authService.isLoggedIn) {
this.router.navigate(['/home']);
return false;
}

return true;
}
}
24 changes: 23 additions & 1 deletion src/app/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import { Injectable } from '@angular/core';
import { environment } from './../../environments/environment';
import { Router } from '@angular/router';

@Injectable()
export class AuthService {

constructor() { }
authenticated: boolean;

constructor(private router: Router) {
this.checkIfLoggedin();
}

//let options = new RequestOptions({ headers: headers, withCredentials: true });
//this.http.post(this.connectUrl, <stringified_data> , options)

checkIfLoggedin(){
//ask api
//to-do use environment.apiUrl+'/isLoggedIn' returns yes, no
this.authenticated = false;
}

get isLoggedIn(): boolean {
// Check if current date is greater
// than expiration and user is logged in
//const expiresAt = JSON.parse(localStorage.getItem('expires_at'));
//return Date.now() < expiresAt && this.authenticated;
return this.authenticated;
}
}

0 comments on commit 72bd38b

Please sign in to comment.