Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49184

Authentificating API

$
0
0

@khalilben wrote:

Hi,

I actually would have a login form using an laravel api, once connected it returns JSON data

{
id: 1,
group_id: 1,
username: "khalilben",
password: "CHIFFRED",
email: "mail@gmail.com",
first_name: "Khalil",
last_name: "Ben",
avatar: "1.jpg",
active: 1,
login_attempt: 12,
last_login: "2017-11-16 11:03:08",
created_at: "2014-03-12 09:18:46",
updated_at: "2017-11-13 16:29:10",
reminder: "CHIFFRED",
activation: null,
remember_token: "CHIFFRED",
last_activity: 1510833728
}

What i want to do is once it success, it send me to a profil page with those informations below. And save the ID globally to use it after on another page…
And don’t know how to proceed.
Already having this code but don’t know if it’s good and how to use it correctly

import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';

//https://www.djamware.com/post/58c1703e80aca7585c808ec1/step-by-step-tutorial-building-ionic-2-rest-api-authentication


//Providers
import {Resources} from  '../resources';
let apiUrl =Resources.Constants.API.api_login;
@Injectable()
export class AuthService {

  constructor(public http: Http) {
    console.log('Hello AuthService Provider');
  }

  login(credentials) {
    return new Promise((resolve, reject) => {
      let headers = new Headers();
      headers.append('Content-Type', 'application/json');

      this.http.post(apiUrl + 'login', JSON.stringify(credentials), { headers: headers })
        .subscribe(res => {
          resolve(res.json());
        }, (err) => {
          reject(err);
        });
    });
  }

  postData(credentials, type) {
    return new Promise((resolve, reject) => {
      let headers = new Headers();

      this.http.post(apiUrl + type, JSON.stringify(credentials), { headers: headers })
        .subscribe(res => {
          resolve(res.json());
        }, (err) => {
          reject(err);
        });
    });
  }

  logout() {
    return new Promise((resolve, reject) => {
      let headers = new Headers();
      headers.append('X-Auth-Token', localStorage.getItem('token'));

      this.http.post(apiUrl + 'logout', {}, { headers: headers })
        .subscribe(res => {
          localStorage.clear();
        }, (err) => {
          reject(err);
        });
    });
  }
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 49184

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>