diff --git a/GatewayAPI/Program.cs b/GatewayAPI/Program.cs index c8b364ee13bd222a210af9a94df64d63a9c1a863..0ed1620f865b10aeecf2071d9b334f1e7e479425 100644 --- a/GatewayAPI/Program.cs +++ b/GatewayAPI/Program.cs @@ -16,7 +16,8 @@ builder.Services.AddCors(options => { builder.WithOrigins("http://localhost:4200") .AllowAnyMethod() - .AllowAnyHeader(); + .AllowAnyHeader() + .AllowCredentials(); }); }); diff --git a/UserMicroservice/Controllers/UserController.cs b/UserMicroservice/Controllers/UserController.cs index b14e10802f0ae5c083d00f51021e68e12bfc09dd..68e4dd206e74a1c6b2c20932457160c24de445cc 100644 --- a/UserMicroservice/Controllers/UserController.cs +++ b/UserMicroservice/Controllers/UserController.cs @@ -59,8 +59,12 @@ namespace UserMicroservice.Controllers if (!int.TryParse(userIdString, out int userId)) return BadRequest("User ID is invalid."); + User? user = _userService.GetUser(userId); + if(user == null) + return Unauthorized(); + setAuthCookies(userId); - return Ok(); + return Ok(new { user.Id, user.Username, user.Email, user.Type }); } // POST: api/User/login diff --git a/client/src/providers/AuthProvider.tsx b/client/src/providers/AuthProvider.tsx index 668859cb8a9380e313c8df48a40126c3e56b8b77..9322a12e7de940ce2e70e6413db3a10922b809a3 100644 --- a/client/src/providers/AuthProvider.tsx +++ b/client/src/providers/AuthProvider.tsx @@ -1,6 +1,7 @@ import { ReactNode, useEffect, useState } from 'react'; import { AuthContext } from '../contexts/AuthContext'; import Spinner from '../components/Spinner/Spinner'; +import { authoriseUser } from '../services/Authorise/Authorise'; export interface IUser { id: number; @@ -15,7 +16,18 @@ function AuthProvider({ children }: { children: ReactNode }) { const [user, setUser] = useState<IUser>(); useEffect(() => { - setTimeout(() => setLoading(false), 500); // Fake api timer + async function authUser() { + try { + const result = await authoriseUser(); + giveAuth(); + updateUser(result.data); + setLoading(false); + } catch (error) { + setLoading(false); + } + } + + authUser(); }, []); const giveAuth = () => { diff --git a/client/src/services/Authorise/Authorise.ts b/client/src/services/Authorise/Authorise.ts new file mode 100644 index 0000000000000000000000000000000000000000..d01fa960af5c0dafdb6200bc410a623ed5d33e7e --- /dev/null +++ b/client/src/services/Authorise/Authorise.ts @@ -0,0 +1,7 @@ +import { AxiosResponse } from 'axios'; +import Api from '../../helpers/Api'; +import { IUser } from '../../providers/AuthProvider'; + +export async function authoriseUser(): Promise<AxiosResponse<IUser>> { + return Api.post('User/authorize', {}, { withCredentials: true }); +} diff --git a/client/src/services/Login/Login.ts b/client/src/services/Login/Login.ts index f29ae643f8a43d357f3a0c46238349b6f9e0bff2..573fa72c095d800ce6394d6e9b5d9c3e81e6c8b0 100644 --- a/client/src/services/Login/Login.ts +++ b/client/src/services/Login/Login.ts @@ -7,5 +7,5 @@ export async function loginUser(form: ILoginForm): Promise<AxiosResponse<IUser>> return Api.post('User/login', { Email: form.email, Password: form.password - }); + }, { withCredentials: true }); } diff --git a/client/src/services/Register/Register.ts b/client/src/services/Register/Register.ts index 1b7fd7fd27656b45485768eb4d923f2749fbcb26..5d4416a216ae60906be7def8ad4f5c5b89ea9509 100644 --- a/client/src/services/Register/Register.ts +++ b/client/src/services/Register/Register.ts @@ -10,5 +10,5 @@ export async function registerUser(form: IRegisterForm): Promise<AxiosResponse<I Username: form.name, Password: form.password, UserType: userStringToType(form.customerType) - }); + }, { withCredentials: true }); }