Skip to content

Commit 862a813

Browse files
committed
remove localstorage from 04 and 05
1 parent 6a3f5c7 commit 862a813

10 files changed

Lines changed: 162 additions & 109 deletions

File tree

01-Login/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
"dependencies": {
66
"auth0-js": "^9.0.0",
77
"bootstrap": "^3.3.7",
8-
"react": "^15.5.4",
9-
"react-bootstrap": "^0.31.0",
10-
"react-dom": "^15.5.4",
11-
"react-router": "^4.1.1",
12-
"react-router-dom": "^4.1.1",
8+
"react": "^15.6.1",
9+
"react-bootstrap": "^0.31.2",
10+
"react-dom": "^15.6.1",
11+
"react-router": "^4.1.2",
12+
"react-router-dom": "^4.1.2",
1313
"react-scripts": "^2.1.1"
1414
},
1515
"scripts": {

02-User-Profile/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
"dependencies": {
66
"auth0-js": "^9.0.0",
77
"bootstrap": "^3.3.7",
8-
"react": "^15.5.4",
9-
"react-bootstrap": "^0.31.0",
10-
"react-dom": "^15.5.4",
11-
"react-router": "^4.1.1",
12-
"react-router-dom": "^4.1.1",
8+
"react": "^15.6.1",
9+
"react-bootstrap": "^0.31.2",
10+
"react-dom": "^15.6.1",
11+
"react-router": "^4.1.2",
12+
"react-router-dom": "^4.1.2",
1313
"react-scripts": "^2.1.1"
1414
},
1515
"scripts": {

03-Calling-an-API/package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
"jwks-rsa": "^1.1.1",
1414
"morgan": "^1.8.2",
1515
"npm-run-all": "^4.0.2",
16-
1716
"auth0-js": "^9.0.0",
1817
"bootstrap": "^3.3.7",
19-
"react": "^15.5.4",
20-
"react-bootstrap": "^0.31.0",
21-
"react-dom": "^15.5.4",
22-
"react-router": "^4.1.1",
23-
"react-router-dom": "^4.1.1",
18+
"react": "^15.6.1",
19+
"react-bootstrap": "^0.31.2",
20+
"react-dom": "^15.6.1",
21+
"react-router": "^4.1.2",
22+
"react-router-dom": "^4.1.2",
2423
"react-scripts": "^2.1.1"
2524
},
2625
"scripts": {

04-Authorization/package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "authorization",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"private": true,
55
"dependencies": {
66
"axios": "^0.16.2",
@@ -13,14 +13,13 @@
1313
"jwks-rsa": "^1.1.1",
1414
"morgan": "^1.8.2",
1515
"npm-run-all": "^4.0.2",
16-
1716
"auth0-js": "^9.0.0",
1817
"bootstrap": "^3.3.7",
19-
"react": "^15.5.4",
20-
"react-bootstrap": "^0.31.0",
21-
"react-dom": "^15.5.4",
22-
"react-router": "^4.1.1",
23-
"react-router-dom": "^4.1.1",
18+
"react": "^15.6.1",
19+
"react-bootstrap": "^0.31.2",
20+
"react-dom": "^15.6.1",
21+
"react-router": "^4.1.2",
22+
"react-router-dom": "^4.1.2",
2423
"react-scripts": "^2.1.1"
2524
},
2625
"scripts": {

04-Authorization/src/App.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ class App extends Component {
1515
this.props.auth.logout();
1616
}
1717

18+
componentDidMount() {
19+
const { renewSession } = this.props.auth;
20+
21+
if (localStorage.getItem('isLoggedIn') === 'true') {
22+
renewSession();
23+
}
24+
}
25+
1826
render() {
1927
const { isAuthenticated, userHasScopes } = this.props.auth;
2028

04-Authorization/src/Auth/Auth.js

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { AUTH_CONFIG } from './auth0-variables';
33
import history from '../history';
44

55
export default class Auth {
6+
accessToken;
7+
idToken;
8+
expiresAt;
69
userProfile;
10+
scopes;
711
requestedScopes = 'openid profile read:messages write:messages';
812

913
auth0 = new auth0.WebAuth({
@@ -22,6 +26,8 @@ export default class Auth {
2226
this.isAuthenticated = this.isAuthenticated.bind(this);
2327
this.userHasScopes = this.userHasScopes.bind(this);
2428
this.getAccessToken = this.getAccessToken.bind(this);
29+
this.getIdToken = this.getIdToken.bind(this);
30+
this.renewSession = this.renewSession.bind(this);
2531
this.getProfile = this.getProfile.bind(this);
2632
}
2733

@@ -33,7 +39,6 @@ export default class Auth {
3339
this.auth0.parseHash((err, authResult) => {
3440
if (authResult && authResult.accessToken && authResult.idToken) {
3541
this.setSession(authResult);
36-
history.replace('/home');
3742
} else if (err) {
3843
history.replace('/home');
3944
console.log(err);
@@ -42,36 +47,46 @@ export default class Auth {
4247
});
4348
}
4449

50+
getAccessToken() {
51+
return this.accessToken;
52+
}
53+
54+
getIdToken() {
55+
return this.idToken;
56+
}
57+
4558
setSession(authResult) {
59+
// Set isLoggedIn flag in localStorage
60+
localStorage.setItem('isLoggedIn', 'true');
61+
4662
// Set the time that the access token will expire at
47-
let expiresAt = JSON.stringify(
48-
authResult.expiresIn * 1000 + new Date().getTime()
49-
);
50-
// If there is a value on the `scope` param from the authResult,
51-
// use it to set scopes in the session for the user. Otherwise
52-
// use the scopes as requested. If no scopes were requested,
53-
// set it to nothing
54-
const scopes = authResult.scope || this.requestedScopes || '';
55-
56-
localStorage.setItem('access_token', authResult.accessToken);
57-
localStorage.setItem('id_token', authResult.idToken);
58-
localStorage.setItem('expires_at', expiresAt);
59-
localStorage.setItem('scopes', JSON.stringify(scopes));
63+
let expiresAt = (authResult.expiresIn * 1000) + new Date().getTime();
64+
this.accessToken = authResult.accessToken;
65+
this.idToken = authResult.idToken;
66+
this.expiresAt = expiresAt;
67+
68+
// Set the users scopes
69+
this.scopes = authResult.scope || this.requestedScopes || '';
70+
6071
// navigate to the home route
6172
history.replace('/home');
6273
}
6374

64-
getAccessToken() {
65-
const accessToken = localStorage.getItem('access_token');
66-
if (!accessToken) {
67-
throw new Error('No access token found');
68-
}
69-
return accessToken;
75+
renewSession() {
76+
this.auth0.checkSession({}, (err, authResult) => {
77+
console.log(err, authResult);
78+
if (authResult && authResult.accessToken && authResult.idToken) {
79+
this.setSession(authResult);
80+
} else if (err) {
81+
this.logout();
82+
console.log(err);
83+
alert(`Could not get a new token (${err.error}: ${err.error_description}).`);
84+
}
85+
});
7086
}
7187

7288
getProfile(cb) {
73-
let accessToken = this.getAccessToken();
74-
this.auth0.client.userInfo(accessToken, (err, profile) => {
89+
this.auth0.client.userInfo(this.accessToken, (err, profile) => {
7590
if (profile) {
7691
this.userProfile = profile;
7792
}
@@ -80,25 +95,33 @@ export default class Auth {
8095
}
8196

8297
logout() {
83-
// Clear access token and ID token from local storage
84-
localStorage.removeItem('access_token');
85-
localStorage.removeItem('id_token');
86-
localStorage.removeItem('expires_at');
87-
localStorage.removeItem('scopes');
98+
// Remove tokens and expiry time
99+
this.accessToken = null;
100+
this.idToken = null;
101+
this.expiresAt = 0;
102+
103+
// Remove user scopes
104+
this.scopes = null;
105+
106+
// Remove user profile
88107
this.userProfile = null;
108+
109+
// Remove isLoggedIn flag from localStorage
110+
localStorage.removeItem('isLoggedIn');
111+
89112
// navigate to the home route
90113
history.replace('/home');
91114
}
92115

93116
isAuthenticated() {
94117
// Check whether the current time is past the
95118
// access token's expiry time
96-
let expiresAt = JSON.parse(localStorage.getItem('expires_at'));
119+
let expiresAt = this.expiresAt;
97120
return new Date().getTime() < expiresAt;
98121
}
99122

100123
userHasScopes(scopes) {
101-
const grantedScopes = (JSON.parse(localStorage.getItem('scopes')) || '').split(' ');
124+
const grantedScopes = this.scopes.split(' ');
102125
return scopes.every(scope => grantedScopes.includes(scope));
103126
}
104127
}

05-Token-Renewal/package.json

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
{
22
"name": "token-renewal",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"private": true,
5-
"devDependencies": {
6-
"react-scripts": "1.0.10"
7-
},
85
"dependencies": {
96
"auth0-js": "^9.0.0",
107
"bootstrap": "^3.3.7",
118
"react": "^15.6.1",
129
"react-bootstrap": "^0.31.2",
1310
"react-dom": "^15.6.1",
1411
"react-router": "^4.1.2",
15-
"react-router-dom": "^4.1.2"
12+
"react-router-dom": "^4.1.2",
13+
"react-scripts": "^2.1.1"
1614
},
1715
"scripts": {
18-
"start": "npm run client:start",
19-
"client:start": "react-scripts start",
16+
"start": "react-scripts start",
2017
"build": "react-scripts build",
2118
"test": "react-scripts test --env=jsdom",
2219
"eject": "react-scripts eject"
23-
}
20+
},
21+
"eslintConfig": {
22+
"extends": "react-app"
23+
},
24+
"browserslist": [
25+
">0.2%",
26+
"not dead",
27+
"not ie <= 11",
28+
"not op_mini all"
29+
]
2430
}

05-Token-Renewal/src/App.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ class App extends Component {
1616
}
1717

1818
renewToken() {
19-
this.props.auth.renewToken();
19+
const { renewSession } = this.props.auth;
20+
renewSession();
21+
}
22+
23+
componentDidMount() {
24+
const { renewSession } = this.props.auth;
25+
26+
if (localStorage.getItem('isLoggedIn') === 'true') {
27+
renewSession();
28+
}
2029
}
2130

2231
render() {

0 commit comments

Comments
 (0)