|
| 1 | +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +/////////////////////////////// |
| 5 | +// UI event handlers |
| 6 | +/////////////////////////////// |
| 7 | +document.getElementById('clearState').addEventListener("click", clearState, false); |
| 8 | +document.getElementById('getUser').addEventListener("click", getUser, false); |
| 9 | +document.getElementById('removeUser').addEventListener("click", removeUser, false); |
| 10 | +document.getElementById('querySessionStatus').addEventListener("click", querySessionStatus, false); |
| 11 | + |
| 12 | +document.getElementById('startSigninMainWindow').addEventListener("click", startSigninMainWindow, false); |
| 13 | +document.getElementById('endSigninMainWindow').addEventListener("click", endSigninMainWindow, false); |
| 14 | +document.getElementById('startSigninMainWindowDiffCallbackPage').addEventListener("click", startSigninMainWindowDiffCallbackPage, false); |
| 15 | + |
| 16 | +document.getElementById('popupSignin').addEventListener("click", popupSignin, false); |
| 17 | +document.getElementById('iframeSignin').addEventListener("click", iframeSignin, false); |
| 18 | + |
| 19 | +document.getElementById('startSignoutMainWindow').addEventListener("click", startSignoutMainWindow, false); |
| 20 | +document.getElementById('endSignoutMainWindow').addEventListener("click", endSignoutMainWindow, false); |
| 21 | + |
| 22 | +document.getElementById('popupSignout').addEventListener("click", popupSignout, false); |
| 23 | + |
| 24 | +/////////////////////////////// |
| 25 | +// config |
| 26 | +/////////////////////////////// |
| 27 | +Oidc.Log.logger = console; |
| 28 | +Oidc.Log.level = Oidc.Log.DEBUG; |
| 29 | + |
| 30 | +var settings = { |
| 31 | + //authority: 'https://demo.identityserver.io/', |
| 32 | + authority: 'http://localhost:5000/', |
| 33 | + client_id: 'js_code', |
| 34 | + redirect_uri: 'http://localhost:15000/code-identityserver-sample.html', |
| 35 | + post_logout_redirect_uri: 'http://localhost:15000/code-identityserver-sample.html', |
| 36 | + response_type: 'code', |
| 37 | + scope: 'openid profile email api1', |
| 38 | + |
| 39 | + popup_redirect_uri:'http://localhost:15000/code-identityserver-sample-popup-signin.html', |
| 40 | + popup_post_logout_redirect_uri:'http://localhost:15000/code-identityserver-sample-popup-signout.html', |
| 41 | + |
| 42 | + silent_redirect_uri:'http://localhost:15000/code-identityserver-sample-silent.html', |
| 43 | + automaticSilentRenew:true, |
| 44 | + //silentRequestTimeout:10000, |
| 45 | + |
| 46 | + filterProtocolClaims: true, |
| 47 | + loadUserInfo: true |
| 48 | +}; |
| 49 | +var mgr = new Oidc.UserManager(settings); |
| 50 | + |
| 51 | +/////////////////////////////// |
| 52 | +// events |
| 53 | +/////////////////////////////// |
| 54 | +mgr.events.addAccessTokenExpiring(function () { |
| 55 | + console.log("token expiring"); |
| 56 | + log("token expiring"); |
| 57 | +}); |
| 58 | + |
| 59 | +mgr.events.addAccessTokenExpired(function () { |
| 60 | + console.log("token expired"); |
| 61 | + log("token expired"); |
| 62 | +}); |
| 63 | + |
| 64 | +mgr.events.addSilentRenewError(function (e) { |
| 65 | + console.log("silent renew error", e.message); |
| 66 | + log("silent renew error", e.message); |
| 67 | +}); |
| 68 | + |
| 69 | +mgr.events.addUserLoaded(function (user) { |
| 70 | + console.log("user loaded", user); |
| 71 | + mgr.getUser().then(function(){ |
| 72 | + console.log("getUser loaded user after userLoaded event fired"); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +mgr.events.addUserUnloaded(function (e) { |
| 77 | + console.log("user unloaded"); |
| 78 | +}); |
| 79 | + |
| 80 | +/////////////////////////////// |
| 81 | +// functions for UI elements |
| 82 | +/////////////////////////////// |
| 83 | +function clearState(){ |
| 84 | + mgr.clearStaleState().then(function(){ |
| 85 | + log("clearStateState success"); |
| 86 | + }).catch(function(e){ |
| 87 | + log("clearStateState error", e.message); |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +function getUser() { |
| 92 | + mgr.getUser().then(function(user) { |
| 93 | + log("got user", user); |
| 94 | + }).catch(function(err) { |
| 95 | + log(err); |
| 96 | + }); |
| 97 | +} |
| 98 | + |
| 99 | +function removeUser() { |
| 100 | + mgr.removeUser().then(function() { |
| 101 | + log("user removed"); |
| 102 | + }).catch(function(err) { |
| 103 | + log(err); |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +function startSigninMainWindow() { |
| 108 | + mgr.signinRedirect({state:'some data'}).then(function() { |
| 109 | + log("signinRedirect done"); |
| 110 | + }).catch(function(err) { |
| 111 | + log(err); |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +function endSigninMainWindow() { |
| 116 | + mgr.signinRedirectCallback().then(function(user) { |
| 117 | + log("signed in", user); |
| 118 | + }).catch(function(err) { |
| 119 | + log(err); |
| 120 | + }); |
| 121 | +} |
| 122 | + |
| 123 | +function startSigninMainWindowDiffCallbackPage() { |
| 124 | + mgr.signinRedirect({state:'some data', redirect_uri: 'http://localhost:15000/code-identityserver-sample-callback.html'}).then(function() { |
| 125 | + log("signinRedirect done"); |
| 126 | + }).catch(function(err) { |
| 127 | + log(err); |
| 128 | + }); |
| 129 | +} |
| 130 | + |
| 131 | +function popupSignin() { |
| 132 | + mgr.signinPopup({state:'some data'}).then(function(user) { |
| 133 | + log("signed in", user); |
| 134 | + }).catch(function(err) { |
| 135 | + log(err); |
| 136 | + }); |
| 137 | +} |
| 138 | + |
| 139 | +function popupSignout() { |
| 140 | + mgr.signoutPopup({state:'some data'}).then(function() { |
| 141 | + log("signed out"); |
| 142 | + }).catch(function(err) { |
| 143 | + log(err); |
| 144 | + }); |
| 145 | +} |
| 146 | + |
| 147 | +function iframeSignin() { |
| 148 | + mgr.signinSilent({state:'some data'}).then(function(user) { |
| 149 | + log("signed in", user); |
| 150 | + }).catch(function(err) { |
| 151 | + log(err); |
| 152 | + }); |
| 153 | +} |
| 154 | + |
| 155 | +function querySessionStatus() { |
| 156 | + mgr.querySessionStatus().then(function(status) { |
| 157 | + log("user's session status", status); |
| 158 | + }).catch(function(err) { |
| 159 | + log(err); |
| 160 | + }); |
| 161 | +} |
| 162 | + |
| 163 | +function startSignoutMainWindow(){ |
| 164 | + mgr.signoutRedirect({state:'some data'}).then(function(resp) { |
| 165 | + //mgr.signoutRedirect().then(function(resp) { |
| 166 | + log("signed out", resp); |
| 167 | + }).catch(function(err) { |
| 168 | + log(err); |
| 169 | + }); |
| 170 | +}; |
| 171 | + |
| 172 | +function endSignoutMainWindow(){ |
| 173 | + mgr.signoutRedirectCallback().then(function(resp) { |
| 174 | + log("signed out", resp); |
| 175 | + }).catch(function(err) { |
| 176 | + log(err); |
| 177 | + }); |
| 178 | +}; |
0 commit comments