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