Answer the question
In order to leave comments, you need to log in
Laravel + Vue + JWT: how to add Authorization header?
Good afternoon!
Yesterday I decided to connect to Laravel JWT and rewrite the authorization. Prior to this, native authorization was used out of the box.
So, there are routes:
Route::get('/', ['uses' => '[email protected]']);
Route::post('/login', ['uses' => '[email protected]']);
Route::get('logout', '[email protected]');
Route::get('/ap', ['uses' => '[email protected]']);
export default {
data () {
return {
login: '',
password: ''
}
},
methods: {
auth () {
axios.post('/api/login', { login: this.login, password: this.password })
.then(function (response) {
if (response.data.success) {
Cookies.set('Token', 'Bearer ' + response.data.data.token);
axios.defaults.headers.common['Authorization'] = 'Bearer ' + response.data.data.token;
console.log('Токен выдан')
}
console.log(response.data.data.token);
})
}
}
}
try {
if (! $user = JWTAuth::parseToken()->authenticate()) {
return response()->json(['user_not_found'], 404);
}
} catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
return response()->json(['token_expired'], $e->getStatusCode());
} catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
return response()->json(['token_invalid'], $e->getStatusCode());
} catch (Tymon\JWTAuth\Exceptions\JWTException $e) {
return response()->json(['token_absent'], $e->getStatusCode());
}
return response()->json(compact('user'));
import VueResource from 'vue-resource';
import Cookies from 'js-cookie';
Vue.use(VueResource);
Vue.http.headers.common['Authorization'] = 'Bearer ' + Cookies.get('Token');
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question