How to properly append JWT token in request header in Angular 7 + Laravel 5.7
up vote
0
down vote
favorite
I have this in my UsersService in my Angular 7 app.
import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions, URLSearchParams } from '@angular/http';
import { ApiService } from '../../services/api.service';
@Injectable({
providedIn: 'root'
})
export class UsersService {
public headers: Headers;
public options: RequestOptions;
constructor(
private http: Http,
public api: ApiService
) {
this.headers = new Headers({
'Content-Type':'application/x-www-form-urlencoded',
'X-Token': localStorage.getItem('token')
});
this.options = new RequestOptions({ headers: this.headers });
}
getAll(){
return this.http.get(`${this.api.url}/api/users`, this.options);
}
}
This service will send HTTP request to my Laravel 5.7 app with endpoint /users
Laravel Routes api.php:
<?php
Route::group([
'middleware' => 'api'
// 'prefix' => 'auth'
], function ($router) {
/**
* Authentication API
*/
Route::post('login', 'AuthController@login');
Route::post('signup', 'AuthController@signup');
Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');
/**
* Users API
*/
Route::get('users', 'UsersController@index');
});
Laravel UsersController:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpControllersController;
use AppUser;
class UsersController extends Controller
{
public function __construct()
{
$this->middleware('auth:api');
}
/**
* Get all Users
*
*/
public function index()
{
$user = User::all();
return response()->json($user);
}
}
When trying call the said endpoint it returns an error:
message:"Unauthenticated."
How to properly append the JWT Token to send in the endpoint?
angular laravel rest http-headers jwt
add a comment |
up vote
0
down vote
favorite
I have this in my UsersService in my Angular 7 app.
import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions, URLSearchParams } from '@angular/http';
import { ApiService } from '../../services/api.service';
@Injectable({
providedIn: 'root'
})
export class UsersService {
public headers: Headers;
public options: RequestOptions;
constructor(
private http: Http,
public api: ApiService
) {
this.headers = new Headers({
'Content-Type':'application/x-www-form-urlencoded',
'X-Token': localStorage.getItem('token')
});
this.options = new RequestOptions({ headers: this.headers });
}
getAll(){
return this.http.get(`${this.api.url}/api/users`, this.options);
}
}
This service will send HTTP request to my Laravel 5.7 app with endpoint /users
Laravel Routes api.php:
<?php
Route::group([
'middleware' => 'api'
// 'prefix' => 'auth'
], function ($router) {
/**
* Authentication API
*/
Route::post('login', 'AuthController@login');
Route::post('signup', 'AuthController@signup');
Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');
/**
* Users API
*/
Route::get('users', 'UsersController@index');
});
Laravel UsersController:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpControllersController;
use AppUser;
class UsersController extends Controller
{
public function __construct()
{
$this->middleware('auth:api');
}
/**
* Get all Users
*
*/
public function index()
{
$user = User::all();
return response()->json($user);
}
}
When trying call the said endpoint it returns an error:
message:"Unauthenticated."
How to properly append the JWT Token to send in the endpoint?
angular laravel rest http-headers jwt
Need to pass'Authorization': 'Bearer ${token}'as a header, also
– Ohgodwhy
Nov 9 at 15:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have this in my UsersService in my Angular 7 app.
import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions, URLSearchParams } from '@angular/http';
import { ApiService } from '../../services/api.service';
@Injectable({
providedIn: 'root'
})
export class UsersService {
public headers: Headers;
public options: RequestOptions;
constructor(
private http: Http,
public api: ApiService
) {
this.headers = new Headers({
'Content-Type':'application/x-www-form-urlencoded',
'X-Token': localStorage.getItem('token')
});
this.options = new RequestOptions({ headers: this.headers });
}
getAll(){
return this.http.get(`${this.api.url}/api/users`, this.options);
}
}
This service will send HTTP request to my Laravel 5.7 app with endpoint /users
Laravel Routes api.php:
<?php
Route::group([
'middleware' => 'api'
// 'prefix' => 'auth'
], function ($router) {
/**
* Authentication API
*/
Route::post('login', 'AuthController@login');
Route::post('signup', 'AuthController@signup');
Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');
/**
* Users API
*/
Route::get('users', 'UsersController@index');
});
Laravel UsersController:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpControllersController;
use AppUser;
class UsersController extends Controller
{
public function __construct()
{
$this->middleware('auth:api');
}
/**
* Get all Users
*
*/
public function index()
{
$user = User::all();
return response()->json($user);
}
}
When trying call the said endpoint it returns an error:
message:"Unauthenticated."
How to properly append the JWT Token to send in the endpoint?
angular laravel rest http-headers jwt
I have this in my UsersService in my Angular 7 app.
import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions, URLSearchParams } from '@angular/http';
import { ApiService } from '../../services/api.service';
@Injectable({
providedIn: 'root'
})
export class UsersService {
public headers: Headers;
public options: RequestOptions;
constructor(
private http: Http,
public api: ApiService
) {
this.headers = new Headers({
'Content-Type':'application/x-www-form-urlencoded',
'X-Token': localStorage.getItem('token')
});
this.options = new RequestOptions({ headers: this.headers });
}
getAll(){
return this.http.get(`${this.api.url}/api/users`, this.options);
}
}
This service will send HTTP request to my Laravel 5.7 app with endpoint /users
Laravel Routes api.php:
<?php
Route::group([
'middleware' => 'api'
// 'prefix' => 'auth'
], function ($router) {
/**
* Authentication API
*/
Route::post('login', 'AuthController@login');
Route::post('signup', 'AuthController@signup');
Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');
/**
* Users API
*/
Route::get('users', 'UsersController@index');
});
Laravel UsersController:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpControllersController;
use AppUser;
class UsersController extends Controller
{
public function __construct()
{
$this->middleware('auth:api');
}
/**
* Get all Users
*
*/
public function index()
{
$user = User::all();
return response()->json($user);
}
}
When trying call the said endpoint it returns an error:
message:"Unauthenticated."
How to properly append the JWT Token to send in the endpoint?
angular laravel rest http-headers jwt
angular laravel rest http-headers jwt
edited Nov 9 at 16:48
georgeawg
32k104865
32k104865
asked Nov 9 at 15:03
Jay Marz
54231833
54231833
Need to pass'Authorization': 'Bearer ${token}'as a header, also
– Ohgodwhy
Nov 9 at 15:13
add a comment |
Need to pass'Authorization': 'Bearer ${token}'as a header, also
– Ohgodwhy
Nov 9 at 15:13
Need to pass
'Authorization': 'Bearer ${token}' as a header, also– Ohgodwhy
Nov 9 at 15:13
Need to pass
'Authorization': 'Bearer ${token}' as a header, also– Ohgodwhy
Nov 9 at 15:13
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
First import HttpHeaders from
import { HttpClient, HttpHeaders,HttpResponse } from '@angular/common/http';
private token=localStorage.getItem('token');
Now create http headeer in the following way
private httpOptions = {
headers: new HttpHeaders({'Authorization': 'Bearer ' +this.token ,'Content-Type':'application/json', 'X-Requested-With':'XMLHttpRequest'})
};
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
First import HttpHeaders from
import { HttpClient, HttpHeaders,HttpResponse } from '@angular/common/http';
private token=localStorage.getItem('token');
Now create http headeer in the following way
private httpOptions = {
headers: new HttpHeaders({'Authorization': 'Bearer ' +this.token ,'Content-Type':'application/json', 'X-Requested-With':'XMLHttpRequest'})
};
add a comment |
up vote
0
down vote
First import HttpHeaders from
import { HttpClient, HttpHeaders,HttpResponse } from '@angular/common/http';
private token=localStorage.getItem('token');
Now create http headeer in the following way
private httpOptions = {
headers: new HttpHeaders({'Authorization': 'Bearer ' +this.token ,'Content-Type':'application/json', 'X-Requested-With':'XMLHttpRequest'})
};
add a comment |
up vote
0
down vote
up vote
0
down vote
First import HttpHeaders from
import { HttpClient, HttpHeaders,HttpResponse } from '@angular/common/http';
private token=localStorage.getItem('token');
Now create http headeer in the following way
private httpOptions = {
headers: new HttpHeaders({'Authorization': 'Bearer ' +this.token ,'Content-Type':'application/json', 'X-Requested-With':'XMLHttpRequest'})
};
First import HttpHeaders from
import { HttpClient, HttpHeaders,HttpResponse } from '@angular/common/http';
private token=localStorage.getItem('token');
Now create http headeer in the following way
private httpOptions = {
headers: new HttpHeaders({'Authorization': 'Bearer ' +this.token ,'Content-Type':'application/json', 'X-Requested-With':'XMLHttpRequest'})
};
answered Nov 9 at 15:15
Ali
332213
332213
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228222%2fhow-to-properly-append-jwt-token-in-request-header-in-angular-7-laravel-5-7%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Need to pass
'Authorization': 'Bearer ${token}'as a header, also– Ohgodwhy
Nov 9 at 15:13