Skip to content

Commit

Permalink
fix(localstorage): change localstorage module
Browse files Browse the repository at this point in the history
  • Loading branch information
zack9433 committed Aug 31, 2016
1 parent 60f375d commit 2528e71
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/component/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import angular from 'angular';
import 'angular-http-auth';
import LocalStorageModule from 'angular-local-storage';
import LocalStorageModule from 'angular-storage';
import {sjRest} from 'sanji-rest-ui';

import authEvent from './auth.constant';
Expand Down
14 changes: 7 additions & 7 deletions app/component/session.provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class SessionProvider {
this.configure = cfg => Object.assign(this.config, cfg);
}

$get(localStorageService) {
$get(store) {
'ngInject';
let config = this.config;
let session = {};

session[config.tokenKey] = localStorageService.get(config.tokenKey) || null;
session.user = localStorageService.get('sj-user-profile') || null;
session[config.tokenKey] = store.get(config.tokenKey) || null;
session.user = store.get('sj-user-profile') || null;

return {
get: get,
Expand Down Expand Up @@ -46,7 +46,7 @@ class SessionProvider {
}

function setUserData(data) {
localStorageService.set('sj-user-profile', data);
store.set('sj-user-profile', data);
set('user', data);
}

Expand All @@ -55,13 +55,13 @@ class SessionProvider {
}

function create(token) {
localStorageService.set(config.tokenKey, token);
store.set(config.tokenKey, token);
session[config.tokenKey] = token;
}

function destroy() {
localStorageService.remove(config.tokenKey);
localStorageService.remove('sj-user-profile');
store.remove(config.tokenKey);
store.remove('sj-user-profile');
session[config.tokenKey] = null;
}
}
Expand Down
28 changes: 14 additions & 14 deletions app/component/session.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'angular-mocks';
import {sjAuth} from './index' ;

let sessionProvider;
let localStorageService;
let store;

describe('Provider: sessionProvider', () => {
beforeEach(angular.mock.module(sjAuth));
Expand All @@ -16,8 +16,8 @@ describe('Provider: sessionProvider', () => {
});

beforeEach(() => {
angular.mock.inject((_localStorageService_) => {
localStorageService = _localStorageService_;
angular.mock.inject((_store_) => {
store = _store_;
});
});

Expand All @@ -31,7 +31,7 @@ describe('Provider: sessionProvider', () => {
});

it('#$get(<...injects>) should return session service instance', () => {
let obj = sessionProvider.$get(localStorageService);
let obj = sessionProvider.$get(store);
expect(obj.get).to.be.a('function');
expect(obj.set).to.be.a('function');
expect(obj.getTokenKey).to.be.a('function');
Expand All @@ -43,64 +43,64 @@ describe('Provider: sessionProvider', () => {

describe('Service: session', () => {
it('#get(<key>) should return specific value', function() {
let obj = sessionProvider.$get(localStorageService);
let obj = sessionProvider.$get(store);
obj.set('test', 'hello world');
expect(obj.get('test')).to.equal('hello world');
});

it('#set(<key>, <value>) should set specific key and value', function() {
let obj = sessionProvider.$get(localStorageService);
let obj = sessionProvider.$get(store);
obj.set('test', 'hello world');
expect(obj.get('test')).to.equal('hello world');
});

it('#getTokenKey() should return default token key name', function() {
let obj = sessionProvider.$get(localStorageService);
let obj = sessionProvider.$get(store);
expect(obj.getTokenKey()).to.equal('token');
});

it('#getTokenKey() should return settings token key name', function() {
let obj;
sessionProvider.configure({tokenKey: 'test'});
obj = sessionProvider.$get(localStorageService);
obj = sessionProvider.$get(store);
expect(obj.getTokenKey()).to.equal('test');
});

it('#getTokenHeader() should return default token header', function() {
let obj = sessionProvider.$get(localStorageService);
let obj = sessionProvider.$get(store);
expect(obj.getTokenHeader()).to.equal('Authorization');
});

it('#getTokenHeader() should return settings token key name', function() {
let obj;
sessionProvider.configure({tokenHeader: 'test'});
obj = sessionProvider.$get(localStorageService);
obj = sessionProvider.$get(store);
expect(obj.getTokenHeader()).to.equal('test');
});

it('#getUserData() should return user data', function() {
let obj = sessionProvider.$get(localStorageService);
let obj = sessionProvider.$get(store);
let data = {role: 'admin', username: 'admin'};
obj.setUserData(data);
expect(obj.getUserData()).to.eql(data);
});

it('#setUserData(<user data>) should set user data', function() {
let obj = sessionProvider.$get(localStorageService);
let obj = sessionProvider.$get(store);
let data = {role: 'admin', username: 'admin'};
obj.setUserData(data);
expect(obj.getUserData()).to.eql(data);
});

it('#create(<token>) should set token data', function() {
let obj = sessionProvider.$get(localStorageService);
let obj = sessionProvider.$get(store);
let token = 'helloworld';
obj.create(token);
expect(obj.get('token')).to.equal(token);
});

it('#destory() should remove token data', function() {
let obj = sessionProvider.$get(localStorageService);
let obj = sessionProvider.$get(store);
let token = 'helloworld';
obj.create(token);
expect(obj.get('token')).to.equal(token);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"dependencies": {
"angular": "~1.5.0",
"angular-http-auth": "~1.3.0",
"angular-local-storage": "~0.2.7",
"angular-storage": "~0.0.15",
"sanji-rest-ui": "~1.3.6"
}
}

0 comments on commit 2528e71

Please sign in to comment.