Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ANGULAR GET TOKEN
- ( function() {
- var app = angular.module( 'jwtAuth', [] );
- app.controller( 'MainController', function( $scope, $http ) {
- var apiHost = 'http://officelife.re/wp-json';
- $http.post( apiHost + '/jwt-auth/v1/token', {
- username: 'admin',
- password: 'password'
- } )
- .then( function( response ) {
- console.log( response.data )
- } )
- .catch( function( error ) {
- console.error( 'Error', error.data[0] );
- } );
- } );
- } )();
- //ANGULAR SEND REQUEST
- 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;
- }
- };
- } ] );
- } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement