Created
March 6, 2015 10:43
-
-
Save abdullin/b15be618129af023a60b to your computer and use it in GitHub Desktop.
Example of a Flux Store that maintains session token in a cookie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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