added basic AccountService

This commit is contained in:
2020-07-21 08:45:40 +02:00
parent 886ab08b1f
commit 11af9c5d0d
4 changed files with 61 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { AccountService } from './account.service';
describe('AccountService', () => {
let service: AccountService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(AccountService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,17 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class AccountService {
userName: string = null;
constructor(private httpClient: HttpClient) { }
login(username, password) {
return this.httpClient.post<User>
}
}

View File

@@ -0,0 +1,7 @@
import { Auth.Guard } from './auth.guard';
describe('Auth.Guard', () => {
it('should create an instance', () => {
expect(new Auth.Guard()).toBeTruthy();
});
});

View File

@@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AccountService } from './account.service';
@Injectable({providedIn: 'root'})
export class AuthGuard implements CanActivate {
constructor(private router: Router,
private accountService: AccountService) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
const user = this.accountService.userName;
if (user) {
return true;
}
this.router.navigate(['/login'], {queryParams: {returnURL: state.url}});
return false;
}
}