@campxdev/shared 1.11.7-1.alpha.3 → 1.11.7-1.alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/config/axios.ts +3 -0
- package/src/hooks/useAuth.ts +71 -81
package/package.json
CHANGED
package/src/config/axios.ts
CHANGED
package/src/hooks/useAuth.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { AxiosError } from 'axios'
|
|
2
1
|
import Cookies from 'js-cookie'
|
|
3
2
|
import { useEffect, useState } from 'react'
|
|
4
3
|
import { useNavigate } from 'react-router-dom'
|
|
@@ -77,7 +76,6 @@ const getInstitutionKey = () => {
|
|
|
77
76
|
return instituitionKey
|
|
78
77
|
}
|
|
79
78
|
}
|
|
80
|
-
const navigate = useNavigate()
|
|
81
79
|
|
|
82
80
|
function handleInstitutions(institutions) {
|
|
83
81
|
if (institutions?.length === 0) {
|
|
@@ -96,95 +94,87 @@ function handleInstitutions(institutions) {
|
|
|
96
94
|
}
|
|
97
95
|
}
|
|
98
96
|
|
|
99
|
-
const
|
|
97
|
+
const useAuth = ({
|
|
98
|
+
permissionsEndpoint,
|
|
100
99
|
loginUrl,
|
|
101
|
-
|
|
102
|
-
err,
|
|
103
|
-
}: {
|
|
104
|
-
loginUrl: string
|
|
105
|
-
setLoading?: any
|
|
106
|
-
err: AxiosError
|
|
107
|
-
}) => {
|
|
108
|
-
setLoading(false)
|
|
109
|
-
|
|
110
|
-
if (isDevelopment) {
|
|
111
|
-
openRootModal({
|
|
112
|
-
key: 'login',
|
|
113
|
-
contentData: {
|
|
114
|
-
loginUrl,
|
|
115
|
-
},
|
|
116
|
-
dialogProps: {
|
|
117
|
-
disableEscapeKeyDown: true,
|
|
118
|
-
onClose: () => {},
|
|
119
|
-
},
|
|
120
|
-
})
|
|
121
|
-
return
|
|
122
|
-
} else {
|
|
123
|
-
navigate('/auth/login')
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
|
|
100
|
+
}: AuthParams): AuthResponse => {
|
|
128
101
|
const [loading, setLoading] = useState<boolean>(false)
|
|
129
|
-
const [data, setData] = useState(null)
|
|
102
|
+
const [data, setData] = useState<any>(null)
|
|
130
103
|
const { current } = InstitutionsStore.useState()
|
|
104
|
+
const navigate = useNavigate()
|
|
131
105
|
|
|
132
106
|
const appInit = async () => {
|
|
133
107
|
setLoading(true)
|
|
134
|
-
|
|
135
|
-
.get(permissionsEndpoint)
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
PermissionsStore.update((s) => {
|
|
149
|
-
s.permissions = {
|
|
150
|
-
...res.data?.permissions,
|
|
151
|
-
can_settings_view: 1,
|
|
152
|
-
can_dashboard_view: 1,
|
|
153
|
-
can_individual_pages_view: 1,
|
|
154
|
-
can_analatics_view: isAdmin,
|
|
155
|
-
can_admin_view: isAdmin,
|
|
156
|
-
can_my_mentees_view:
|
|
157
|
-
res.data?.user?.isMentor &&
|
|
158
|
-
res.data?.permissions.can_my_mentees_view,
|
|
159
|
-
} as any
|
|
160
|
-
s.applications = res.data?.applications ?? []
|
|
161
|
-
s.institutionType = res.data?.institutionType
|
|
162
|
-
s.masterInstitutionUniqueId = res.data?.masterInstitutionUniqueId
|
|
163
|
-
s.masterInstitutionId = res.data?.masterInstitutionId
|
|
164
|
-
s.isMasterInstitutionUser = res.data?.institutions
|
|
165
|
-
?.map((institution) => institution?.id)
|
|
166
|
-
?.includes(res?.data?.masterInstitutionUniqueId)
|
|
167
|
-
s.isMasterInstitution = res.data?.isMasterInstitution
|
|
168
|
-
s.isHomePageEnabled = res.data?.isHomePageEnabled
|
|
169
|
-
})
|
|
170
|
-
|
|
171
|
-
InstitutionsStore.update((s) => {
|
|
172
|
-
s.institutions = res.data?.institutions
|
|
173
|
-
s.current = res.data?.institutions.find(
|
|
174
|
-
(item) => item.code === Cookies.get('campx_institution'),
|
|
175
|
-
)
|
|
176
|
-
})
|
|
108
|
+
try {
|
|
109
|
+
const res = await axios.get(permissionsEndpoint)
|
|
110
|
+
const isAdmin = checkIsAdmin(res.data.user)
|
|
111
|
+
|
|
112
|
+
setData(res.data)
|
|
113
|
+
UserStore.update((s) => {
|
|
114
|
+
s.username = res.data?.user?.username
|
|
115
|
+
s.user = res.data?.user
|
|
116
|
+
s.roles = res.data?.roles
|
|
117
|
+
s.globalUserId = res.data?.globalUserId
|
|
118
|
+
s.fullName = res.data?.fullName
|
|
119
|
+
})
|
|
177
120
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
121
|
+
PermissionsStore.update((s) => {
|
|
122
|
+
s.permissions = {
|
|
123
|
+
...res.data?.permissions,
|
|
124
|
+
can_settings_view: 1,
|
|
125
|
+
can_dashboard_view: 1,
|
|
126
|
+
can_individual_pages_view: 1,
|
|
127
|
+
can_analatics_view: isAdmin,
|
|
128
|
+
can_admin_view: isAdmin,
|
|
129
|
+
can_my_mentees_view:
|
|
130
|
+
res.data?.user?.isMentor &&
|
|
131
|
+
res.data?.permissions.can_my_mentees_view,
|
|
132
|
+
}
|
|
133
|
+
s.applications = res.data?.applications ?? []
|
|
134
|
+
s.institutionType = res.data?.institutionType
|
|
135
|
+
s.masterInstitutionUniqueId = res.data?.masterInstitutionUniqueId
|
|
136
|
+
s.masterInstitutionId = res.data?.masterInstitutionId
|
|
137
|
+
s.isMasterInstitutionUser = res.data?.institutions
|
|
138
|
+
?.map((institution) => institution?.id)
|
|
139
|
+
?.includes(res?.data?.masterInstitutionUniqueId)
|
|
140
|
+
s.isMasterInstitution = res.data?.isMasterInstitution
|
|
141
|
+
s.isHomePageEnabled = res.data?.isHomePageEnabled
|
|
142
|
+
})
|
|
182
143
|
|
|
183
|
-
|
|
144
|
+
InstitutionsStore.update((s) => {
|
|
145
|
+
s.institutions = res.data?.institutions
|
|
146
|
+
s.current = res.data?.institutions.find(
|
|
147
|
+
(item) => item.code === Cookies.get('campx_institution'),
|
|
148
|
+
)
|
|
184
149
|
})
|
|
185
|
-
|
|
186
|
-
|
|
150
|
+
|
|
151
|
+
AssetsStore.update((s) => {
|
|
152
|
+
s.logo = res.data?.assets.logo
|
|
153
|
+
s.logo_square = res.data?.assets.logo_square
|
|
187
154
|
})
|
|
155
|
+
|
|
156
|
+
setLoading(false)
|
|
157
|
+
} catch (err: any) {
|
|
158
|
+
setLoading(false)
|
|
159
|
+
const origin = window.location.origin
|
|
160
|
+
const isStaging = origin.split('campx')[1] === '.dev'
|
|
161
|
+
|
|
162
|
+
if (isDevelopment || isStaging) {
|
|
163
|
+
openRootModal({
|
|
164
|
+
key: 'login',
|
|
165
|
+
contentData: {
|
|
166
|
+
loginUrl,
|
|
167
|
+
},
|
|
168
|
+
dialogProps: {
|
|
169
|
+
disableEscapeKeyDown: true,
|
|
170
|
+
onClose: () => {},
|
|
171
|
+
},
|
|
172
|
+
})
|
|
173
|
+
return
|
|
174
|
+
} else {
|
|
175
|
+
navigate('/auth/login')
|
|
176
|
+
}
|
|
177
|
+
}
|
|
188
178
|
}
|
|
189
179
|
|
|
190
180
|
useEffect(() => {
|