Skip to content

Commit

Permalink
Submitting the login form results in acquiring a token from the API.
Browse files Browse the repository at this point in the history
  • Loading branch information
little-pinecone committed Jan 8, 2019
1 parent c9be87c commit 71b1338
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 2 deletions.
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Credentials } from '../../credentials'; import { Credentials } from '../../credentials';
import { AuthService } from 'src/app/auth/services/auth.service';


@Component({ @Component({
selector: 'app-login', selector: 'app-login',
Expand All @@ -10,12 +11,12 @@ export class LoginComponent implements OnInit {


credentials: Credentials = new Credentials('', ''); credentials: Credentials = new Credentials('', '');


constructor() { } constructor(private authService: AuthService) { }


ngOnInit() { ngOnInit() {
} }


public login(): void { public login(): void {
this.authService.login(this.credentials);
} }

} }
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,44 @@
import { TestBed, getTestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';

import { AuthService } from './auth.service';
import { TokenService } from './token.service';
import { Credentials } from '../credentials';
import { Observable } from 'rxjs';

describe('AuthService', () => {
let injector: TestBed;
let service: AuthService;
let tokenService: TokenService;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
AuthService
],
imports: [
RouterTestingModule,
HttpClientTestingModule
]
});
injector = getTestBed();
service = injector.get(AuthService);
tokenService = injector.get(TokenService);
});

it('should be created', () => {
const service: AuthService = TestBed.get(AuthService);
expect(service).toBeTruthy();
});

it('should have redirectToUrl option set to "cookies" by default', () => {
expect(service.redirectToUrl).toEqual('/cookies');
});

it('should call token service for ResponseHeaders when logging in a user', () => {
spyOn(tokenService, 'getResponseHeaders').and.returnValue(new Observable<string>());
service.login(new Credentials('',''));
expect(tokenService.getResponseHeaders).toHaveBeenCalled();
});
});
33 changes: 33 additions & 0 deletions frontend/src/main/angular/src/app/auth/services/auth.service.ts
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Injectable } from '@angular/core';

import { TokenService } from './token.service'
import { Credentials } from '../credentials';
import { HttpResponse } from '@angular/common/http';
import { Router } from '@angular/router';

@Injectable({
providedIn: 'root'
})
export class AuthService {

static readonly TOKEN_STORAGE_KEY = 'token';
redirectToUrl: string = '/cookies';

constructor(private router: Router, private tokenService: TokenService) { }

public login(credentials: Credentials): void {
this.tokenService.getResponseHeaders(credentials)
.subscribe((res: HttpResponse<any>) => {
this.saveToken(res.headers.get('authorization'));
this.router.navigate([this.redirectToUrl]);
});
}

private saveToken(token: string){
localStorage.setItem(AuthService.TOKEN_STORAGE_KEY, token);
}

public getToken(): string {
return localStorage.getItem(AuthService.TOKEN_STORAGE_KEY);
}
}
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,54 @@
import { TestBed, getTestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';

import { TokenService } from './token.service';
import { environment } from '../../../environments/environment';
import { HttpHeaders, HttpResponse } from '@angular/common/http';
import { Credentials } from '../credentials';

describe('TokenService', () => {
let injector: TestBed;
let service: TokenService;
let httpMock: HttpTestingController;
let apiUrl = environment.apiUrl;
let HttpResponseMock: HttpResponse<any> = {
body: null,
type: null,
clone:null,
status: 200,
statusText: null,
url: null,
ok: true,
headers: new HttpHeaders({ 'authorization': 'bearer token' })
};

beforeEach(() => {
TestBed.configureTestingModule({
providers: [TokenService],
imports: [ HttpClientTestingModule ]
});
injector = getTestBed();
service = injector.get(TokenService);
httpMock = injector.get(HttpTestingController);
});

afterEach(() => {
httpMock.verify();
});

it('should be created', () => {
const service: TokenService = TestBed.get(TokenService);
expect(service).toBeTruthy();
});

it('should call login endpoint', () => {
service.getResponseHeaders(new Credentials('', ''))
.subscribe((res: HttpResponse<any>) => {
expect(res.headers.get('authorization')).toEqual('bearer token');
});

const req = httpMock.expectOne(apiUrl + '/login');
expect(req.request.method).toBe("POST");
req.flush({}, HttpResponseMock);
});
});
24 changes: 24 additions & 0 deletions frontend/src/main/angular/src/app/auth/services/token.service.ts
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

import { environment } from '../../../environments/environment';
import { Credentials } from '../credentials';

const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
observe: 'response' as 'response'
};
const API_URL = environment.apiUrl;

@Injectable({
providedIn: 'root'
})
export class TokenService {

constructor(private http: HttpClient) { }

public getResponseHeaders(credentials: Credentials) {
let loginUrl = API_URL + '/login';
return this.http.post(loginUrl, credentials, httpOptions);
}
}

0 comments on commit 71b1338

Please sign in to comment.