Skip to content

Instantly share code, notes, and snippets.

@mziwisky
Created January 22, 2015 23:07
Show Gist options
  • Save mziwisky/d23d9f145dffe9ba080a to your computer and use it in GitHub Desktop.
Save mziwisky/d23d9f145dffe9ba080a to your computer and use it in GitHub Desktop.

Revisions

  1. mziwisky created this gist Jan 22, 2015.
    50 changes: 50 additions & 0 deletions AjaxInterceptorManager.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    var authExpirationHandler,
    errorHandler;

    function catchUnauthorizedResponses(data) {
    if (data.status === 401 && !maybeLocalStorage.getItem("token")) {
    // ensure the in-memory session really is expired before destroying it
    return axios.get("/api/auth/session").then(
    (response) => {
    // we ARE still auth'd, so just throw the error down the chain
    throw data;
    }, (err) => {
    // local session is invalid, so invoke handler and throw the original error
    authExpirationHandler ? authExpirationHandler.call(null) : null;
    // TODO: remove self from interceptors chain
    throw data;
    });
    } else {
    throw data;
    }
    }

    function authHeader(email, token) {
    return "Bearer email=\"" + email + "\", token=\"" + token + "\"";
    }

    function authorizeAxiosRequests(config) {
    if (session) {
    config.headers.Authorization = authHeader(email, token);
    }
    return config;
    }

    function catchAllErrorResponses(data) {
    // maybe don't call errorHandler if (!!authExpirationHandler && data.status === 401) ?? i dunno.
    errorHandler.call(null, data.status);
    }


    module.exports = {
    addErrorInterceptors (handler) {
    errorHandler = handler;
    // TODO: add catchAllErrorResponses to the interceptors chain
    },

    addAuthInterceptors (email, token, handler) {
    authExpirationHandler = handler;
    // TODO: add catchUnauthorizedResponses to the interceptors chain
    // TODO: add authorizeAxiosRequests to the interceptors chain
    }
    }
    16 changes: 16 additions & 0 deletions ErrorStore.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    // ...

    function handleError(status) {
    switch(status) {
    case 401:
    // display "Unauthorized" error
    default:
    // display more generic error
    }
    }

    function initialize() {
    AjaxInterceptorManager.addErrorInterceptors();
    }

    // ...
    16 changes: 16 additions & 0 deletions SessionStore.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    // ...

    function handleSessionExpiration() {
    SessionStore.actionHandlers.SESSION_DESTROYED.call(SessionStore);
    }

    function loadSession(sessionData) {
    session = sessionData;

    maybeLocalStorage.setItem("email", session.user.email);
    maybeLocalStorage.setItem("token", session.token);

    AjaxInterceptorManager.addAuthInterceptors(session.user.email, session.token, handleSessionExpiration);
    }

    // ...