Skip to content
Permalink
Browse files

Submitting the login form results in acquiring a token from the API.

  • Loading branch information
little-pinecone committed Jan 8, 2019
1 parent c9be87c commit 71b13383b4b77819234b854e97fec419c60bc450
@@ -1,5 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { Credentials } from '../../credentials';
import { AuthService } from 'src/app/auth/services/auth.service';

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

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

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

ngOnInit() {
}

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

}
@@ -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();
});
});
@@ -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);
}
}
@@ -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);
});
});
@@ -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.
You can’t perform that action at this time.