Skip to content

Instantly share code, notes, and snippets.

@pdonorio
Created May 15, 2015 08:26
Show Gist options
  • Save pdonorio/4f2c1bd86e673c941f6e to your computer and use it in GitHub Desktop.
Save pdonorio/4f2c1bd86e673c941f6e to your computer and use it in GitHub Desktop.
A secured angular cookie
var setCookie = function(token, username)
{
// If
// If logout, token is null or undefined
if (!token || !username) {
token = FAILED_TOKEN;
username = FAILED_USER;
}
// One day expiration
var now = new Date(),
exp = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
// Depends on https and debug
var securing = false;
if (AppConfig.debug && AppConfig.protocol == 'https')
securing = true;
//set options
var cOptions = {
secure: securing,
expires: exp,
};
// Save
$cookies.put(COOKIEVAR_AUTHTOKEN, token, cOptions);
$cookies.put(COOKIEVAR_USER, username, cOptions);
logger.log("Saved new cookie");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment