Ivan - Update localStorage [skip ci]

This commit is contained in:
ivan 2016-07-19 15:49:03 -03:00
parent 6662f0da61
commit 0caa5cb4f9
6 changed files with 96 additions and 41 deletions

View File

@ -55,6 +55,7 @@
"app-module-path": "^1.0.3",
"classnames": "^2.1.3",
"jquery": "^2.1.4",
"localStorage": "^1.0.3",
"lodash": "^3.10.0",
"messageformat": "^0.2.2",
"react": "^15.0.1",

View File

@ -5,19 +5,21 @@ module.exports = [
response: function (data) {
let response;
if (data.password === 'invalid') {
response = {
status: 'fail',
message: 'Invalid Credientals'
};
} else {
if (data.password === 'valid' || (data.rememberToken === 'aa41efe0a1b3eeb9bf303e4561ff8392' && data.userId === 12)) {
response = {
status: 'success',
data: {
'userId': 12,
'token': 'cc6b4921e6733d6aafe284ec0d7be57e'
'token': 'cc6b4921e6733d6aafe284ec0d7be57e',
'rememberToken': (data.remember) ? 'aa41efe0a1b3eeb9bf303e4561ff8392' : null,
'rememberExpiration': (data.remember) ? 2018 : 0
}
};
} else {
response = {
status: 'fail',
message: 'Invalid Credientals'
};
}
return response;

View File

@ -11,6 +11,7 @@ function processData (data) {
module.exports = {
call: function ({path, data, onSuccess, onFail}) {
APIUtils.post(root + path, processData(data)).then(function (result) {
console.log(result);
if (result.status === 'success') {
onSuccess && onSuccess(result);
} else {

View File

@ -1,10 +1,9 @@
import LocalStorage from 'localstorage';
import LocalStorage from 'localStorage';
class LocalStore {
constructor() {
this.setItem = LocalStorage.setItem;
this.getItem = LocalStorage.getItem;
this.storage = LocalStorage;
}
initialize() {
@ -18,21 +17,37 @@ class LocalStore {
}
storeRememberData({token, userId, expiration}) {
this.setItem('rememberData', {
token,
userId,
expiration
});
this.setItem('rememberData-token', token);
this.setItem('rememberData-userId', userId);
this.setItem('rememberData-expiration', expiration);
}
isRememberDataExpired() {
let rememberData = this.getItem('rememberData');
let rememberData = this.getRememberData();
return rememberData && rememberData.expirationDate > 2016
return rememberData.expiration < 2016;
}
getRememberData() {
return {
token: this.getItem('rememberData-token'),
userId: parseInt(this.getItem('rememberData-userId')),
expiration: parseInt(this.getItem('rememberData-expiration'))
};
}
clearRememberData() {
this.setItem('rememberData', null);
this.setItem('rememberData-token', null);
this.setItem('rememberData-userId', null);
this.setItem('rememberData-expiration', null);
}
getItem(key) {
return this.storage.getItem(key);
}
setItem(key, value) {
return this.storage.setItem(key, value);
}
}

View File

@ -1,33 +1,42 @@
import SessionStorage from 'sessionstorage';
import LocalStore from 'lib-app/local-store';
class SessionStore {
static initialize() {
constructor() {
this.storage = SessionStorage;
}
static createSession(userId, token) {
SessionStorage.setItem('userId', userId);
SessionStorage.setItem('token', token);
createSession(userId, token) {
this.setItem('userId', userId);
this.setItem('token', token);
}
static getSessionData() {
getSessionData() {
return {
userId: SessionStorage.getItem('userId'),
token: SessionStorage.getItem('token')
userId: this.getItem('userId'),
token: this.getItem('token')
};
}
static isLoggedIn() {
return !!SessionStorage.getItem('userId');
isLoggedIn() {
return !!this.getItem('userId');
}
static closeSession() {
SessionStorage.removeItem('userId');
SessionStorage.removeItem('token');
closeSession() {
this.removeItem('userId');
this.removeItem('token');
}
getItem(key) {
return this.storage.getItem(key);
}
setItem(key, value) {
return this.storage.setItem(key, value);
}
removeItem(key) {
return this.storage.removeItem(key);
}
}
SessionStore.initialize();
export default SessionStore;
export default new SessionStore();

View File

@ -1,6 +1,7 @@
const Reflux = require('reflux');
const API = require('lib-app/api-call');
const SessionStore = require('lib-app/session-store');
const sessionStore = require('lib-app/session-store');
const localStore = require('lib-app/local-store');
const UserActions = require('actions/user-actions');
const CommonActions = require('actions/common-actions');
@ -13,14 +14,18 @@ const UserStore = Reflux.createStore({
this.listenTo(UserActions.checkLoginStatus, this.checkLoginStatus);
this.listenTo(UserActions.login, this.loginUser);
this.listenTo(UserActions.logout, this.logoutUser);
if (!this.isLoggedIn()) {
this.loginIfRememberExists();
}
},
loginUser(loginData) {
API.call({
path: 'user/login',
data: loginData,
onSuccess: this.handleLoginSuccess,
onFail: this.handleLoginFail
onSuccess: (loginData.remember) ? this.handleLoginSuccessWithRemember : this.handleLoginSuccess,
onFail: (loginData.isAutomatic) ? null : this.handleLoginFail
});
},
@ -28,7 +33,7 @@ const UserStore = Reflux.createStore({
API.call({
path: 'user/logout',
onSuccess: function () {
SessionStore.closeSession();
sessionStore.closeSession();
CommonActions.loggedOut();
this.trigger('LOGOUT');
}.bind(this)
@ -36,11 +41,33 @@ const UserStore = Reflux.createStore({
},
isLoggedIn() {
return SessionStore.isLoggedIn();
return sessionStore.isLoggedIn();
},
loginIfRememberExists() {
let rememberData = localStore.getRememberData();
if (!localStore.isRememberDataExpired()) {
UserActions.login({
userId: rememberData.userId,
rememberToken: rememberData.token,
isAutomatic: true
});
}
},
handleLoginSuccessWithRemember(result) {
localStore.storeRememberData({
token: result.data.rememberToken,
userId: result.data.userId,
expiration: result.data.rememberExpiration
});
this.handleLoginSuccess(result)
},
handleLoginSuccess(result) {
SessionStore.createSession(result.data.userId, result.data.token);
sessionStore.createSession(result.data.userId, result.data.token);
CommonActions.logged();
this.trigger('LOGIN_SUCCESS');
},