Skip to content

Commit a6dab12

Browse files
committed
Improve the user sign in session
1 parent 628da79 commit a6dab12

File tree

5 files changed

+53
-26
lines changed

5 files changed

+53
-26
lines changed

.env.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ REACT_APP_API_VERIFY_TOKEN_URL=http://localhost:7770/verify-token
99
REACT_APP_API_SESSIONS_URL=http://localhost:7770/sessions
1010
REACT_APP_API_USERS_URL=http://localhost:7770/users
1111

12-
REACT_APP_API_JWT_SECRET=jwtsecretcode
12+
REACT_APP_HASH=356a192b7913b04c54574d18c28d46e6395428ab.da4b9237bacccdf19c0760cab7aec4a8359010b0.77de68daecd823babbb58edb1c8e14d7106e83bb.1b6453892473a467d07372d45eb05abc2031647a.ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ REACT_APP_API_SIGN_OUT_URL=http://localhost:7770/sign-out
1919
REACT_APP_API_VERIFY_TOKEN_URL=http://localhost:7770/verify-token
2020
REACT_APP_API_SESSIONS_URL=http://localhost:7770/sessions
2121
REACT_APP_API_USERS_URL=http://localhost:7770/users
22-
REACT_APP_API_JWT_SECRET=jwtsecretcode
22+
REACT_APP_HASH=sha1hash1.sha1hash2.sha1hash3
2323
```
2424

25+
NOTE: Generate random hashes and concatenate the hashes with “.”.
26+
The same hashes should also be used in the API app.
27+
The more hashes, the better.
28+
2529
### Config Files
2630

2731
Copy `./src/Config.dist` folder to `Config` and change the configurations if needed.
@@ -71,20 +75,20 @@ Note: To view the Docker containers, open another terminal console then enter `d
7175

7276
### Docker
7377

74-
| Command | Description |
75-
|----------------------------------------|------------------------------------------------------------------------|
76-
| `./bin/install` | Build the Docker container and start the app |
77-
| `./bin/reinstall` | Rebuild the Docker container with the current branch and start the app |
78-
| `./bin/start` | Start the client app service |
79-
| `./bin/stop` | Stop the client app service |
80-
| `./bin/console <container ID or Name>` | Access the terminal console of the container |
78+
| Command | Description |
79+
|--------------------------------------|------------------------------------------------------------------------|
80+
| `bin/install` | Build the Docker container and start the app |
81+
| `bin/reinstall` | Rebuild the Docker container with the current branch and start the app |
82+
| `bin/start` | Start the client app service |
83+
| `bin/stop` | Stop the client app service |
84+
| `bin/console <container ID or Name>` | Access the terminal console of the container |
8185

8286
### CSS
8387

84-
| Command | Description |
85-
|-------------------|---------------------------------------------------------------------|
86-
| `./bin/css/watch` | Watch and compile *.scss files on file changes (for Mac users only) |
87-
| `./bin/css/build` | Manually compile *.scss files |
88+
| Command | Description |
89+
|-----------------|---------------------------------------------------------------------|
90+
| `bin/css/watch` | Watch and compile *.scss files on file changes (for Mac users only) |
91+
| `bin/css/build` | Manually compile *.scss files |
8892

8993
## Available API App Boilerplates
9094

src/Components/Buttons/SignOut.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class SignOut extends Component {
1111
Axios
1212
.post(process.env.REACT_APP_API_SIGN_OUT_URL)
1313
.then(response => {
14-
Session.deleteToken()
14+
Session.deleteTokens()
1515
closeNavbar()
1616
this.props.auth(false)
1717
})

src/Components/Forms/SignIn.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ export default class SignIn extends Component {
3535
showAlertMessage: true
3636
})
3737

38-
const token = JWT.sign(formData, process.env.REACT_APP_API_JWT_SECRET)
38+
const hash = Session.hash()
39+
const token = JWT.sign(formData, hash[0])
40+
const tkid = hash[1]
3941

4042
Axios
41-
.post(process.env.REACT_APP_API_SIGN_IN_URL, { token })
43+
.post(process.env.REACT_APP_API_SIGN_IN_URL, { token, tkid })
4244
.then(response => {
43-
const { token, redirect } = response.data
45+
const { token, data, redirect, tkid } = response.data
4446

4547
Session.store({ token })
48+
Session.store({ data })
49+
Session.store({ tkid })
4650

4751
this.setState({
4852
alertMessage: {

src/Lib/Helpers/Session.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash'
12
import Store from 'store'
23
import JWT from 'jsonwebtoken'
34
import Axios from '../Common/Axios'
@@ -8,6 +9,18 @@ export function store(data) {
89
}
910
}
1011

12+
export function hash(index) {
13+
const hashList = process.env.REACT_APP_HASH.split('.')
14+
15+
if (index) index = parseInt(index.toString().charAt(index.toString().length - 1), 10)
16+
else index = _.random(0, hashList.length - 1)
17+
18+
return [
19+
hashList[index],
20+
[_.random(1111111, 9999999), index].join('')
21+
]
22+
}
23+
1124
export function isSignedIn() {
1225
token()
1326
? Axios.defaults.headers.common['Authorization'] = `Bearer ${token()}`
@@ -46,8 +59,8 @@ export function userRole() {
4659
/* PAGE ACCESS */
4760

4861
export function showPage(path) {
49-
const allowedPaths = tokenData('allowedPaths')
50-
const excludedPaths = tokenData('excludedPaths')
62+
const allowedPaths = getData('allowedPaths')
63+
const excludedPaths = getData('excludedPaths')
5164

5265
if (excludedPaths && excludedPaths.length > 0 && excludedPaths.indexOf(path) > -1) return false
5366
if (allowedPaths && allowedPaths.toString() === '*') return true
@@ -75,28 +88,34 @@ export function verifyToken() {
7588
Axios
7689
.get(process.env.REACT_APP_API_VERIFY_TOKEN_URL)
7790
.catch(error => {
78-
deleteToken()
91+
deleteTokens()
7992
window.location.reload()
8093
})
8194
}, 1000);
8295
}
8396

97+
export function dataToken() {
98+
return Store.get('data')
99+
}
100+
84101
export function token() {
85102
return Store.get('token')
86103
}
87104

88-
export function deleteToken() {
105+
export function deleteTokens() {
106+
Store.remove('data')
89107
Store.remove('token')
108+
Store.remove('tkid')
90109
}
91110

92111
export function decodedToken() {
93-
if (token()) {
112+
if (dataToken()) {
94113
return JWT.verify(
95-
token(),
96-
process.env.REACT_APP_API_JWT_SECRET,
114+
dataToken(),
115+
hash(Store.get('tkid'))[0],
97116
function(errors, decoded) {
98117
if (errors) {
99-
deleteToken()
118+
deleteTokens()
100119
return false
101120
}
102121

@@ -106,6 +125,6 @@ export function decodedToken() {
106125
}
107126
}
108127

109-
export function tokenData(data) {
128+
export function getData(data) {
110129
return decodedToken() && decodedToken()[data] ? decodedToken()[data] : null
111130
}

0 commit comments

Comments
 (0)