Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. //ANGULAR GET TOKEN
  2. ( function() {
  3.  
  4. var app = angular.module( 'jwtAuth', [] );
  5.  
  6. app.controller( 'MainController', function( $scope, $http ) {
  7.  
  8. var apiHost = 'http://officelife.re/wp-json';
  9.  
  10. $http.post( apiHost + '/jwt-auth/v1/token', {
  11. username: 'admin',
  12. password: 'password'
  13. } )
  14.  
  15. .then( function( response ) {
  16. console.log( response.data )
  17. } )
  18.  
  19. .catch( function( error ) {
  20. console.error( 'Error', error.data[0] );
  21. } );
  22.  
  23. } );
  24.  
  25. } )();
  26.  
  27. //ANGULAR SEND REQUEST
  28.  
  29. app.config( function( $httpProvider ) {
  30. $httpProvider.interceptors.push( [ '$q', '$location', '$cookies', function( $q, $location, $cookies ) {
  31. return {
  32. 'request': function( config ) {
  33. config.headers = config.headers || {};
  34. //Assume that you store the token in a cookie.
  35. var globals = $cookies.getObject( 'globals' ) || {};
  36. //If the cookie has the CurrentUser and the token
  37. //add the Authorization header in each request
  38. if ( globals.currentUser && globals.currentUser.token ) {
  39. config.headers.Authorization = 'Bearer ' + globals.currentUser.token;
  40. }
  41. return config;
  42. }
  43. };
  44. } ] );
  45. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement