Created
October 25, 2017 11:41
-
-
Save christianbirg/5be0f007db23e8d79728d56dd1b0a904 to your computer and use it in GitHub Desktop.
Needs to be added to all angular 1.x projects, to enable add an access token in every request header for development purposes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./src/accessToken.js | |
import config from 'config' | |
/* @ngInject */ | |
export default ($httpProvider) => { | |
/* @ngInject */ | |
const interceptor = function($q) { | |
return { | |
'request': function(config) { | |
if(process.env.NODE_ENV === 'development') { | |
config.headers['access-token'] = config.accessToken; | |
} | |
console.log(config.headers); | |
return config; | |
} | |
}; | |
}; | |
$httpProvider.interceptors.push(interceptor); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./config/development.js | |
export default { | |
accessToken: '<YOUR_ACESS_TOKEN_HERE>' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./src/index.js | |
import accessToken from './accessToken.js'; | |
export default angular.module('app', [ | |
]).config(accessToken); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./webpack.config.js | |
var webpack = require('webpack'); | |
var path = require('path'); | |
// not the full webpack config, only the required parts to work | |
module.exports = { | |
context: path.join(__dirname, './src'), | |
entry: "./index.js", | |
output: { | |
path: path.join(__dirname, './public'), | |
filename: "./bundle.[hash].js" | |
}, | |
resolve: { | |
alias: { | |
config: path.join(__dirname, './config', `${process.env.NODE_ENV}.js`) | |
} | |
}), | |
plugins: [ | |
new webpack.DefinePlugin({ | |
'process.env.NODE_ENV': JSON.stringify('development') | |
}) | |
] | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment