Skip to content

Instantly share code, notes, and snippets.

@abdullin
Created March 6, 2015 10:43
Show Gist options
  • Save abdullin/b15be618129af023a60b to your computer and use it in GitHub Desktop.
Save abdullin/b15be618129af023a60b to your computer and use it in GitHub Desktop.
Example of a Flux Store that maintains session token in a cookie
"use strict";
var createStore = require("fluxible/utils/createStore");
var client = require("../client");
var cookies = require("cookies-js");
var debug = require("debug")("ClientTokenStore");
var ClientTokenStore = createStore({
storeName: "ClientSessionStore",
handlers: {
"event:LoginFailed": "clear",
"event:LoginSucceeded": "init",
"event:LoggedOut": "clear"
},
clear: function() {
client.setToken(undefined);
cookies.set("svt");
this.emitChange();
},
init: function(author) {
// expire in an hour
client.setToken(author.token, {
expires: 60 * 60
});
cookies.set("svt", author.token);
this.emitChange();
},
initAndGetToken: function() {
var endpoint = cookies.get("sv_api_endpoint");
if (undefined !== endpoint) {
debug("Cookie detected. Switching API endpoint to ", endpoint);
client.setEndpoint(endpoint);
}
var token = cookies.get("svt");
return token;
}
});
module.exports = ClientTokenStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment