Laravel Passport 401 Unauthorized Error Using Apache and Vue

I am trying to connect to generate the laravel user API using the vue and laravel passports, but I continue to receive an authorization error in my headers. This code ismy

<script> import Hello from './components/Hello' export default { name: 'app', components: { Hello }, created () { const postData = { grant_type: 'password', client_id: 2, client_secret: 'sXdg5nOO4UU2muiHaQnTq4hDQjyj17Kd9AeKuNEx', username: ' robertrutenge@gmail.com ', password: 'password', scope: '' } this.$http.post('http://localhost:8000/oauth/token', postData) .then(response => { console.log(response) const header = { 'Accept': 'application/json', 'Authorization': ~'Bearer ' + response.body.access_token } this.$http.get('http://localhost:8000/api/user', {headers: header}) .then(response => { console.log(response) }) }) } } </script> 

I did a research, and most of the answers suggest changing the apache configuration file or the .htaccess file, but this also doesn’t work on my part. Any help would be appreciated :-)

0
source share
3 answers

this is not a problem in vue.js or larvel. I moved my l Larvel API from Apache to nginx, then it worked fine. I updated my middleware handler like this. then we work fine on the Apache server

  $origin = $request->server()['HTTP_ORIGIN']; if(in_array($origin, $url)){ header('Access-Control-Allow-Origin: '. $origin); header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Csrf-Token'); } 
+1
source

I think that if your vue application and laravel application are combined, you can use your api without any authorization header, which you just need to send X-CSRF-TOKEN and send this token with every request, you do not need to send authorization check here

https://laravel.com/docs/5.3/passport#consuming-your-api-with-javascript

+1
source

solve (2) go to AuthServiceProvider.php

keypoint: set token expiration

Passport :: tokensExpireIn (Carbon :: now () β†’ addDays (1));

You must set the validity period of the token cannot be infinite

0
source

Source: https://habr.com/ru/post/1013915/


All Articles