Skip to content

Instantly share code, notes, and snippets.

@evanshortiss
Created February 20, 2018 22:11
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 evanshortiss/53ff15fd23d78f1f43d10fbee66259c0 to your computer and use it in GitHub Desktop.
Save evanshortiss/53ff15fd23d78f1f43d10fbee66259c0 to your computer and use it in GitHub Desktop.
Contains a reference hello.js for the Red Hat & RapID blogpost
// If you're working with a local domain you can uncomment this
// or update your fhconfig.json according to the RHMAP local
// development guides
// $fh.getCloudUrl = function () {
// return 'https://nodejs-cloudappdevezqll-rhmap-rhmap-development.127.0.0.1.nip.io'
//}
// Let the code below initialise, then configure our UI
setTimeout(function () {
components.name.hide();
components.login.show();
intialiseHelloComponent();
intialiseLoginComponent();
} , 0);
// Container with refrences to primary application components
var components = {
name: createComponent('name-component'),
login: createComponent('login-component')
}
// Element that communicates logged in status to a user
var loginStatus = document.getElementById('login-status')
function log () {
var args = Array.prototype.slice.call(arguments)
args.unshift(new Date())
console.log.apply(console, args)
}
/**
* Creates a lightweight component from a HTML node.
* A component is a reference to the node with some utilities attached.
*/
function createComponent (id) {
var el = document.getElementById(id);
if (!el) {
throw new Error('unable to find component with the id "' + id + '"')
}
return {
el: el,
show: () => { el.style = "" },
hide: () => { el.style = "display:none;" }
}
}
function formatHeaders (headers) {
// Convert headers from an object to a string
return Object.keys(headers).reduce(function (memo, key) {
var val = headers[key]
return memo += key + ': ' + val + '\n'
}, '')
}
function intialiseHelloComponent () {
var sayHelloBtn = document.getElementById('say_hello');
var cloudReponse = document.getElementById('cloudResponse')
sayHelloBtn.onclick = onSend;
function onSend () {
cloudReponse.innerHTML = "<p>Calling Cloud.....</p>";
var name = document.getElementById('hello_to').value;
var username = document.getElementById('username').value
var headers = $fh.getFHHeaders()
log('calling api/hello with username "' + username + '"')
security.sendRequestEx(
'get',
$fh.getCloudUrl() + '/api/hello?hello=' + name,
formatHeaders(headers),
null,
'true',
'15',
username,
'RapidForce',
onSuccess
)
function onSuccess (res) {
if (res.err) {
return onFailure(res.err)
}
log('success response from hello endpoint', res)
cloudReponse.innerHTML = "<p>" + JSON.parse(res.body).msg + "</p>";
}
function onFailure (err) {
log('fail response from hello endpoint', err)
alert('Cloud call failed. Error properties: ' + JSON.stringify(err));
}
}
}
function intialiseLoginComponent () {
document.getElementById('login-form').addEventListener('submit', doLogin)
function onLoginComplete () {
// Update logged in state
loginStatus.innerHTML = 'You are logged in.';
// Hide login UI and show primary UI
components.login.hide();
components.name.show();
}
function doLogin (e) {
e.preventDefault()
var username = document.getElementById('username').value
var password = document.getElementById('password').value
log('performing login for user', username)
var httpOpts = {
path: 'auth/login',
method: 'POST',
data: {
username: username,
password: password
}
}
function onLoginFailure (msg, err) {
alert('Cloud call failed with error message: ' + msg + '. Error properties: ' + JSON.stringify(err));
}
$fh.cloud(httpOpts, collectIdentity(username, onLoginComplete, onLoginFailure), onLoginFailure);
return false;
}
}
function collectIdentity (username, onSuccess, onFailure) {
return function _collectIdentity (cloudResponse) {
log('login success, collect identity with username, requestId', username, cloudResponse.requestId)
security.collectIdentity(username, cloudResponse.requestId, onSuccess, onFailure);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment