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

Commit

Permalink
adding ability to use custom cookie name on client. Clears cookie on …
Browse files Browse the repository at this point in the history
…logout. Closes #122
  • Loading branch information
ekryski committed Mar 24, 2016
1 parent 900bf02 commit d3655f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/client/index.js
@@ -1,8 +1,9 @@
import * as hooks from './hooks';
import { connected, authenticateSocket, getJWT, getStorage } from './utils';
import { connected, authenticateSocket, getJWT, getStorage, clearCookie } from './utils';
import errors from 'feathers-errors';

const defaults = {
cookie: 'fathers-jwt',
tokenKey: 'feathers-jwt',
localEndpoint: '/auth/local',
tokenEndpoint: '/auth/token'
Expand All @@ -24,7 +25,7 @@ export default function(opts = {}) {

// If no type was given let's try to authenticate with a stored JWT
if (!options.type) {
getOptions = getJWT(config.tokenKey, this.get('storage')).then(token => {
getOptions = getJWT(config.tokenKey, config.cookie, this.get('storage')).then(token => {
if (!token) {
return Promise.reject(new errors.NotAuthenticated(`Could not find stored JWT and no authentication type was given`));
}
Expand Down Expand Up @@ -69,6 +70,8 @@ export default function(opts = {}) {
app.logout = function() {
app.set('user', null);
app.set('token', null);

clearCookie(config.cookie);

// TODO (EK): invalidate token with server
return Promise.resolve(app.get('storage').setItem(config.tokenKey, ''));
Expand Down
21 changes: 15 additions & 6 deletions src/client/utils.js
Expand Up @@ -38,7 +38,7 @@ export function authenticateSocket(options, socket, method) {

// Returns the value for a cookie
export function getCookie(name) {
if(typeof document !== 'undefined') {
if (typeof document !== 'undefined') {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);

Expand All @@ -50,13 +50,22 @@ export function getCookie(name) {
return null;
}

// Returns the value for a cookie
export function clearCookie(name = 'feathers-jwt') {
if (typeof document !== 'undefined') {
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}

return null;
}

// Tries the JWT from the given key either from a storage or the cookie
export function getJWT(key, storage) {
return Promise.resolve(storage.getItem(key)).then(jwt => {
const cookieKey = getCookie(key);
export function getJWT(tokenKey, cookieKey, storage) {
return Promise.resolve(storage.getItem(tokenKey)).then(jwt => {
const cookieToken = getCookie(cookieKey);

if(cookieKey) {
return cookieKey;
if (cookieToken) {
return cookieToken;
}

return jwt;
Expand Down

0 comments on commit d3655f0

Please sign in to comment.