@@ -36,20 +36,21 @@ const Login: React.FC = () => {
3636 const [ usernamePasswordMethod , setUsernamePasswordMethod ] = useState < string > ( '' ) ;
3737
3838 useEffect ( ( ) => {
39- getBaseUrl ( ) . then ( ( baseUrl ) => {
40- axios . get ( `${ baseUrl } /api/auth/config` ) . then ( ( response ) => {
41- const usernamePasswordMethod = response . data . usernamePasswordMethod ;
42- const otherMethods = response . data . otherMethods ;
39+ const fetchAuthConfig = async ( ) => {
40+ const baseUrl = await getBaseUrl ( ) ;
41+ const response = await axios . get ( `${ baseUrl } /api/auth/config` ) ;
42+ const usernamePasswordMethod = response . data . usernamePasswordMethod ;
43+ const otherMethods = response . data . otherMethods ;
4344
44- setUsernamePasswordMethod ( usernamePasswordMethod ) ;
45- setAuthMethods ( otherMethods ) ;
45+ setUsernamePasswordMethod ( usernamePasswordMethod ) ;
46+ setAuthMethods ( otherMethods ) ;
4647
47- // Automatically login if only one non-username/password method is enabled
48- if ( ! usernamePasswordMethod && otherMethods . length === 1 ) {
49- handleAuthMethodLogin ( otherMethods [ 0 ] ) ;
50- }
51- } ) ;
52- } ) ;
48+ // Automatically login if only one non-username/password method is enabled
49+ if ( ! usernamePasswordMethod && otherMethods . length === 1 ) {
50+ await handleAuthMethodLogin ( otherMethods [ 0 ] ) ;
51+ }
52+ } ;
53+ fetchAuthConfig ( ) ;
5354 } , [ ] ) ;
5455
5556 function validateForm ( ) : boolean {
@@ -58,40 +59,40 @@ const Login: React.FC = () => {
5859 ) ;
5960 }
6061
61- function handleAuthMethodLogin ( authMethod : string ) : void {
62- getBaseUrl ( ) . then ( ( baseUrl ) => {
63- window . location . href = `${ baseUrl } /api/auth/${ authMethod } ` ;
64- } ) ;
62+ async function handleAuthMethodLogin ( authMethod : string ) : Promise < void > {
63+ const baseUrl = await getBaseUrl ( ) ;
64+ window . location . href = `${ baseUrl } /api/auth/${ authMethod } ` ;
6565 }
6666
67- function handleSubmit ( event : FormEvent ) : void {
67+ async function handleSubmit ( event : FormEvent ) : Promise < void > {
6868 event . preventDefault ( ) ;
6969 setIsLoading ( true ) ;
7070
71- getBaseUrl ( ) . then ( ( baseUrl ) => {
71+ try {
72+ const baseUrl = await getBaseUrl ( ) ;
7273 const loginUrl = `${ baseUrl } /api/auth/login` ;
73- axios
74- . post < LoginResponse > ( loginUrl , { username, password } , getAxiosConfig ( ) )
75- . then ( ( ) => {
74+ await axios . post < LoginResponse > ( loginUrl , { username, password } , getAxiosConfig ( ) ) ;
75+ window . sessionStorage . setItem ( 'git.proxy.login' , 'success' ) ;
76+ setMessage ( 'Success!' ) ;
77+ setSuccess ( true ) ;
78+ await authContext . refreshUser ( ) ;
79+ navigate ( 0 ) ;
80+ } catch ( error : unknown ) {
81+ if ( error instanceof AxiosError ) {
82+ if ( error . response ?. status === 307 ) {
7683 window . sessionStorage . setItem ( 'git.proxy.login' , 'success' ) ;
77- setMessage ( 'Success!' ) ;
78- setSuccess ( true ) ;
79- authContext . refreshUser ( ) . then ( ( ) => navigate ( 0 ) ) ;
80- } )
81- . catch ( ( error : AxiosError ) => {
82- if ( error . response ?. status === 307 ) {
83- window . sessionStorage . setItem ( 'git.proxy.login' , 'success' ) ;
84- setGitAccountError ( true ) ;
85- } else if ( error . response ?. status === 403 ) {
86- setMessage ( processAuthError ( error , false ) ) ;
87- } else {
88- setMessage ( 'You entered an invalid username or password...' ) ;
89- }
90- } )
91- . finally ( ( ) => {
92- setIsLoading ( false ) ;
93- } ) ;
94- } ) ;
84+ setGitAccountError ( true ) ;
85+ } else if ( error . response ?. status === 403 ) {
86+ setMessage ( processAuthError ( error , false ) ) ;
87+ } else {
88+ setMessage ( 'You entered an invalid username or password...' ) ;
89+ }
90+ } else {
91+ setMessage ( 'You entered an invalid username or password...' ) ;
92+ }
93+ } finally {
94+ setIsLoading ( false ) ;
95+ }
9596 }
9697
9798 if ( gitAccountError ) {
0 commit comments