Created
June 7, 2018 19:10
-
-
Save jwill9999/76afdd3de6e6665a4b0a703231e13a59 to your computer and use it in GitHub Desktop.
Set a local storage session token JavaScript
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
// Form response processor set session token | |
app.formResponseProcessor = function(formId,requestPayload,responsePayload){ | |
var functionToCall = false; | |
// If account creation was successful, try to immediately log the user in | |
if(formId == 'accountCreate'){ | |
// Take the phone and password, and use it to log the user in | |
var newPayload = { | |
'phone' : requestPayload.phone, | |
'password' : requestPayload.password | |
}; | |
app.client.request(undefined,'api/tokens','POST',undefined,newPayload,function(newStatusCode,newResponsePayload){ | |
// Display an error on the form if needed | |
if(newStatusCode !== 200){ | |
// Set the formError field with the error text | |
document.querySelector("#"+formId+" .formError").innerHTML = 'Sorry, an error has occured. Please try again.'; | |
// Show (unhide) the form error field on the form | |
document.querySelector("#"+formId+" .formError").style.display = 'block'; | |
} else { | |
// If successful, set the token and redirect the user | |
app.setSessionToken(newResponsePayload); | |
window.location = '/checks/all'; | |
} | |
}); | |
} | |
// If login was successful, set the token in localstorage and redirect the user | |
if(formId == 'sessionCreate'){ | |
app.setSessionToken(responsePayload); | |
window.location = '/checks/all'; | |
} | |
// If forms saved successfully and they have success messages, show them | |
var formsWithSuccessMessages = ['accountEdit1', 'accountEdit2','checksEdit1']; | |
if(formsWithSuccessMessages.indexOf(formId) > -1){ | |
document.querySelector("#"+formId+" .formSuccess").style.display = 'block'; | |
} | |
// If the user just deleted their account, redirect them to the account-delete page | |
if(formId == 'accountEdit3'){ | |
app.logUserOut(false); | |
window.location = '/account/deleted'; | |
} | |
// If the user just created a new check successfully, redirect back to the dashboard | |
if(formId == 'checksCreate'){ | |
window.location = '/checks/all'; | |
} | |
// If the user just deleted a check, redirect them to the dashboard | |
if(formId == 'checksEdit2'){ | |
window.location = '/checks/all'; | |
} | |
}; |
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
// Set the session token in the app.config object as well as localstorage | |
app.setSessionToken = function(token){ | |
app.config.sessionToken = token; | |
var tokenString = JSON.stringify(token); | |
localStorage.setItem('token',tokenString); | |
if(typeof(token) == 'object'){ | |
app.setLoggedInClass(true); | |
} else { | |
app.setLoggedInClass(false); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment