1
+ import { makeAutoObservable } from 'mobx'
1
2
import { rest } from './utils'
2
3
3
4
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
+ }
8
15
9
16
init = async ( api ) => {
10
17
window . rest = rest ( api )
11
18
window . rest . API_ROOT = api
12
- await this . refresh ( )
19
+ await this . refresh ( true )
20
+ this . initialized = true
13
21
}
14
22
15
23
login = async ( email , password ) => {
16
24
try {
17
25
const response = await window . rest . post ( '/auth/login' , { email, password} )
18
26
const { accessExpire, refreshExpire } = response . data
19
27
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 }
30
35
} catch ( error ) {
31
36
return { ...error , ok : false }
32
37
}
@@ -35,41 +40,39 @@ class Auth
35
40
logout = async ( ) => {
36
41
try {
37
42
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 }
42
45
return { ok : true }
43
46
} catch ( error ) {
44
47
return { ...error , ok : false }
45
48
}
46
49
}
47
50
48
- refresh = async ( ) => {
51
+ refresh = async ( initialized = false ) => {
52
+ if ( ! this . initialized && ! initialized ) { return { ok : false } }
53
+ if ( this . authenticated ( ) ) { return { ok : true } }
49
54
try {
50
55
const response = await window . rest . post ( '/auth/refresh' , { } )
51
56
const { accessExpire, refreshExpire } = response . data
52
57
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 }
62
65
} catch ( error ) {
63
66
return { ...error , ok : false }
64
67
}
65
68
}
66
69
67
70
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 }
70
73
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 }
73
76
return true
74
77
}
75
78
@@ -80,7 +83,43 @@ class Auth
80
83
}
81
84
return await fn ( ...data )
82
85
}
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
+ }
83
122
}
84
123
85
124
86
- export const auth = new Auth ( )
125
+ export const auth = new AuthStore ( )
0 commit comments