Skip to content

Commit 4e5440a

Browse files
committed
Call refresh on init
1 parent 405495d commit 4e5440a

File tree

7 files changed

+101
-134
lines changed

7 files changed

+101
-134
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "freenit",
3-
"version": "0.1.5",
3+
"version": "0.1.13",
44
"private": false,
55
"description": "Freenit framework",
66
"author": "Goran Mekić",

src/App.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import React from 'react'
2-
import { StoreProvider, store } from 'store-provider'
32
import { BrowserRouter as Router } from 'react-router-dom'
43
import { ThemeProvider } from '@material-ui/styles'
54
import { Style } from 'radium'
65

6+
import { StoreProvider, store } from './store-provider'
77
import Routing from './routing'
88
import styles from './styles'
99
import theme from './theme'
10-
import { auth } from './auth'
11-
12-
13-
auth.init('/api/v0')
1410

1511

1612
const App = () => {

src/Provider.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/auth.js

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
1+
import { makeAutoObservable } from 'mobx'
12
import { rest } from './utils'
23

34

4-
class Auth
5-
{
6-
access = { expire: null, date: null }
7-
refresh = { expire: null, data: null }
5+
class AuthStore {
6+
tokens = {
7+
access: { expire: null, date: null },
8+
refresh: { expire: null, data: null },
9+
}
10+
initialized = false
11+
12+
constructor() {
13+
makeAutoObservable(this)
14+
}
815

916
init = async (api) => {
1017
window.rest = rest(api)
1118
window.rest.API_ROOT = api
12-
await this.refresh()
19+
await this.refresh(true)
20+
this.initialized = true
1321
}
1422

1523
login = async (email, password) => {
1624
try {
1725
const response = await window.rest.post('/auth/login', {email, password})
1826
const { accessExpire, refreshExpire } = response.data
1927
const now = new Date()
20-
this.access.expire = Number(accessExpire)
21-
this.access.date = new Date(now.getTime() + this.access.expire * 1000)
22-
this.refresh.expire = Number(refreshExpire)
23-
this.refresh.date = new Date(now.getTime() + this.refresh.expire * 1000)
24-
const result = {
25-
access: this.access,
26-
refresh: this.refresh,
27-
ok: true,
28-
}
29-
return result
28+
const ae = Number(accessExpire)
29+
const re = Number(refreshExpire)
30+
this.tokens.access.expire = ae
31+
this.tokens.access.date = new Date(now.getTime() + ae * 1000)
32+
this.tokens.refresh.expire = re
33+
this.tokens.refresh.date = new Date(now.getTime() + re * 1000)
34+
return { ...this.tokens, ok: true }
3035
} catch (error) {
3136
return { ...error, ok: false }
3237
}
@@ -35,41 +40,39 @@ class Auth
3540
logout = async () => {
3641
try {
3742
await window.rest.post('/auth/logout', {})
38-
this.access.expire = null
39-
this.access.date = null
40-
this.refresh.expire = null
41-
this.refresh.date = null
43+
this.tokens.access = { expire: null, date: null }
44+
this.tokens.refresh = { expire: null, date: null }
4245
return { ok: true }
4346
} catch (error) {
4447
return { ...error, ok: false }
4548
}
4649
}
4750

48-
refresh = async () => {
51+
refresh = async (initialized = false) => {
52+
if (!this.initialized && !initialized) { return { ok: false } }
53+
if (this.authenticated()) { return { ok: true } }
4954
try {
5055
const response = await window.rest.post('/auth/refresh', {})
5156
const { accessExpire, refreshExpire } = response.data
5257
const now = new Date()
53-
this.access.expire = Number(accessExpire)
54-
this.access.date = new Date(now.getTime() + this.access.expire * 1000)
55-
this.refresh.expire = Number(refreshExpire)
56-
this.refresh.date = new Date(now.getTime() + this.refresh.expire * 1000)
57-
return {
58-
access: this.access,
59-
refresh: this.access,
60-
ok: true,
61-
}
58+
const ae = Number(accessExpire)
59+
const re = Number(refreshExpire)
60+
this.tokens.access.expire = ae
61+
this.tokens.access.date = new Date(now.getTime() + ae * 1000)
62+
this.tokens.refresh.expire = re
63+
this.tokens.refresh.date = new Date(now.getTime() + re * 1000)
64+
return { ...this.tokens, ok: true }
6265
} catch (error) {
6366
return { ...error, ok: false }
6467
}
6568
}
6669

6770
authenticated = () => {
68-
if (!this.access.expire) { return false }
69-
if (!this.refresh.expire) { return false }
71+
if (!this.tokens.access.expire) { return false }
72+
if (!this.tokens.refresh.expire) { return false }
7073
const now = new Date()
71-
if (now > this.access.date) { return false }
72-
if (now > this.refresh.date) { return false }
74+
if (now > this.tokens.access.date) { return false }
75+
if (now > this.tokens.refresh.date) { return false }
7376
return true
7477
}
7578

@@ -80,7 +83,43 @@ class Auth
8083
}
8184
return await fn(...data)
8285
}
86+
87+
confirm = async (token) => {
88+
try {
89+
await window.rest.get(`/auth/register/${token}`)
90+
return { ok: true }
91+
} catch (error) {
92+
return { ...error, ok: false }
93+
}
94+
}
95+
96+
register = async (email, password) => {
97+
try {
98+
await window.rest.post('/auth/register', { email, password })
99+
return { ok: true }
100+
} catch (error) {
101+
return { ...error, ok: false }
102+
}
103+
}
104+
105+
reset = async email => {
106+
try {
107+
await window.rest.post('/auth/reset/request', { email })
108+
return { ok: true }
109+
} catch (error) {
110+
return { ...error, ok: false }
111+
}
112+
}
113+
114+
changePassword = async (password, token) => {
115+
try {
116+
await window.rest.post('/auth/reset', { password, token })
117+
return { ok: true }
118+
} catch (error) {
119+
return { ...error, ok: false }
120+
}
121+
}
83122
}
84123

85124

86-
export const auth = new Auth()
125+
export const auth = new AuthStore()

src/pages/auth/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Confirm from './confirm'
33
import Login from './login'
44
import Register from './register'
55
import Reset from './reset'
6-
import Store from './store'
6+
import { auth as store } from '../../auth'
77

88

99
const auth = {
@@ -12,7 +12,7 @@ const auth = {
1212
Login,
1313
Register,
1414
Reset,
15-
store: new Store(),
15+
store,
1616
}
1717

1818

src/pages/auth/store.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/templates/empty/detail.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
import React from 'react'
22
import { Button, Snackbar } from '@material-ui/core'
3-
import { auth } from '../../auth'
43
import { withStore } from '../../store'
54

65

7-
const EmptyTemplate = props => {
8-
const { history, notification } = props.store
9-
if (props.secure && !auth.authenticated()) {
10-
history.push('/')
11-
return null
6+
class EmptyTemplate extends React.Component {
7+
render() {
8+
const { auth, history, notification } = this.props.store
9+
if (auth.initialized && this.props.secure && !auth.authenticated()) {
10+
history.push('/')
11+
return null
12+
}
13+
return (
14+
<div style={this.props.style}>
15+
{this.props.children}
16+
<Snackbar
17+
autoHideDuration={5000}
18+
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
19+
open={Boolean(notification.message)}
20+
onClose={notification.close}
21+
message={notification.message}
22+
action={(
23+
<Button color="secondary" size="small" onClick={notification.close}>
24+
CLOSE
25+
</Button>
26+
)}
27+
/>
28+
</div>
29+
)
1230
}
13-
return (
14-
<div style={props.style}>
15-
{props.children}
16-
<Snackbar
17-
autoHideDuration={5000}
18-
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
19-
open={Boolean(notification.message)}
20-
onClose={notification.close}
21-
message={notification.message}
22-
action={(
23-
<Button color="secondary" size="small" onClick={notification.close}>
24-
CLOSE
25-
</Button>
26-
)}
27-
/>
28-
</div>
29-
)
3031
}
3132

3233

0 commit comments

Comments
 (0)