Skip to content

Instantly share code, notes, and snippets.

@brianly
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianly/e0c4ab701b6ecfb13b19 to your computer and use it in GitHub Desktop.
Save brianly/e0c4ab701b6ecfb13b19 to your computer and use it in GitHub Desktop.
Use setAuthToken to store an OAuth token in a cookie. Long-lived OAuth tokens should be generated through the server-side flow and the token variable should be set from server-side code.
<!DOCTYPE html>
<html>
<head>
<title>setAuthToken() example</title>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" data-app-id="VlXilc0P6NemTfCzxTKChg" src="https://c64.assets-yammer.com/assets/platform_js_sdk.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function () {
var $result = $("#result");
var $details = $("#details");
var token = docCookies.getItem("user_token");
function dumpDetails(response) {
$details.append("Status: " + response.status + "</br>");
$details.append("Network name: " + response.access_token.network_name + "</br>");
$details.append("User ID: " + response.access_token.user_id + "</br>");
$details.append("Token: " + response.access_token.token + "</br>");
}
try {
$result.append("running...<br/>");
yam.getLoginStatus(
yam.platform.setAuthToken(token,
function (response) {
$result.append("response received...<br/>");
if (response.authResponse) {
$result.append("authResponse is true and token loaded from cookie<br/>");
dumpDetails(response);
} else {
$result.append("Trying yam.login because authResponse is false...<br/>");
$result.append("Additional popup may appear...<br/>");
yam.platform.login(function (response) {
if (response.authResponse) {
token = response.access_token.token;
docCookies.setItem("user_token", token);
console.dir(response);
$result.append("authResponse is now true.<br/>");
dumpDetails(response);
}
})
}
})
)
} catch (e){
$result.html("EXCEPTION!");
$details.html(e);
}
});
// Basic cookie handling from http://mdn.beonex.com/en/DOM/document.cookie.html
docCookies = {
getItem: function (sKey) {
if (!sKey || !this.hasItem(sKey)) { return null; }
return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1"));
},
/**
* docCookies.setItem(sKey, sValue, vEnd, sPath, sDomain, bSecure)
*
* @argument sKey (String): the name of the cookie;
* @argument sValue (String): the value of the cookie;
* @optional argument vEnd (Number, String, Date Object or null): the max-age in seconds (e.g., 31536e3 for a year) or the
* expires date in GMTString format or in Date Object format; if not specified it will expire at the end of session;
* @optional argument sPath (String or null): e.g., "/", "/mydir"; if not specified, defaults to the current path of the current document location;
* @optional argument sDomain (String or null): e.g., "example.com", ".example.com" (includes all subdomains) or "subdomain.example.com"; if not
* specified, defaults to the host portion of the current document location;
* @optional argument bSecure (Boolean or null): cookie will be transmitted only over secure protocol as https;
* @return undefined;
**/
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/.test(sKey)) { return; }
var sExpires = "";
if (vEnd) {
switch (typeof vEnd) {
case "number": sExpires = "; max-age=" + vEnd; break;
case "string": sExpires = "; expires=" + vEnd; break;
case "object": if (vEnd.hasOwnProperty("toGMTString")) { sExpires = "; expires=" + vEnd.toGMTString(); } break;
}
}
document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
},
removeItem: function (sKey) {
if (!sKey || !this.hasItem(sKey)) { return; }
var oExpDate = new Date();
oExpDate.setDate(oExpDate.getDate() - 1);
document.cookie = escape(sKey) + "=; expires=" + oExpDate.toGMTString() + "; path=/";
},
hasItem: function (sKey) { return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); }
};
// docCookies.setItem("test1", "Hello world!");
// docCookies.setItem("test2", "Hello world!", new Date(2020, 5, 12));
// docCookies.setItem("test3", "Hello world!", new Date(2027, 2, 3), "/blog");
// docCookies.setItem("test4", "Hello world!", "Sun, 06 Nov 2022 21:43:15 GMT");
// docCookies.setItem("test5", "Hello world!", "Tue, 06 Dec 2022 13:11:07 GMT", "/home");
// docCookies.setItem("test6", "Hello world!", 150);
// docCookies.setItem("test7", "Hello world!", 245, "/content");
// docCookies.setItem("test8", "Hello world!", null, null, "example.com");
// docCookies.setItem("test9", "Hello world!", null, null, null, true);
// alert(docCookies.getItem("test1"));
</script>
<body>
<h1>Yammer JS SDK</h1>
<div>
Setting authentication token... Status:<br/>
<span id="result" style="color: #00f;">init...<br/> </span>
</div>
<h2>Details:</h2>
<div id="details"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment