Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { HttpClient, HttpHeaders } from '@angular/common/http';
- import { Observable } from 'rxjs';
- import { Storage } from '@ionic/storage';
- import 'rxjs/add/operator/map'
- const httpOptions = {
- headers: new HttpHeaders({
- 'Content-Type' : 'application/json',
- 'Authorization' : 'Basic ' + btoa('user'+':'+'pass')
- })
- };
- @Injectable()
- export class AuthenticationService {
- public token: string;
- constructor(private http: HttpClient, private _storage: Storage) {
- _storage.get('currentUser')
- .then(res => {
- let currentUser = res;
- this.token = currentUser && currentUser.token;
- })
- }
- login(municipio, username: string, password: string): Observable<boolean> {
- return this.http.post('http://localhost:8080/datasnap/rest/TService/Login/',
- { ibge: municipio.ibge, username: username, password: password },
- httpOptions)
- .map(res => {
- let token = res && res['token'];
- if (token) {
- this.token = token;
- this._storage.set('currentUser', { username: username, municipio: municipio, token: token });
- return true;
- } else {
- return false;
- }
- });
- }
- logout(): void {
- this.token = null;
- this._storage.remove('currentUser');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement