Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { AuthHttp, JwtHelper } from 'angular2-jwt';
- import {Http, RequestOptions, Headers, Response} from "@angular/http";
- import 'rxjs/add/operator/map';
- import {ConfigurationBase} from "../configuration/config.service";
- import {Observable} from "rxjs";
- import {IUser, IMessage, IRegisterUser, IErrorMessage} from "../models/models";
- @Injectable()
- export class AuthService {
- private apiEndpoint;
- private headers;
- private options;
- private _tokenData: any;
- constructor(private _config: ConfigurationBase,
- private _authHttp: AuthHttp,
- private _http: Http,
- private _jwtHelper: JwtHelper) {
- this.apiEndpoint = this._config.apiEndpoint;
- this.headers = new Headers({'Content-Type': 'application/json', 'X-Requested-With':'XMLHttpRequest' });
- this.options = new RequestOptions({ headers: this.headers });
- }
- login(email: string, password: string) {
- let body = JSON.stringify({ email: email, password: password });
- return this._http.post(this.apiEndpoint + "/user/signin", body , this.options)
- .map(res => res.json())
- .subscribe(
- data => {
- if(data.token != ""){
- this.token = data.token;
- }
- },
- error => {
- switch (error.status) {
- case 401:
- console.log("Invalid Credentials");
- break;
- default:
- console.log("An error occured. Please contact support.");
- }
- },
- () => {
- }
- );
- }
- private get token() {
- return sessionStorage.getItem(this._config.tokenName);
- }
- private set token(token: string) {
- sessionStorage.removeItem(this._config.tokenName);
- this._tokenData = null;
- if (token) {
- sessionStorage.setItem(this._config.tokenName, token);
- }
- }
- private get refresh_token(): string {
- let refresh_token: string;
- let token: string = this.token;
- if (token) {
- refresh_token = JSON.parse(token).refresh_token;
- }
- return refresh_token;
- }
- private get tokenData(): any {
- if (!this.token) {
- this._tokenData = this.token;
- }
- if (!this._tokenData && this.token) {
- this._tokenData = this._jwtHelper.decodeToken(this.token);
- }
- return this._tokenData;
- }
- getUserDetails(): Observable<IUser> {
- var url = this.apiEndpoint + "/user/signedin";
- console.log("gfsagfas");
- return this._authHttp.request(url, {
- method: "get"
- }).map(
- response => {
- var data = response.text();
- var status = response.status.toString();
- if (status === "200") {
- var result200: IUser = null;
- result200 = data === "" ? null : <IUser>JSON.parse(data);
- console.log(result200);
- return result200;
- }
- else {
- throw "error_no_callback_for_the_received_http_status";
- }
- }).catch((error) => this.catchError(error));
- }
- registerUser(request: IRegisterUser): Observable<IMessage> {
- var url = this.apiEndpoint + "/user/register";
- var content = JSON.stringify(request);
- return this._http.post(url, content, this.options).map(
- response => {
- var data = response.text();
- var status = response.status.toString();
- if (status === "200") {
- var result200: IMessage = null;
- result200 = data === "" ? null : <IMessage>JSON.parse(data);
- return result200;
- }
- else {
- throw "error_no_callback_for_the_received_http_status";
- }
- }).catch((error) => this.catchError(error));
- }
- updateUser(request: IRegisterUser): Observable<IMessage> {
- var url = this.apiEndpoint + "/user/update";
- var content = JSON.stringify(request);
- return this._authHttp.post(url, content, this.options).map(
- response => {
- var data = response.text();
- var status = response.status.toString();
- if (status === "200") {
- var result200: IMessage = null;
- result200 = data === "" ? null : <IMessage>JSON.parse(data);
- return result200;
- }
- else {
- throw "error_no_callback_for_the_received_http_status";
- }
- }).catch((error) => this.catchError(error));
- }
- signout(): Observable<IMessage>{
- var url = this.apiEndpoint + "/user/signout";
- return this._authHttp.post(url, null,this.options).map(
- response => {
- console.log(response);
- var data = response.text();
- var status = response.status.toString();
- if (status === "200") {
- var result200: IMessage = null;
- result200 = data === "" ? null : <IMessage>JSON.parse(data);
- this.proceedSignOut();
- return result200;
- }
- else {
- throw "error_no_callback_for_the_received_http_status";
- }
- }).catch((error) => this.catchError(error));
- }
- private proceedSignOut(){
- this.token = null;
- }
- private catchError(error: Response) {
- if (error instanceof Response) {
- console.log(error);
- return Observable.throw(JSON.parse(error.text()));
- }
- else {
- throw "error_no_callback_for_the_received_http_status";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement