Skip to content

Instantly share code, notes, and snippets.

@mckiersey
Created March 13, 2021 17:24
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 mckiersey/590c49a1f800c0a1ed374c9e2802d0b8 to your computer and use it in GitHub Desktop.
Save mckiersey/590c49a1f800c0a1ed374c9e2802d0b8 to your computer and use it in GitHub Desktop.
<script>
// ******************************** JAVASCRIPT ******************************** //
// GET COOKIE FUNCTION
function getCookieValue(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
//Session Status
var cookieToken = getCookieValue('USER_SESSION_TOKEN')
console.log('Session status: cookie token value:', cookieToken)
if (cookieToken == "") {
document.getElementById('SessionStatusText').innerHTML = "<span style='color: red;'>Not Signed in</span>";
} else {
document.getElementById('SessionStatusText').innerHTML =
"<span style='color: RGB(170, 204, 0);'>Signed In</span>";
}
//SIGN IN/ SIGN UP: SET COOKIE VALUE IN BROWSER
function onSignIn(googleUser) {
var id_token = googleUser.getAuthResponse().id_token;
console.log("Token value to post to BE for validation: ", id_token)
// POST NEW USER TO BACK END;
try {
$.post('http://localhost:80/SignIn', {
token: id_token
}).done(function (data) {
console.log('Server response :', data)
});
} catch (err) {
console.log('failed to post to backend')
response.send('Error: ' + err)
}
}
// DESCRIPTION:
// 1) Take token from cookie in browser
// 2) Send token to BE
// 3) If BE verifies token is valid, take user ID and redirect Profile Page
function ProfileRoute() {
// Get token from browser
var CookieToken = getCookieValue('USER_SESSION_TOKEN')
try {
$.post('http://localhost:80/ProfileRoute', {
token: CookieToken
}).done(function (data) {
VerificationStatus = data[0]
if (VerificationStatus == '* Token verification SUCCESS: User logged in *') {
alert('Verification success')
user_id = data[1]
// Redirect back to BackEnd to render profile page
var BaseProfiledUrl = 'http://localhost/ProfilePage?user_id='
var ProfileUrl = BaseProfiledUrl + user_id
window.location.href = ProfileUrl
} else {
console.log('Server response :', data)
alert('Please sign in')
window.location.href = 'http://localhost:80/Home'
};
});
} catch (err) {
console.log('failed to post to backend')
response.send('Error: ' + err)
window.location.href = 'http://localhost:80/home'
}
}
function signOut() {
var getUrl = `http://localhost:80/SignOut`
try {
$.get(getUrl, {})
.done(function (data) {
if (data = 'CookieDeleted') {
window.location.href = "http://localhost:80/LoggedOutPage"
} else {
alert('Something went wrong, please try to sign out again.')
}
});
} catch (err) {
console.log('failed to post to backend')
response.send('Error: ' + err)
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment