Skip to content

Commit 839447d

Browse files
committed
1.0.0
0 parents  commit 839447d

File tree

10,523 files changed

+782831
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

10,523 files changed

+782831
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Server Side Rendering React on Total.js
2+
3+
## Installation
4+
5+
1. Install the all necesary modules and packages in project directory
6+
7+
```
8+
npm install
9+
```
10+
11+
2. Install babel-node package
12+
13+
```
14+
npm install babel-node -g
15+
```
16+
17+
4. run webpack for React.js and Auth0 in /client. Bundled files will be copied to /public directory
18+
19+
```
20+
npm run webpack
21+
```
22+
23+
5. Start the server from the root directory of project
24+
25+
```
26+
babel-node server.js
27+
```
28+
29+
6. Visit http://localhost:8000
30+

client/.babelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"plugins": [
3+
["transform-runtime", {
4+
"polyfill": false,
5+
"regenerator": true
6+
}]
7+
]
8+
}

client/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
REACT_APP_AUTH0_CLIENT_ID = 'J2Yxx9kSZ4llxOtbMjpiAGGM2P30EdQE'
2+
REACT_APP_AUTH0_DOMAIN = 'jrbsystem.auth0.com'

client/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# React + Auth0
2+
3+
Sceleton of integration React with Auth0 module
4+
5+
## Installation
6+
7+
1. Install modules in project directory
8+
9+
```
10+
npm install
11+
```
12+
13+
```
14+
15+
2. Start the server from the root directory of project
16+
17+
```
18+
npm run start
19+
```
20+
21+
6. Visit http://localhost:3000
22+

client/package.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"name": "react-auth0",
3+
"version": "0.1.0",
4+
"private": true,
5+
"devDependencies": {
6+
"auth0-react-scripts": "0.6.20",
7+
"babel-plugin-transform-runtime": "^6.22.0"
8+
},
9+
"dependencies": {
10+
"auth0-lock": "^10.11.0",
11+
"babel-core": "6.22.0",
12+
"babel-plugin-react-transform": "^2.0.2",
13+
"babel-plugin-transform-class-properties": "^6.22.0",
14+
"babel-plugin-transform-decorators-legacy": "^1.3.4",
15+
"babel-plugin-transform-runtime": "^6.22.0",
16+
"babel-preset-es2015": "^6.22.0",
17+
"babel-preset-react": "6.22.0",
18+
"babel-preset-stage-2": "^6.22.0",
19+
"babel-runtime": "^6.22.0",
20+
"classnames": "^2.2.5",
21+
"copy-webpack-plugin": "^3.0.1",
22+
"css-loader": "^0.23.1",
23+
"dotenv": "^4.0.0",
24+
"events": "^1.1.1",
25+
"express": "^4.14.1",
26+
"express-jwt": "^5.1.0",
27+
"extract-text-webpack-plugin": "^1.0.1",
28+
"file-loader": "^0.9.0",
29+
"fs": "0.0.1-security",
30+
"html-webpack-plugin": "^2.22.0",
31+
"immutable": "^3.8.1",
32+
"json-loader": "^0.5.4",
33+
"jwt-decode": "^2.1.0",
34+
"less": "^2.7.1",
35+
"less-loader": "^2.2.3",
36+
"react": "^15.4.2",
37+
"react-dom": "^15.4.2",
38+
"react-redux": "^4.4.5",
39+
"react-router": "^3.0.2",
40+
"react-slider": "^0.6.1",
41+
"react-transform-hmr": "^1.0.4",
42+
"redux": "^3.5.2",
43+
"redux-immutable": "^3.0.8",
44+
"style-loader": "^0.13.1",
45+
"url-loader": "^0.5.7",
46+
"webpack": "^1.13.1",
47+
"webpack-dev-server": "^1.14.1"
48+
},
49+
"scripts": {
50+
"webpack": "webpack --config webpack.config.js --progress --profile --colors",
51+
"start": "react-scripts start",
52+
"build": "react-scripts build",
53+
"test": "react-scripts test --env=jsdom",
54+
"eject": "react-scripts eject"
55+
},
56+
"proxy": "http://localhost:8000"
57+
}

client/public/favicon.ico

24.3 KB
Binary file not shown.

client/public/index.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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">
6+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
7+
<!--
8+
Notice the use of %PUBLIC_URL% in the tag above.
9+
It will be replaced with the URL of the `public` folder during the build.
10+
Only files inside the `public` folder can be referenced from the HTML.
11+
12+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
13+
work correctly both with client-side routing and a non-root public URL.
14+
Learn how to configure a non-root public URL by running `npm run build`.
15+
-->
16+
<title>React App + Auth0 on Total.js</title>
17+
</head>
18+
<body>
19+
<div id="root"></div>
20+
<!--
21+
This HTML file is a template.
22+
If you open it directly in the browser, you will see an empty page.
23+
24+
You can add webfonts, meta tags, or analytics to this file.
25+
The build step will place the bundled scripts into the <body> tag.
26+
27+
To begin the development, run `npm start`.
28+
To create a production bundle, use `npm run build`.
29+
-->
30+
</body>
31+
</html>

client/src/auth.js

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
import decode from 'jwt-decode';
2+
import {EventEmitter} from 'events';
3+
import React, {Component, PropTypes} from 'react';
4+
import {browserHistory} from 'react-router';
5+
import Auth0Lock from 'auth0-lock';
6+
7+
8+
const NEXT_PATH_KEY = 'next_path';
9+
const ID_TOKEN_KEY = 'id_token';
10+
const ACCESS_TOKEN_KEY = 'access_token';
11+
const PROFILE_KEY = 'profile';
12+
const LOGIN_ROUTE = '/login';
13+
const ROOT_ROUTE = '/';
14+
15+
const REACT_APP_AUTH0_CLIENT_ID = process.env.REACT_APP_AUTH0_CLIENT_ID;
16+
const REACT_APP_AUTH0_DOMAIN = process.env.REACT_APP_AUTH0_DOMAIN ;
17+
18+
19+
20+
if (!REACT_APP_AUTH0_CLIENT_ID || !REACT_APP_AUTH0_DOMAIN) {
21+
throw new Error('Please define `REACT_APP_AUTH0_CLIENT_ID` and `REACT_APP_AUTH0_DOMAIN` in your .env file');
22+
}
23+
24+
const lock = new Auth0Lock(
25+
REACT_APP_AUTH0_CLIENT_ID,
26+
REACT_APP_AUTH0_DOMAIN, {
27+
auth: {
28+
redirectUrl: `${window.location.origin}${LOGIN_ROUTE}`,
29+
responseType: 'token'
30+
}
31+
}
32+
);
33+
34+
const events = new EventEmitter();
35+
36+
lock.on('authenticated', authResult => {
37+
setIdToken(authResult.idToken);
38+
setAccessToken(authResult.accessToken);
39+
lock.getUserInfo(authResult.accessToken, (error, profile) => {
40+
if (error) { return setProfile({error}); }
41+
setProfile(profile);
42+
browserHistory.push(getNextPath());
43+
clearNextPath();
44+
});
45+
});
46+
47+
export function login(options) {
48+
lock.show(options);
49+
50+
return {
51+
hide() {
52+
lock.hide();
53+
}
54+
}
55+
}
56+
57+
export function logout() {
58+
clearNextPath();
59+
clearIdToken();
60+
clearProfile();
61+
browserHistory.push(LOGIN_ROUTE);
62+
}
63+
64+
export function requireAuth(nextState, replace) {
65+
if (!isLoggedIn()) {
66+
setNextPath(nextState.location.pathname);
67+
replace({pathname: LOGIN_ROUTE});
68+
}
69+
}
70+
71+
export function connectProfile(WrappedComponent) {
72+
return class ProfileContainer extends Component {
73+
state = {
74+
profile: null
75+
};
76+
77+
componentWillMount() {
78+
this.profileSubscription = subscribeToProfile((profile) => {
79+
this.setState({profile});
80+
});
81+
}
82+
83+
componentWillUnmount() {
84+
this.profileSubscription.close();
85+
}
86+
87+
render() {
88+
return (
89+
<WrappedComponent
90+
{...this.props}
91+
profile={this.state.profile}
92+
onUpdateProfile={this.onUpdateProfile}
93+
/>
94+
);
95+
}
96+
97+
onUpdateProfile = (newProfile) => {
98+
return updateProfile(this.state.profile.user_id, newProfile);
99+
}
100+
};
101+
}
102+
103+
connectProfile.PropTypes = {
104+
profile: PropTypes.object,
105+
onUpdateProfile: PropTypes.func
106+
};
107+
108+
export function fetchAsUser(input, init={}) {
109+
const headers = init.headers || {};
110+
111+
return fetch(input, {
112+
...init,
113+
headers: {
114+
'Accept': 'application/json',
115+
'Content-Type': 'application/json',
116+
'Authorization': `Bearer ${getIdToken()}`,
117+
...headers
118+
}
119+
}).then((response) => {
120+
if (!response.ok) { throw new Error(response); }
121+
return response;
122+
});
123+
}
124+
125+
function subscribeToProfile(subscription) {
126+
events.on('profile_updated', subscription);
127+
128+
if (isLoggedIn()) {
129+
subscription(getProfile());
130+
131+
lock.getUserInfo(getAccessToken(), (error, profile) => {
132+
if (error) { return setProfile({error}); }
133+
setProfile(profile);
134+
});
135+
}
136+
137+
return {
138+
close() {
139+
events.removeListener('profile_updated', subscription);
140+
}
141+
};
142+
}
143+
144+
async function updateProfile(userId, newProfile) {
145+
try {
146+
const response = await fetchAsUser(`https://${REACT_APP_AUTH0_DOMAIN}/api/v2/users/${userId}`, {
147+
method: 'PATCH',
148+
body: JSON.stringify(newProfile)
149+
});
150+
151+
const profile = await response.json();
152+
setProfile(profile);
153+
} catch (error) {
154+
return error;
155+
}
156+
}
157+
158+
function setProfile(profile) {
159+
localStorage.setItem(PROFILE_KEY, JSON.stringify(profile));
160+
events.emit('profile_updated', profile);
161+
}
162+
163+
function getProfile() {
164+
return JSON.parse(localStorage.getItem(PROFILE_KEY));
165+
}
166+
167+
function clearProfile() {
168+
localStorage.removeItem(PROFILE_KEY);
169+
events.emit('profile_updated', null);
170+
}
171+
172+
function setIdToken(idToken) {
173+
localStorage.setItem(ID_TOKEN_KEY, idToken);
174+
}
175+
176+
function setAccessToken(accessToken) {
177+
localStorage.setItem(ACCESS_TOKEN_KEY, accessToken);
178+
}
179+
180+
function getIdToken() {
181+
return localStorage.getItem(ID_TOKEN_KEY);
182+
}
183+
184+
function getAccessToken() {
185+
return localStorage.getItem(ACCESS_TOKEN_KEY);
186+
}
187+
188+
function clearIdToken() {
189+
localStorage.removeItem(ID_TOKEN_KEY);
190+
}
191+
192+
function setNextPath(nextPath) {
193+
localStorage.setItem(NEXT_PATH_KEY, nextPath);
194+
}
195+
196+
function getNextPath() {
197+
return localStorage.getItem(NEXT_PATH_KEY) || ROOT_ROUTE;
198+
}
199+
200+
function clearNextPath() {
201+
localStorage.removeItem(NEXT_PATH_KEY);
202+
}
203+
204+
function isLoggedIn() {
205+
const idToken = getIdToken();
206+
return idToken && !isTokenExpired(idToken);
207+
}
208+
209+
function getTokenExpirationDate(encodedToken) {
210+
const token = decode(encodedToken);
211+
if (!token.exp) { return null; }
212+
213+
const date = new Date(0);
214+
date.setUTCSeconds(token.exp);
215+
216+
return date;
217+
}
218+
219+
function isTokenExpired(token) {
220+
const expirationDate = getTokenExpirationDate(token);
221+
return expirationDate < new Date();
222+
}

0 commit comments

Comments
 (0)