added basic AccountService
This commit is contained in:
16
src/app/account/account.service.spec.ts
Normal file
16
src/app/account/account.service.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
17
src/app/account/account.service.ts
Normal file
17
src/app/account/account.service.ts
Normal 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>
|
||||
}
|
||||
|
||||
}
|
||||
7
src/app/account/auth.guard.spec.ts
Normal file
7
src/app/account/auth.guard.spec.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Auth.Guard } from './auth.guard';
|
||||
|
||||
describe('Auth.Guard', () => {
|
||||
it('should create an instance', () => {
|
||||
expect(new Auth.Guard()).toBeTruthy();
|
||||
});
|
||||
});
|
||||
21
src/app/account/auth.guard.ts
Normal file
21
src/app/account/auth.guard.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user