auth-component
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "auth-component", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "An Authenication Component for DoneJS", | ||
"main": "src/auth-component.js", | ||
"scripts": { | ||
"test": "npm run test" | ||
"test": "npm run test", | ||
"publish": "git push origin --tags", | ||
"release:patch": "npm version patch && npm publish", | ||
"release:minor": "npm version minor && npm publish", | ||
"release:major": "npm version major && npm publish" | ||
}, | ||
@@ -25,3 +29,3 @@ "repository": { | ||
"directories": { | ||
"lib": "lib" | ||
"lib": "src" | ||
} | ||
@@ -28,0 +32,0 @@ }, |
@@ -13,18 +13,44 @@ import $ from 'jquery'; | ||
get(){ | ||
var location = this.viewModel.attr('keyLocation'); | ||
return this.viewModel.attr('rememberMe') ? localStorage.getItem(location) : sessionStorage.getItem(location); | ||
var token, location = this.attr('keyLocation'); | ||
if (this.attr('rememberMe') && window.localStorage) { | ||
token = window.localStorage.getItem(location); | ||
} else if(window.sessionStorage) { | ||
token = window.sessionStorage.getItem(location); | ||
} | ||
return token; | ||
}, | ||
/** | ||
* Writes the token to either sessionStorage or localStorage depending on | ||
* if rememberMe is enabled. | ||
* @param {token} the token passed from the auth attr. | ||
*/ | ||
set(val){ | ||
var location = this.attr('keyLocation'); | ||
if (this.attr('rememberMe') && window.localStorage) { | ||
window.localStorage.setItem(location, val); | ||
} else if(window.sessionStorage) { | ||
window.sessionStorage.setItem(location, val); | ||
} | ||
return val; | ||
} | ||
}, | ||
/** | ||
* auth will either be an object containing `token` and `data` attributes, or it will be `false`. | ||
* @type {Object or Boolean} | ||
*/ | ||
auth: { | ||
get(lastSetVal, setVal){ | ||
return lastSetVal; | ||
set(val){ | ||
if (val.token) { | ||
this.attr('token', val.token); | ||
} | ||
return val; | ||
} | ||
}, | ||
/** | ||
* This will cause the auth token to be stored in localStorage instead of | ||
* When enabled, rememberMe will store the auth token in localStorage instead of | ||
* sessionStorage (so that it persists between sessions). | ||
* @type {String} | ||
* @type {Boolean} | ||
*/ | ||
@@ -53,11 +79,39 @@ rememberMe: { | ||
login(params){ | ||
login(username, password){ | ||
var self = this, params = {}; | ||
params[this.attr('usernameField')] = username; | ||
params[this.attr('passwordField')] = password; | ||
this.sendLogin(params).then(function(response){ | ||
// console.log('login response: ', response); | ||
self.attr('auth', response); | ||
}); | ||
}, | ||
tokenLogin(params){ | ||
var self = this; | ||
this.sendLogin(params).then(function(response){ | ||
// console.log('tokenLogin response: ', response); | ||
self.attr('auth', response); | ||
}); | ||
}, | ||
usernameField: 'username', | ||
passwordField: 'password', | ||
sendLogin(params){ | ||
var self = this; | ||
return $.ajax({ | ||
method: 'POST', | ||
contentType: 'application/json', | ||
url: this.attr('loginEndpoint'), | ||
url: self.attr('loginEndpoint'), | ||
data: JSON.stringify(params) | ||
}); | ||
} | ||
}, | ||
/** | ||
* Toggles a loading indicator until it's decided if the login form should show or not. | ||
* @type {Boolean} | ||
*/ | ||
loading: true | ||
}); | ||
@@ -70,8 +124,13 @@ | ||
events: { | ||
/** | ||
* On init, check for a token. If one exists, try to login, otherwise, | ||
* show the login form to the user. | ||
*/ | ||
init(){ | ||
// Try to login with token. | ||
var token = this.viewModel.attr('token'); | ||
if (token) { | ||
this.viewModel.login({token:token}).then(function(data){ | ||
console.log(data); | ||
}); | ||
this.viewModel.tokenLogin({token: token}); | ||
} else if (window.localStorage) { | ||
this.viewModel.attr('loading', false); | ||
} | ||
@@ -78,0 +137,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23126
26
180