Skip to content

Commit 78244a6

Browse files
authored
Merge pull request #33 from auth0-samples/token-renewal
Token renewal
2 parents 2a60326 + 0c31b0d commit 78244a6

26 files changed

Lines changed: 7283 additions & 0 deletions

05-Token-Renewal/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
AUTH0_AUDIENCE={API_IDENTIFIER}
2+
AUTH0_DOMAIN={DOMAIN}

05-Token-Renewal/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# See https://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
6+
# testing
7+
/coverage
8+
9+
# production
10+
/build
11+
12+
# misc
13+
.DS_Store
14+
.env.local
15+
.env.development.local
16+
.env.test.local
17+
.env.production.local
18+
19+
npm-debug.log*
20+
yarn-debug.log*
21+
yarn-error.log*
22+
23+
auth0-variables.js
24+
.env

05-Token-Renewal/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Auth0 React Token Renewal
2+
3+
This sample demonstrates how to silently renew `access_token`s in a React application with Auth0. The sample uses the create-react-app.
4+
5+
## Getting Started
6+
7+
Create a new API in the [APIs section](https://manage.auth0.com/#/apis) and provide an identifier for it.
8+
9+
Clone the repo or download it from the React quickstart page in Auth0's documentation. Install create-react-app and the dependencies for the app.
10+
11+
```bash
12+
npm install -g create-react-app
13+
cd 05-Token-Renewal
14+
npm install
15+
```
16+
17+
## Set the Client ID, Domain, and API URL
18+
19+
If you download the sample from the quickstart page, it will come pre-populated with the **client ID** and **domain** for your application. If you clone the repo directly from Github, rename the `auth0-variables.js.example` file to `auth0-variables.js` and provide the **client ID** and **domain** there. This file is located in `src/Auth/`.
20+
21+
You should also provide the identifier for the API you create in the Auth0 dashboard as your `apiUrl`.
22+
23+
## Set Up the `silent.html` File
24+
25+
If you download the sample from the quickstart page, it will come pre-populated with the **client ID** and **domain** for your application. If you clone the repo directly from Github, edit `silent.html` and add your **client ID** and **domain**.
26+
27+
## Run the Application
28+
29+
The development server that comes with create-react-app can be used to serve the application.
30+
31+
```bash
32+
npm start
33+
```
34+
35+
The application will be served at `http://localhost:3000`.
36+
37+
## Making It Live
38+
39+
To make the silent authentication work on a live environment, you'll need to edit the two `localhost` urls in `silent.html` and the one `localhost` url you have in the `auth0-variables.ts` file.
40+
41+
## Troubleshooting
42+
If you see an error on renewal saying `login_required`, that means you may be using the Auth0 dev keys for whichever social login you're testing. You'll need to add your own keys for this to work.
43+
44+
## What is Auth0?
45+
46+
Auth0 helps you to:
47+
48+
* Add authentication with [multiple authentication sources](https://docs.auth0.com/identityproviders), either social like **Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce, amont others**, or enterprise identity systems like **Windows Azure AD, Google Apps, Active Directory, ADFS or any SAML Identity Provider**.
49+
* Add authentication through more traditional **[username/password databases](https://docs.auth0.com/mysql-connection-tutorial)**.
50+
* Add support for **[linking different user accounts](https://docs.auth0.com/link-accounts)** with the same user.
51+
* Support for generating signed [Json Web Tokens](https://docs.auth0.com/jwt) to call your APIs and **flow the user identity** securely.
52+
* Analytics of how, when and where users are logging in.
53+
* Pull data from other sources and add it to the user profile, through [JavaScript rules](https://docs.auth0.com/rules).
54+
55+
## Create a free Auth0 account
56+
57+
1. Go to [Auth0](https://auth0.com/signup) and click Sign Up.
58+
2. Use Google, GitHub or Microsoft Account to login.
59+
60+
## Issue Reporting
61+
62+
If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The [Responsible Disclosure Program](https://auth0.com/whitehat) details the procedure for disclosing security issues.
63+
64+
## Author
65+
66+
[Auth0](auth0.com)
67+
68+
## License
69+
70+
This project is licensed under the MIT license. See the [LICENSE](LICENSE.txt) file for more info.
71+

05-Token-Renewal/package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "token-renewal",
3+
"version": "0.1.0",
4+
"private": true,
5+
"devDependencies": {
6+
"react-scripts": "1.0.10"
7+
},
8+
"dependencies": {
9+
"auth0-js": "^8.8.0",
10+
"connect-static-file": "^1.2.0",
11+
"cors": "^2.8.4",
12+
"express": "^4.15.3",
13+
"npm-run-all": "^4.0.2",
14+
"react": "^15.6.1",
15+
"react-bootstrap": "^0.31.2",
16+
"react-dom": "^15.6.1",
17+
"react-router": "^4.1.2",
18+
"react-router-dom": "^4.1.2"
19+
},
20+
"scripts": {
21+
"start": "npm-run-all --parallel server:start client:start",
22+
"client:start": "react-scripts start",
23+
"server:start": "node server.js",
24+
"build": "react-scripts build",
25+
"test": "react-scripts test --env=jsdom",
26+
"eject": "react-scripts eject"
27+
}
28+
}
24.3 KB
Binary file not shown.

05-Token-Renewal/public/index.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
6+
<meta name="theme-color" content="#000000">
7+
<!--
8+
manifest.json provides metadata used when your web app is added to the
9+
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
10+
-->
11+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
12+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
13+
<!--
14+
Notice the use of %PUBLIC_URL% in the tags above.
15+
It will be replaced with the URL of the `public` folder during the build.
16+
Only files inside the `public` folder can be referenced from the HTML.
17+
18+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
19+
work correctly both with client-side routing and a non-root public URL.
20+
Learn how to configure a non-root public URL by running `npm run build`.
21+
-->
22+
<title>React App</title>
23+
</head>
24+
<body>
25+
<noscript>
26+
You need to enable JavaScript to run this app.
27+
</noscript>
28+
<div id="root"></div>
29+
<!--
30+
This HTML file is a template.
31+
If you open it directly in the browser, you will see an empty page.
32+
33+
You can add webfonts, meta tags, or analytics to this file.
34+
The build step will place the bundled scripts into the <body> tag.
35+
36+
To begin the development, run `npm start` or `yarn start`.
37+
To create a production bundle, use `npm run build` or `yarn build`.
38+
-->
39+
</body>
40+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "192x192",
8+
"type": "image/png"
9+
}
10+
],
11+
"start_url": "./index.html",
12+
"display": "standalone",
13+
"theme_color": "#000000",
14+
"background_color": "#ffffff"
15+
}

05-Token-Renewal/server.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const express = require('express');
2+
const app = express();
3+
const cors = require('cors');
4+
const staticFile = require('connect-static-file');
5+
6+
app.use(cors());
7+
app.use('/silent', staticFile(`${__dirname}/silent.html`));
8+
9+
app.listen(3001);
10+
console.log('Server listening on http://localhost:3001. The React app will be built and served at http://localhost:4200.');

05-Token-Renewal/silent.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<script src="https://cdn.auth0.com/js/auth0/8.8/auth0.min.js"></script>
6+
<script>
7+
var AUTH0_CLIENT_ID = '{CLIENT_ID}';
8+
var AUTH0_DOMAIN = '{DOMAIN}';
9+
10+
if (!AUTH0_CLIENT_ID || !AUTH0_DOMAIN) {
11+
alert('Make sure to set the AUTH0_CLIENT_ID and AUTH0_DOMAIN variables in silent.html.');
12+
}
13+
14+
var webAuth = new auth0.WebAuth({
15+
domain: AUTH0_DOMAIN,
16+
clientID: AUTH0_CLIENT_ID,
17+
scope: 'openid profile',
18+
responseType: 'token id_token',
19+
redirectUri: 'http://localhost:3000'
20+
});
21+
</script>
22+
<script>
23+
webAuth.parseHash(window.location.hash, function (err, response) {
24+
parent.postMessage(err || response, 'http://localhost:3000');
25+
});
26+
</script>
27+
</head>
28+
<body></body>
29+
</html>
30+

05-Token-Renewal/src/App.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.btn-margin {
2+
margin: 7px 3px;
3+
}

0 commit comments

Comments
 (0)