forked from camicroscope/caMicroscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_login.html
82 lines (77 loc) · 2.82 KB
/
google_login.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<html>
<meta name="google-signin-client_id" content="539699277901-99iheradvd3fo53aln49o5j493jahkpi.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
<div class="g-signin2" data-onsuccess="onSignIn"></div>
<a href="#" onclick="signOut();">Sign out</a>
<script>
// This function is invoked when sign out button is clicked. It signs out the existing user
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
</script>
<script>
/* The variables are -
cookie_name : "token" is expected by elevate router
base_deployment_url : for storing the base url
redirect_uri : redirecting link to login.html
default_redirect : The default redirecting link is to table.html
signup_redirect : This link stores the redirect link to signup page
*/
var cookie_name = "token"
var base_deployment_url = window.location.toString().split("/").slice(0,-1).join("/")
var redirect_uri = base_deployment_url + "/login.html"
var default_redirect = base_deployment_url + "/apps/table.html"
var signup_redirect = base_deployment_url + "/apps/signup/signup.html"
let state = decodeURIComponent(getUrlParam("state"))
if (!state || state == "null"){
state = default_redirect
}
// google's certs are here: https://www.googleapis.com/oauth2/v1/certs; remember to replace literal \n with linebreak
function randomString(length) {
var bytes = new Uint8Array(length);
var random = window.crypto.getRandomValues(bytes);
var result = [];
var charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._~'
random.forEach(function (c) {
result.push(charset[c % charset.length]);
});
return result.join('');
}
function getUrlParam(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&#]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
// The function is called when sign in option is called
function onSignIn(googleUser) {
var id_token = googleUser.getAuthResponse().id_token;
console.info(id_token)
// trade for camic token
if (id_token){
document.cookie = cookie_name + "=" + id_token;
fetch("./auth/Token/check",
{headers: {
'Authorization': "Bearer " + id_token
}}
).then(x=>x.json()).then(x=>{
console.log("{id provider", id_token)
console.log("{auth service}", x)
if (x.hasOwnProperty('token')){
document.cookie = cookie_name + "=" + x.token;
window.location = state
} else {
window.alert("User not added.")
window.location = signup_redirect
}
})
}
}
</script>
<html>