SHARE
TWEET

Untitled

a guest Jan 26th, 2019 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Angular */
  2. import { Injectable } from '@angular/core';
  3. import { NavController, LoadingController, AlertController, Events } from 'ionic-angular';
  4. import { BackgroundMode } from '@ionic-native/background-mode';
  5. import { Http, RequestOptions, Headers } from '@angular/http';
  6. import { UrlBase } from '../models/UrlBase';
  7. import { Storage } from '@ionic/storage';
  8.  
  9. @Injectable()
  10. export class AuthService {
  11.  
  12.     header = new Headers();
  13.     options = new RequestOptions();
  14.     public token = "";
  15.    
  16.     constructor(private events : Events, private storage : Storage, private http : Http,  private backgroundMode: BackgroundMode ) {
  17.         this.header.append('Content-Type', 'application/x-www-form-urlencoded');
  18.         this.options = new RequestOptions({
  19.         headers: this.header,
  20.         });
  21.  
  22.     }
  23.  
  24.     verifyConnection(url){
  25.         return this.http.get("http://" + url + ":8085/api/Core/VerifyConnectionPsiu");
  26.     };
  27.  
  28.     authenticate(request, url){
  29.         let urlFull = "http://" + url + ':8085/token';
  30.         let data = "grant_type=password&username=" + request.Username + "&password=" + request.Password;
  31.         return this.http.post(urlFull, data, this.options);
  32.     }
  33.  
  34.     public userIsLogged(){
  35.         return this.authInfo().then((data)=>{
  36.           if(data !== undefined && data !== null){
  37.               this.setToken(data.token);
  38.             return true;
  39.           }else{
  40.             return false;
  41.           }
  42.         });
  43.     }
  44.  
  45.     public authInfo(){
  46.         return this.storage.get('authInfo');
  47.     }
  48.  
  49.     public logout(){
  50.         return this.storage.remove('authInfo').then(()=>{
  51.             this.events.publish('logout');
  52.         });
  53.     }
  54.  
  55.     public setToken(token){
  56.         this.token = token;
  57.     }
  58.  
  59.  
  60.    
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top