Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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