Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

Commit

Permalink
Use removeItem in the storage on logout (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored and ekryski committed May 24, 2016
1 parent 8a3d496 commit c75b20e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/client/index.js
Expand Up @@ -80,9 +80,9 @@ export default function(opts = {}) {
app.set('token', null);

clearCookie(config.cookie);

// remove the token from localStorage
return Promise.resolve(app.get('storage').setItem(config.tokenKey, '')).then(() => {
return Promise.resolve(app.get('storage').removeItem(config.tokenKey)).then(() => {
// If using sockets de-authenticate the socket
if (app.io || app.primus) {
const method = app.io ? 'emit' : 'send';
Expand Down
7 changes: 6 additions & 1 deletion src/client/utils.js
Expand Up @@ -66,7 +66,7 @@ export function getCookie(name) {
// Returns the value for a cookie
export function clearCookie(name) {
if (typeof document !== 'undefined') {
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}

return null;
Expand Down Expand Up @@ -99,6 +99,11 @@ export function getStorage(storage) {

setItem(key, value) {
return (this.store[key] = value);
},

removeItem(key) {
delete this.store[key];
return this;
}
};
}
26 changes: 15 additions & 11 deletions test/client/index.test.js
Expand Up @@ -110,26 +110,30 @@ const setupTests = initApp => {
}).catch(done);
});

it('.logout works, does not grant access to protected service', done => {
it('.logout works, does not grant access to protected service and token is removed from localstorage', done => {
app.authenticate({
type: 'local',
email, password
}).then(response => {
expect(response.token).to.not.equal(undefined);
expect(response.data).to.not.equal(undefined);
type: 'local',
email, password
}).then(response => {
expect(response.token).to.not.equal(undefined);
expect(response.data).to.not.equal(undefined);

app.logout().then(() => {
expect(app.get('token')).to.equal(null);
expect(app.get('user')).to.equal(null);
return app.logout().then(() => {
expect(app.get('token')).to.equal(null);
expect(app.get('user')).to.equal(null);


return Promise.resolve(app.get('storage').getItem('feathers-jwt')).then(token => {
expect(token).to.equal(undefined);
app.service('messages').create({ text: 'auth test message' })
.then(done)
.catch(error => {
expect(error.code).to.equal(401);
done();
});
}).catch(done);
}).catch(done);
});
});
}).catch(done);
});
};

Expand Down

0 comments on commit c75b20e

Please sign in to comment.