usage
-
well i obtain a token but after i don’t know of conitnue with this (look under in my topic..) well where i must put that code inside first angular function?
Once you get the token, you must store it somewhere in your application, ex. in a cookie or using localstorage.
From this point, you should pass this token to every API call
Sample call using the Authorization header using AngularJS
app.config( function( $httpProvider ) {
$httpProvider.interceptors.push( [ ‘$q’, ‘$location’, ‘$cookies’, function( $q, $location, $cookies ) {
return {
‘request’: function( config ) {
config.headers = config.headers || {};
//Assume that you store the token in a cookie.
var globals = $cookies.getObject( ‘globals’ ) || {};
//If the cookie has the CurrentUser and the token
//add the Authorization header in each request
if ( globals.currentUser && globals.currentUser.token ) {
config.headers.Authorization = ‘Bearer ‘ + globals.currentUser.token;
}
return config;
}
};
} ] );
} );The wp-api-jwt-auth will intercept every call to the server and will look for the Authorization Header, if the Authorization header is present will try to decode the token and will set the user according with the data stored in it.
If the token is valid, the API call flow will continue as always.
- The topic ‘usage’ is closed to new replies.