diff --git a/VirtualTeacherGenAIDemo.Server/Controllers/UserController.cs b/VirtualTeacherGenAIDemo.Server/Controllers/UserController.cs index 55a085d..dd1ee55 100644 --- a/VirtualTeacherGenAIDemo.Server/Controllers/UserController.cs +++ b/VirtualTeacherGenAIDemo.Server/Controllers/UserController.cs @@ -19,6 +19,7 @@ public UserController(UserService userService) //getUser [HttpGet("{id}")] [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task> Get(string id) { var user = await _userService.GetUserAsync(id); diff --git a/virtualteachergenaidemo.client/src/App.tsx b/virtualteachergenaidemo.client/src/App.tsx index 28e6e5f..372aa1c 100644 --- a/virtualteachergenaidemo.client/src/App.tsx +++ b/virtualteachergenaidemo.client/src/App.tsx @@ -51,54 +51,61 @@ function AuthenticatedApp(props: any) { const { setRole } = useUserRole(); useEffect(() => { - const fetchUserRole = async () => { + const fetchUserRole = () => { if (email) { - try { - const response = await fetch(`/api/User/${email}`); - if (!response.ok) { - throw new Error('Failed to fetch user role'); - } - const userData = await response.json(); - setRole(userData.role); - } catch (error) { - console.error('Error fetching user role:', error); - } + fetch(`/api/User/${email}`) + .then(response => { + if (!response.ok) { + if (response.status === 404) { + return createUserIfNotExists(); + } else { + console.error('Failed to fetch user role:', response.statusText); + return Promise.reject('Failed to fetch user role'); + } + } + return response.json(); + }) + .then(userData => { + if (userData) { + setRole(userData.role); + } + }) + .catch(error => { + console.error('Error fetching user role:', error); + }); } }; - fetchUserRole(); - }, [email, setRole]); - - useEffect(() => { - const createUserIfNotExists = async () => { + const createUserIfNotExists = () => { if (email) { - try { - const response = await fetch('/api/User', { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - id: email, - name: email, - role: UserRoleEnum.User, - settings: {} - }) + fetch('/api/User', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + id: email, + name: email, + role: UserRoleEnum.User, + settings: {} + }) + }) + .then(response => { + if (!response.ok) { + console.error('Failed to create or get user:', response.statusText); + return Promise.reject('Failed to create or get user'); + } else { + setRole(UserRoleEnum.User); + } + }) + .catch(error => { + console.error('Error creating user:', error); }); - if (!response.ok) { - throw new Error('Failed to create or get user'); - } - else { - setRole(UserRoleEnum.User); - } - } catch (error) { - console.error('Error creating user:', error); - } } }; - createUserIfNotExists(); - }, []); + fetchUserRole(); + }, [email, setRole]); return (