私はAngular jとLarvel 5.4で作業しています。レスポンスアングルからトークンを取得する際に、すべてのヘッダにパスポートトークンを設定する方法js
私は角JSでの認証のためのログインコントローラを作成しており、そのコードは以下のようになります。
app.controller('LoginController',function($scope,$state) {
$scope.vm.login = function() {
$http({
method: 'POST',
url: apiUrl+'auth/token',
data: $.param($scope.vm.logDetails),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function(response) {
console.log(response.data.access_token);
if(response.data.access_token) {
$state.go('dashboard.masters_userroles');
}
else {
alert("Your credentials is wrong");
}
});
}
});
私はそれのためにインターセプタを作成しました。しかし、ヘッダーを設定することはできません。
.factory('tokenInjector',function($window){
return {
//For each request the interceptor will set the bearer token header.
request: function($config) {
if($window.sessionStorage.getItem('userInfo-token'))
{
var token=$window.sessionStorage.getItem('userInfo-token');
console.log(token);
//set authorization header
$config.headers['Authorization'] = 'Bearer '+token;
return $config;
}
}
}
})
.config(function ($httpProvider) {
$httpProvider.interceptors.push('tokenInjector');
})
これはError: $compile:tpload Error Loading Template
のようなエラーです。
ここで、コンソールでは、コントローラでトークンを受信しました。今度はすべてのリクエストのヘッダーを$ stateProviderに設定したいと思います。では、トークンを保存するためにはどのような機能を作成する必要がありますか?
http://stackoverflow.com/a/27136594/796400 –
@RaghuVenmarathoorどのように私は動的にトークンを設定することができますか? –
?私は理解できませんでした。インターセプタを使用して、そのトークンをすべての要求に設定できます。 –