ayapapaya

import {Injectable} from '@angular/core'; import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import {Http, Headers, RequestOptions} from '@angular/http'; /** * User Model */ export class User { public firma: string; public username: string; public password: string; /** * Konstruktor */ constructor(firma: string, username: string, password: string) { this.firma = firma; this.username = username; this.password = password; } } /** * Authorization Service */ @Injectable() export class AuthService { public currentUser: User; // why no boolean ? public access: string; public url: string = "http://127.0.0.1:8888/gebman/api/index.cfm?endpoint=/authenticate"; /** * Konstruktor */ constructor(private http: Http) { } /** * user login * * @param userCredentials * @returns {any} */ public login(userCredentials: any): any { // haven't checked if && || && is right if (userCredentials.username && !userCredentials.username.trim() || userCredentials.password && !userCredentials.password.trim()) { return new Error("Keine Daten"); } else { let body: any = JSON.stringify({ username: userCredentials.username, password: userCredentials.password }); let options: RequestOptions = this.getRequestOptions(); this.getDataFromGebman(body, options) .subscribe(response => { this.access = response.ACCESS; localStorage.userId = response.ACCESS; if (response.ACCESS == "true") { console.log("has acces"); // redirect or what else you doing. } }, error => { console.log(error); // just don't redirect, maybe throw an event ? }); } } /** * http login call * * @param body * @param options * @returns {Observable<any>} */ public getDataFromGebman(body, options): Observable<any> { return this.http.post(this.url, body, options) .map(response => response.json()); } /** * http call default header * @returns {any} */ public getRequestOptions(): RequestOptions { var headers = new Headers(); headers.append("Accept", 'application/json'); headers.append('Content-Type', 'application/json'); let options = new RequestOptions({headers: headers}); return options; } /** * user logout */ public logout(): void { this.currentUser = null; // how about some localstorage / cookies ? } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.