|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <title>Auth0.js Demo Examples</title> |
| 5 | + <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> |
| 6 | + <script src="//cdn.auth0.com/js/auth0/development/auth0.min.js"></script> |
| 7 | + </head> |
| 8 | + <body class="container"> |
| 9 | + |
| 10 | + <h1>How to get and use a Refresh Token</h1> |
| 11 | + <div> |
| 12 | + <p>1. Get code for exchange</p> |
| 13 | + <input |
| 14 | + type="button" |
| 15 | + class="code-exchange-button" |
| 16 | + value="Generate code" |
| 17 | + /> |
| 18 | + <label for="code-exchange">Code:</label> |
| 19 | + <input id="code-exchange" type="text" value="" /> |
| 20 | + </div> |
| 21 | + |
| 22 | + |
| 23 | + <div> |
| 24 | + <p>2. Get new tokens using the code</p> |
| 25 | + <input |
| 26 | + type="button" |
| 27 | + class="get-token-using-code-button" |
| 28 | + value="Get" |
| 29 | + /> |
| 30 | + <pre id="code-exchange-result"></pre> |
| 31 | + </div> |
| 32 | + |
| 33 | + |
| 34 | + <div> |
| 35 | + <p>3. Get new tokens using the Refresh Token</p> |
| 36 | + <label for="refresh-token">Refresh token:</label> |
| 37 | + <input id="refresh-token" type="text" value="" /> |
| 38 | + |
| 39 | + <input |
| 40 | + type="button" |
| 41 | + class="get-new-token-button" |
| 42 | + value="Get Tokens" |
| 43 | + /> |
| 44 | + <p>New tokens:</p> |
| 45 | + <pre id="refresh-token-result"></pre> |
| 46 | + </div> |
| 47 | + |
| 48 | + <input type="button" class="logout-button" value="logout" /> |
| 49 | + |
| 50 | + <input type="button" class="reset-button" value="reset" /> |
| 51 | + |
| 52 | + <pre id="err"></pre> |
| 53 | + |
| 54 | + <script type="text/javascript"> |
| 55 | + const config = { |
| 56 | + audience: '{YOUR_AUDIENCE}', |
| 57 | + domain: '{YOUR_AUTH0_DOMAIN}', |
| 58 | + clientID: '{YOUR_AUTH0_CLIENT_ID}', |
| 59 | + redirectUri: '{YOUR_REDIRECT_URI}', |
| 60 | + } |
| 61 | + |
| 62 | + var webAuth = new auth0.WebAuth({ |
| 63 | + domain: config.domain, |
| 64 | + redirectUri: config.redirectUri, |
| 65 | + clientID: config.clientID, |
| 66 | + responseType: 'code', |
| 67 | + }); |
| 68 | + |
| 69 | + $('.code-exchange-button').click(function(e) { |
| 70 | + e.preventDefault(); |
| 71 | + |
| 72 | + webAuth.authorize({ |
| 73 | + audience: config.audience, |
| 74 | + scope: 'offline_access' |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + $('.get-token-using-code-button').click(function(e) { |
| 79 | + e.preventDefault(); |
| 80 | + const code = $('#code-exchange').val(); |
| 81 | + if (code !== '') { |
| 82 | + webAuth.client.oauthToken( |
| 83 | + { |
| 84 | + code, |
| 85 | + grantType: 'authorization_code', |
| 86 | + redirectUri: config.redirectUri, |
| 87 | + }, |
| 88 | + function(err, data) { |
| 89 | + let result = null; |
| 90 | + if (err) { |
| 91 | + const { statusCode, code, description } = err; |
| 92 | + result = { |
| 93 | + statusCode, |
| 94 | + code, |
| 95 | + description |
| 96 | + } |
| 97 | + } else { |
| 98 | + result = data; |
| 99 | + $('#refresh-token').val(data.refreshToken); |
| 100 | + } |
| 101 | + |
| 102 | + displayJson(result, '#code-exchange-result'); |
| 103 | + } |
| 104 | + ); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + $('.get-new-token-button').click(function(e) { |
| 109 | + e.preventDefault(); |
| 110 | + webAuth.client.oauthToken( |
| 111 | + { |
| 112 | + grantType: 'refresh_token', |
| 113 | + clientID: config.clientID, |
| 114 | + refresh_token: $('#refresh-token').val(), |
| 115 | + }, |
| 116 | + function(err, data) { |
| 117 | + let result = null; |
| 118 | + if (err) { |
| 119 | + const { statusCode, code, description } = err; |
| 120 | + result = { |
| 121 | + statusCode, |
| 122 | + code, |
| 123 | + description |
| 124 | + } |
| 125 | + } else { |
| 126 | + result = data; |
| 127 | + } |
| 128 | + |
| 129 | + displayJson(result, '#refresh-token-result'); |
| 130 | + } |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + $('.logout-button').click(function(e) { |
| 135 | + e.preventDefault(); |
| 136 | + webAuth.logout({ returnTo: config.redirectUri }); |
| 137 | + }); |
| 138 | + |
| 139 | + $('.reset-button').click(function(e) { |
| 140 | + e.preventDefault(); |
| 141 | + window.location.href = config.redirectUri; |
| 142 | + }); |
| 143 | + |
| 144 | + const displayJson = (json, locator) => { |
| 145 | + const jsonStr = JSON.stringify(json, null, 2); |
| 146 | + $(locator).append(jsonStr); |
| 147 | + }; |
| 148 | + |
| 149 | + $(document).ready(function() { |
| 150 | + var params = new URLSearchParams(window.location.search); |
| 151 | + |
| 152 | + if (params.get('code')) { |
| 153 | + $('#code-exchange').val(params.get('code')); |
| 154 | + } |
| 155 | + }); |
| 156 | + </script> |
| 157 | + </body> |
| 158 | +</html> |
0 commit comments