Skip to content

Commit

Permalink
2.0: Support browserify and webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldijou committed Feb 8, 2016
1 parent 4383c08 commit 6fd41b9
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules
npm-debug.log
test/browserify/main.bundle.js
test/webpack/main.bundle.js
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test
karma.conf.js
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jwt-client",
"main": "jwt-client.js",
"version": "0.1.4",
"version": "0.2.0",
"homepage": "https://github.com/pauldijou/jwt-client",
"authors": [
"Paul Dijou <[email protected]> (http://pauldijou.fr)"
Expand Down
1 change: 1 addition & 0 deletions browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./jwt-client')(window);
16 changes: 8 additions & 8 deletions jwt-client.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
(function (glob, factory) {
(function (global_, factory) {
if (typeof exports === 'object') {
module.exports = factory(glob);
module.exports = factory;
}
else if (typeof define === 'function' && define.amd) {
define([], function () {
return factory(glob);
return factory(global_);
});
}
else {
window.JWT = factory(glob);
window.JWT = factory(global_);
}
})(this, function (global) {
})(this, function (_global) {
var JWT = {};

JWT.defaults = {
Expand All @@ -19,20 +19,20 @@
// This is the official token to use for JWT but feel free to use another one if you want
tokenPrefix: 'Bearer ',
// Where to store the token, by default localStorage
storage: global.localStorage,
storage: _global.localStorage,
// In Base64 url-safe mode, padding isn't mandatory, so we will disable it by default
// but you can force it by setting this param to true if you want
padding: false
};

JWT.encode64 = function encode64(value) {
var encoded = global.btoa(global.unescape(global.encodeURIComponent(value)));
var encoded = _global.btoa(_global.unescape(_global.encodeURIComponent(value)));
if (!JWT.defaults.padding) { return encoded.replace(/=+$/, ''); }
else { return encoded; }
};

JWT.decode64 = function decode64(value) {
return global.decodeURIComponent(global.escape(global.atob(value)));
return _global.decodeURIComponent(_global.escape(_global.atob(value)));
};

JWT.write = function write(value) {
Expand Down
1 change: 1 addition & 0 deletions node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('JWT Client does not support Node.js yet. PR welcome.');
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jwt-client",
"description": "Micro service to read, write and save JWT headers",
"version": "0.1.4",
"version": "0.2.0",
"author": "Paul Dijou <[email protected]> (http://pauldijou.fr)",
"license": "Apache 2",
"homepage": "https://github.com/pauldijou/jwt-client",
Expand All @@ -15,12 +15,16 @@
"keywords": [
"jwt"
],
"main": "jwt-client.js",
"main": "node.js",
"browser": "browser.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "./node_modules/karma/bin/karma start"
"test": "./node_modules/karma/bin/karma start",
"node": "node ./test/node/main.js",
"browserify": "browserify ./test/browserify/main.js -o ./test/browserify/main.bundle.js",
"webpack": "webpack ./test/webpack/main.js ./test/webpack/main.bundle.js"
},
"devDependencies": {
"karma": "^0.12.x",
Expand All @@ -31,6 +35,7 @@
"karma-opera-launcher": "^0.1.x",
"karma-phantomjs-launcher": "^0.1.x",
"karma-safari-launcher": "^0.1.x",
"requirejs": "^2.1.22"
"requirejs": "^2.1.22",
"webpack": "^1.12.13"
}
}
7 changes: 7 additions & 0 deletions test/browserify/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!doctype>
<html>
<head></head>
<body>
<script src="main.bundle.js"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions test/browserify/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
console.log('Init browserify');

var JWT = require('../../');

console.log(JWT);

var token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEyMzQ1Njc4OTAsIm5hbWUiOiJKb2huIERvZSIsImFkbWluIjp0cnVlfQ.eoaDVGTClRdfxUZXiPs3f8FmJDkDE_VCQFXqKxpLsts';
var value = JWT.read(token);

console.log('Read', value);
console.log('Validate', JWT.validate(value));

JWT.keep(value);
console.log('Keep', window.localStorage.getItem('JWT_TOKEN'));
console.log('Remember', JWT.remember());

JWT.forget();
console.log('Forget', JWT.remember());
2 changes: 2 additions & 0 deletions test/node/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var JWT = require('../../');
// Should throw
7 changes: 7 additions & 0 deletions test/webpack/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!doctype>
<html>
<head></head>
<body>
<script src="main.bundle.js"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions test/webpack/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
console.log('Init webpack');

var JWT = require('../../');

console.log(JWT);

var token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEyMzQ1Njc4OTAsIm5hbWUiOiJKb2huIERvZSIsImFkbWluIjp0cnVlfQ.eoaDVGTClRdfxUZXiPs3f8FmJDkDE_VCQFXqKxpLsts';
var value = JWT.read(token);

console.log('Read', value);
console.log('Validate', JWT.validate(value));

JWT.keep(value);
console.log('Keep', window.localStorage.getItem('JWT_TOKEN'));
console.log('Remember', JWT.remember());

JWT.forget();
console.log('Forget', JWT.remember());

0 comments on commit 6fd41b9

Please sign in to comment.