1
+ import { z } from 'zod' ;
2
+ import { useForm } from 'react-hook-form' ;
3
+ import { EyeIcon , EyeOffIcon } from 'lucide-react' ;
4
+ import { zodResolver } from '@hookform/resolvers/zod' ;
5
+ import { useEffect , useReducer , useState } from 'react' ;
6
+ import { createFileRoute , useNavigate } from '@tanstack/react-router' ;
7
+
8
+ import { Form } from '@/components/ui/form' ;
9
+ import { Input } from '@/components/ui/input' ;
1
10
import { Button } from '@/components/ui/button' ;
2
11
import {
3
12
FormField ,
@@ -6,20 +15,13 @@ import {
6
15
FormControl ,
7
16
FormMessageWithErrorIcon ,
8
17
} from '@/components/ui/form' ;
9
- import { Input } from '@/components/ui/input' ;
10
- import { Form } from '@/components/ui/form' ;
11
- import { cn } from '@/lib/utils' ;
12
- import { zodResolver } from '@hookform/resolvers/zod' ;
13
- import { EyeIcon , EyeOffIcon } from 'lucide-react' ;
14
- import { useEffect , useReducer , useState } from 'react' ;
15
- import { useForm } from 'react-hook-form' ;
16
- import { createFileRoute , useNavigate } from '@tanstack/react-router' ;
17
- import { z } from 'zod' ;
18
18
import { ConvoyLoader } from '@/components/convoy-loader' ;
19
- import * as loginService from '@/services/login.service' ;
20
- import * as signUpService from '@/services/signup.service' ;
21
- import * as organisationService from '@/services/organisations.service' ;
19
+
20
+ import { cn } from '@/lib/utils' ;
21
+
22
+ import * as authService from '@/services/auth.service' ;
22
23
import * as licensesService from '@/services/licenses.service' ;
24
+ import * as organisationService from '@/services/organisations.service' ;
23
25
24
26
import type { UseFormReturn } from 'react-hook-form' ;
25
27
@@ -182,7 +184,7 @@ function LoginWithSAMLButton() {
182
184
localStorage . setItem ( 'AUTH_TYPE' , 'login' ) ;
183
185
184
186
try {
185
- const res = await loginService . loginWithSAML ( ) ;
187
+ const res = await authService . loginWithSAML ( ) ;
186
188
const { redirectUrl } = res . data ;
187
189
window . open ( redirectUrl ) ;
188
190
} catch ( error ) {
@@ -229,18 +231,18 @@ function SignUpButton() {
229
231
230
232
type ReducerPayload = Partial < {
231
233
isSignUpEnabled : boolean ;
232
- isFetchingConfig : boolean ;
233
234
isLoadingProject : boolean ;
234
235
hasCreateUserLicense : boolean ;
235
236
isLoginButtonEnabled : boolean ;
237
+ isFetchingSignUpConfig : boolean ;
236
238
} > ;
237
239
238
240
const initialReducerState = {
239
241
isSignUpEnabled : false ,
240
- isFetchingConfig : false ,
241
242
isLoadingProject : false ,
242
243
isLoginButtonEnabled : true ,
243
244
hasCreateUserLicense : false ,
245
+ isFetchingSignUpConfig : false ,
244
246
} ;
245
247
246
248
function reducer ( state : ReducerPayload , payload : ReducerPayload ) {
@@ -255,8 +257,8 @@ function LoginPage() {
255
257
const [ state , dispatchState ] = useReducer ( reducer , initialReducerState ) ;
256
258
257
259
useEffect ( function ( ) {
258
- getSignUpConfig ( ) ;
259
- licensesService . setLicenses ( ) ;
260
+ getSignUpConfig ( ) . then ( ) ;
261
+ licensesService . setLicenses ( ) . then ( ) ;
260
262
const hasCreateUserLicense = licensesService . hasLicense ( 'CREATE_USER' ) ;
261
263
dispatchState ( { hasCreateUserLicense } ) ;
262
264
} , [ ] ) ;
@@ -274,39 +276,29 @@ function LoginPage() {
274
276
dispatchState ( { isLoginButtonEnabled : false } ) ;
275
277
276
278
try {
277
- await loginService . login ( values ) ;
279
+ await authService . login ( values ) ;
278
280
dispatchState ( { isLoadingProject : true } ) ;
279
- await getOrganisations ( ) ;
281
+ await organisationService . getOrganisations ( { refresh : true } ) ;
280
282
dispatchState ( { isLoginButtonEnabled : true , isLoadingProject : false } ) ;
281
-
282
- navigate ( {
283
- to : '/' ,
284
- from : '/login' ,
285
- } ) ;
283
+ navigate ( { to : '/projects' , from : '/login' } ) ;
286
284
} catch ( err ) {
287
285
// TODO notify user using the UI
288
286
console . error ( login . name , err ) ;
287
+ dispatchState ( { isLoginButtonEnabled : true , isLoadingProject : false } ) ;
289
288
}
290
289
}
291
290
292
291
async function getSignUpConfig ( ) {
293
- dispatchState ( { isFetchingConfig : true } ) ;
292
+ dispatchState ( { isFetchingSignUpConfig : true } ) ;
293
+
294
294
try {
295
- const { data } = await signUpService . getSignUpConfig ( ) ;
295
+ const { data } = await authService . getSignUpConfig ( ) ;
296
296
dispatchState ( { isSignUpEnabled : data } ) ;
297
297
} catch ( err ) {
298
298
// TODO notify user using the UI
299
299
console . error ( getSignUpConfig . name , err ) ;
300
300
} finally {
301
- dispatchState ( { isFetchingConfig : false } ) ;
302
- }
303
- }
304
-
305
- async function getOrganisations ( ) {
306
- try {
307
- await organisationService . getOrganisations ( { refresh : true } ) ;
308
- } catch ( err ) {
309
- console . error ( getOrganisations . name , err ) ;
301
+ dispatchState ( { isFetchingSignUpConfig : false } ) ;
310
302
}
311
303
}
312
304
@@ -345,7 +337,7 @@ function LoginPage() {
345
337
346
338
< ConvoyLoader
347
339
isTransparent = { false }
348
- isVisible = { state . isLoadingProject || state . isFetchingConfig }
340
+ isVisible = { state . isLoadingProject || state . isFetchingSignUpConfig }
349
341
/>
350
342
</ >
351
343
) ;
@@ -355,5 +347,5 @@ export const Route = createFileRoute('/login')({
355
347
component : LoginPage ,
356
348
} ) ;
357
349
358
- // TODO loginService and other impure extraneous deps should be injected as a
350
+ // TODO authService and other impure extraneous deps should be injected as a
359
351
// dependency for testing and flexibility/maintainability
0 commit comments