How to use the user ID logged in angular2 and JWT?

0

I have this code in my service which is linked to a query on the server , the query uses a ID relative, this ID would be that of the logged-in user, but I do not know how to call the service and be able to use it.

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';

import { MisCursos } from './miscursos';
import { LoginPage } from '../pages/login/login';

//import { AuthService } from '../services/auth.service';


import 'rxjs/add/operator/map';
import 'rxjs/add/operator/first';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

@Injectable()
export class MisCursosService {
	private options;
	private url = 'http://localhost:8000/curso_x_usuario';

	constructor(
		private http: Http){
		let token = localStorage.getItem('token');
		let headers = new Headers({
			'Content-Type':'application/json',
			'Authorization':'Bearer ' + token
		});
	}

	getMisCursos():Observable<MisCursos[]>{
		let url = '${this.url}/{id}'; //id realtivo/fijo
		return this.http.get(url, this.options)
			.map(r => r.json())
			.catch(this.handleError);

	}

	private handleError(error: Response | any){
		let errMsg: string;
		if(error instanceof Response){
			let body = error.json() || '';
			let err = body.error || JSON.stringify(body);
			errMsg = '${error.status} - ${error.statusText || ''} ${err}'; 
		}else{
			errMsg = error.message ? error.message : error.toString();
		}
		return Observable.throw(errMsg);
	}
}

as you can see in this part:

"let url = '${this.url}/{id}';"

I need to change the {id} to the logged user ID, in fact I already tried it using a fixed ID and it works.

Thanks for your help, I am still very new with this of angular and Ionic. If you need more information, you can ask for it.

    
asked by Maikol Garcia 05.06.2017 в 18:26
source

0 answers