@campxdev/shared 1.7.13 → 1.7.14

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@campxdev/shared",
3
- "version": "1.7.13",
3
+ "version": "1.7.14",
4
4
  "main": "./exports.ts",
5
5
  "scripts": {
6
6
  "start": "react-scripts start",
@@ -18,8 +18,16 @@ import { FormTextField } from './HookForm'
18
18
  import axios from '../config/axios'
19
19
  import ResetPassword from './ResetPassword'
20
20
  import { Link } from 'react-router-dom'
21
+ import { DialogButton } from './ModalButtons'
22
+ import adminAxios from '../utils/adminAxios'
21
23
 
22
- export function LoginForm({ loginUrl }: { loginUrl?: string }) {
24
+ export function LoginForm({
25
+ loginUrl,
26
+ showSuperAdminForm,
27
+ }: {
28
+ loginUrl?: string
29
+ showSuperAdminForm?: boolean
30
+ }) {
23
31
  const [showPassword, setShowPassword] = useState(false)
24
32
  const [forgotMail, setForgotMail] = useState(null)
25
33
  const [forgotPassword, setForgotPassword] = useState(true)
@@ -72,7 +80,7 @@ export function LoginForm({ loginUrl }: { loginUrl?: string }) {
72
80
  }, [])
73
81
 
74
82
  return (
75
- <>
83
+ <Box sx={{ position: 'relative', height: '100%' }}>
76
84
  {resetPassword ? (
77
85
  <>
78
86
  <ResetPassword />
@@ -80,37 +88,33 @@ export function LoginForm({ loginUrl }: { loginUrl?: string }) {
80
88
  ) : forgotPassword ? (
81
89
  <form onSubmit={handleSubmit(onSubmit)}>
82
90
  <Stack gap={'30px'} sx={{ display: resetPassword ? 'none' : 'flex' }}>
83
- <Box>
84
- <FormTextField
85
- control={control}
86
- name="username"
87
- label="User ID"
88
- required
89
- />
90
- </Box>
91
- <Box>
92
- <FormTextField
93
- control={control}
94
- name="password"
95
- label="Password"
96
- type={showPassword ? 'text' : 'password'}
97
- required
98
- InputProps={{
99
- endAdornment: (
100
- <InputAdornment position="end">
101
- <IconButton
102
- size="small"
103
- aria-label="toggle password visibility"
104
- onClick={() => setShowPassword((prev) => !prev)}
105
- edge="end"
106
- >
107
- {showPassword ? <VisibilityOff /> : <Visibility />}
108
- </IconButton>
109
- </InputAdornment>
110
- ),
111
- }}
112
- />
113
- </Box>
91
+ <FormTextField
92
+ control={control}
93
+ name="username"
94
+ label="User ID"
95
+ required
96
+ />
97
+ <FormTextField
98
+ control={control}
99
+ name="password"
100
+ label="Password"
101
+ type={showPassword ? 'text' : 'password'}
102
+ required
103
+ InputProps={{
104
+ endAdornment: (
105
+ <InputAdornment position="end">
106
+ <IconButton
107
+ size="small"
108
+ aria-label="toggle password visibility"
109
+ onClick={() => setShowPassword((prev) => !prev)}
110
+ edge="end"
111
+ >
112
+ {showPassword ? <VisibilityOff /> : <Visibility />}
113
+ </IconButton>
114
+ </InputAdornment>
115
+ ),
116
+ }}
117
+ />
114
118
  <ActionButton type="submit">Login</ActionButton>
115
119
  <Typography
116
120
  variant="h6"
@@ -159,7 +163,27 @@ export function LoginForm({ loginUrl }: { loginUrl?: string }) {
159
163
  {error}
160
164
  </Alert>
161
165
  )}
162
- </>
166
+
167
+ {showSuperAdminForm && (
168
+ <Box
169
+ sx={{
170
+ position: 'absolute',
171
+ bottom: '20px',
172
+ right: '20px',
173
+ }}
174
+ >
175
+ <DialogButton
176
+ title="Super Admin Login"
177
+ anchor={({ open }) => (
178
+ <Button onClick={open} size="small" variant="text">
179
+ Super Admin Login
180
+ </Button>
181
+ )}
182
+ content={({ close }) => <SuperAdminLoginForm close={close} />}
183
+ />
184
+ </Box>
185
+ )}
186
+ </Box>
163
187
  )
164
188
  }
165
189
 
@@ -169,3 +193,82 @@ export const StyledLink = styled(Link)({
169
193
  })
170
194
 
171
195
  export default LoginForm
196
+
197
+ const SuperAdminLoginForm = ({ close }) => {
198
+ const [showPassword, setShowPassword] = useState(false)
199
+ const [loading, setLoading] = useState(false)
200
+ const { handleSubmit, control } = useForm({
201
+ defaultValues: null,
202
+ })
203
+ const [error, setError] = useState('')
204
+
205
+ const onSubmit = async (values) => {
206
+ setLoading(true)
207
+ try {
208
+ const res = await adminAxios.post(`/auth/login`, values)
209
+ Cookies.set('campx_admin_session_key', res?.data?.cookie)
210
+
211
+ setLoading(false)
212
+ window.location.href = '/campx'
213
+ } catch (err) {
214
+ setLoading(false)
215
+ setError(err.response.data.message ?? 'Server Error')
216
+ }
217
+ }
218
+
219
+ const onError = (e) => {
220
+ console.log(e)
221
+ }
222
+
223
+ return (
224
+ <Box width={'100%'} marginTop="20px">
225
+ <form onSubmit={handleSubmit(onSubmit, onError)}>
226
+ <Stack gap={'40px'}>
227
+ <Typography variant="h6" textAlign={'center'}>
228
+ Sign in to Super Admin Account
229
+ </Typography>
230
+ <FormTextField
231
+ control={control}
232
+ name="username"
233
+ label="Username/Email"
234
+ required
235
+ />
236
+ <FormTextField
237
+ control={control}
238
+ name="password"
239
+ label="Password"
240
+ type={showPassword ? 'text' : 'password'}
241
+ required
242
+ InputProps={{
243
+ endAdornment: (
244
+ <InputAdornment position="end">
245
+ <IconButton
246
+ size="small"
247
+ aria-label="toggle password visibility"
248
+ onClick={() => setShowPassword((prev) => !prev)}
249
+ edge="end"
250
+ >
251
+ {showPassword ? <VisibilityOff /> : <Visibility />}
252
+ </IconButton>
253
+ </InputAdornment>
254
+ ),
255
+ }}
256
+ />
257
+ <ActionButton
258
+ type="submit"
259
+ fullWidth
260
+ disabled={loading}
261
+ loading={loading}
262
+ >
263
+ Login
264
+ </ActionButton>
265
+ </Stack>
266
+ </form>
267
+ {error && (
268
+ <Alert severity="error" sx={{ marginTop: '20px' }}>
269
+ {error}
270
+ </Alert>
271
+ )}
272
+ </Box>
273
+ )
274
+ }
@@ -8,14 +8,19 @@ const LoginContext = createContext<{
8
8
  openLoginForm: (loginUrl: string) => {},
9
9
  })
10
10
 
11
- export default function LoginFormProvider({ children }) {
11
+ export default function LoginFormProvider({ children, showSuperAdminForm }) {
12
12
  const modal = useModal()
13
13
 
14
14
  const onLogin = (loginUrl: string) => {
15
15
  modal({
16
16
  title: 'Developer Login',
17
17
  content({ close }) {
18
- return <LoginForm loginUrl={loginUrl} />
18
+ return (
19
+ <LoginForm
20
+ loginUrl={loginUrl}
21
+ showSuperAdminForm={showSuperAdminForm}
22
+ />
23
+ )
19
24
  },
20
25
  })
21
26
  }
@@ -13,7 +13,13 @@ import LoginFormProvider from './LoginFormProvider'
13
13
  export const campxTenantKey = Cookies.get('campx_tenant')
14
14
  export const urlTenantKey = window.location.pathname.split('/')[1]
15
15
 
16
- export default function Providers({ children }: { children: ReactNode }) {
16
+ export default function Providers({
17
+ children,
18
+ showSuperAdminLoginForm = false,
19
+ }: {
20
+ children: ReactNode
21
+ showSuperAdminLoginForm?: boolean
22
+ }) {
17
23
  useEffect(() => {
18
24
  if (!urlTenantKey) {
19
25
  if (campxTenantKey) {
@@ -30,7 +36,7 @@ export default function Providers({ children }: { children: ReactNode }) {
30
36
  <MuiThemeProvider>
31
37
  <ConfirmContextProvider>
32
38
  <DialogProvider>
33
- <LoginFormProvider>
39
+ <LoginFormProvider showSuperAdminForm={showSuperAdminLoginForm}>
34
40
  {children}
35
41
  <GlobalNetworkLoadingIndicator />
36
42
  </LoginFormProvider>
@@ -0,0 +1,15 @@
1
+ import Axios from 'axios'
2
+ import Cookies from 'js-cookie'
3
+
4
+ const sessionKey = Cookies.get('campx_admin_session_key')
5
+ let adminAxios = Axios.create({
6
+ baseURL: process.env.REACT_APP_TENANT_API_HOST,
7
+ withCredentials: true,
8
+ headers: {
9
+ ...(sessionKey && {
10
+ campx_admin_session_key: sessionKey,
11
+ }),
12
+ },
13
+ })
14
+
15
+ export default adminAxios