-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
176 lines (151 loc) · 5.26 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Single Page Application</title>
<script src="https://cdn.auth0.com/js/auth0/9.0.2/auth0.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<ul>
<li>
<a href="https://demonstration.auth0.com/authorize?scope=openid%20profile%20email%20read:contacts%20read:calendar&audience=organise&response_type=id_token%20token&client_id=OEVVkIGyOnTm5AgqQC08WJ3xQ6hzHNZh&redirect_uri=http://app1.com:3000&nonce=NONCE">
Sign In
</a>
</li>
<li>
<a id="authenticate" href="#">Sigin Auth0.js</a>
</li>
</ul>
<div id="app" style="display:none;">
<button id="get-profile">Get Profile JS</button>
<button id="get-profile-authentication-userinfo">Get Profile Auth</button>
<button id="get-profile-managementapi">Get Profile Mgmt API</button>
<button id="get-contacts">List Contacts</button>
<button id="get-appointments">List Appointments</button>
<div id="results"><pre></pre></div>
</div>
<script>
const AUTH0_DOMAIN = 'demonstration.auth0.com';
const AUTH0_CLIENT_ID = 'OEVVkIGyOnTm5AgqQC08WJ3xQ6hzHNZh';
const AUTH0_CONNECTION = 'Username-Password-Authentication';
if (!AUTH0_CLIENT_ID || !AUTH0_DOMAIN || !AUTH0_CONNECTION) {
alert('Make sure to set the AUTH0_CLIENT_ID, AUTH0_DOMAIN and AUTH0_CONNECTION variables in auth0-variables.js.');
}
const auth0WebAuth = new auth0.WebAuth({
domain: AUTH0_DOMAIN,
clientID: AUTH0_CLIENT_ID,
redirectUri: 'http://app1.com:3000',
responseType: 'id_token token',
// audience: `https://${AUTH0_DOMAIN}/userinfo`,
audience: `organise`,
scope: 'openid profile email read:contacts read:calendar'
});
const auth0Authentication = new auth0.Authentication( auth0WebAuth, {
domain: AUTH0_DOMAIN,
clientID: AUTH0_CLIENT_ID,
});
function getParameterByName(name) {
var match = RegExp('[#&]' + name + '=([^&]*)').exec(window.location.hash);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
function getAccessToken() {
return getParameterByName('access_token');
}
function getIdToken() {
return getParameterByName('id_token');
}
$(function () {
$('#authenticate').on('click', function(e) {
e.preventDefault();
auth0WebAuth.authorize()
});
});
$(function () {
var access_token = getAccessToken();
if (access_token) {
$('#app').show();
} else {
return
}
console.log('Your access token is ' + access_token)
$('#get-profile').on('click', function(e) {
e.preventDefault();
// Change to the port you're using
$.ajax({
url: "https://demonstration.auth0.com/userinfo",
method: "GET",
headers: { "Authorization": "Bearer " + access_token },
success: function (data) {
$('#results pre').text(JSON.stringify(data, null, 2))
}
});
});
$('#get-profile-authentication-userinfo').on('click', function(e) {
e.preventDefault();
auth0Authentication.userInfo(access_token, (err, usrInfo) => {
if (err) {
// handle error
console.error('Failed to get userInfo');
return;
}
$('#results pre').text(JSON.stringify(usrInfo, null, 2))
});
});
$('#get-profile-managementapi').on('click', function(e) {
e.preventDefault();
auth0WebAuth.checkSession({
audience: 'https://demonstration.auth0.com/api/v2/',
scope: 'read:current_user'
},
function(err, result) {
if (!err) {
var auth0Manage = new auth0.Management({
domain: AUTH0_DOMAIN,
token: result.accessToken
});
auth0Manage.getUser(result.idTokenPayload.sub, function(err, usrInfo) {
if (!err) {
// use userInfo
$('#results pre').text(JSON.stringify(usrInfo, null, 2))
}
else {
// handle error
}
});
}
else {
// handle error
}
}
);
});
// Use the access token to make API calls
$('#get-appointments').on('click', function(e) {
e.preventDefault();
// Change to the port you're using
$.ajax({
url: "http://localhost:3002/api/appointments",
method: "GET",
headers: { "Authorization": "Bearer " + access_token },
success: function (data) {
$('#results pre').text(JSON.stringify(data, null, 2))
}
});
});
$('#get-contacts').on('click', function(e) {
e.preventDefault();
// Change to the port you're using
$.ajax({
url: "http://localhost:3001/api/contacts",
method: "GET",
headers: { "Authorization": "Bearer " + access_token },
success: function (data) {
$('#results pre').text(JSON.stringify(data, null, 2))
}
});
});
});
</script>
</body>
</html>