11import { getCookie } from '../utils' ;
2+ import { UserData } from '../../types/models' ;
3+ import { API_BASE } from '../apiBase' ;
4+ import { AxiosError } from 'axios' ;
25
3- const baseUrl = import . meta. env . VITE_API_URI
4- ? `${ import . meta. env . VITE_API_URI } `
5- : `${ location . origin } ` ;
6+ interface AxiosConfig {
7+ withCredentials : boolean ;
8+ headers : {
9+ 'X-CSRF-TOKEN' : string ;
10+ Authorization ?: string ;
11+ } ;
12+ }
613
714/**
815 * Gets the current user's information
9- * @return {Promise<Object> } The user's information
1016 */
11- export const getUserInfo = async ( ) => {
17+ export const getUserInfo = async ( ) : Promise < UserData | null > => {
1218 try {
13- const response = await fetch ( `${ baseUrl } /api/auth/me` , {
19+ const response = await fetch ( `${ API_BASE } /api/auth/me` , {
1420 credentials : 'include' , // Sends cookies
1521 } ) ;
16-
1722 if ( ! response . ok ) throw new Error ( `Failed to fetch user info: ${ response . statusText } ` ) ;
18-
1923 return await response . json ( ) ;
2024 } catch ( error ) {
2125 console . error ( 'Error fetching user info:' , error ) ;
@@ -25,9 +29,8 @@ export const getUserInfo = async () => {
2529
2630/**
2731 * Gets the Axios config for the UI
28- * @return {Object } The Axios config
2932 */
30- export const getAxiosConfig = ( ) => {
33+ export const getAxiosConfig = ( ) : AxiosConfig => {
3134 const jwtToken = localStorage . getItem ( 'ui_jwt_token' ) ;
3235 return {
3336 withCredentials : true ,
@@ -40,11 +43,9 @@ export const getAxiosConfig = () => {
4043
4144/**
4245 * Processes authentication errors and returns a user-friendly error message
43- * @param {Object } error - The error object
44- * @return {string } The error message
4546 */
46- export const processAuthError = ( error , jwtAuthEnabled = false ) => {
47- let errorMessage = `Failed to authorize user: ${ error . response . data . trim ( ) } . ` ;
47+ export const processAuthError = ( error : AxiosError < any > , jwtAuthEnabled = false ) : string => {
48+ let errorMessage = `Failed to authorize user: ${ error . response ? .data ? .trim ( ) ?? '' } . ` ;
4849 if ( jwtAuthEnabled && ! localStorage . getItem ( 'ui_jwt_token' ) ) {
4950 errorMessage +=
5051 'Set your JWT token in the settings page or disable JWT auth in your app configuration.' ;
0 commit comments