Skip to content
Snippets Groups Projects
Commit 92e0eef3 authored by Cross, Liam (UG - Comp Sci & Elec Eng)'s avatar Cross, Liam (UG - Comp Sci & Elec Eng)
Browse files

Authorize now calls backend

parent 797a5a23
No related branches found
No related tags found
No related merge requests found
......@@ -16,7 +16,8 @@ builder.Services.AddCors(options =>
{
builder.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader();
.AllowAnyHeader()
.AllowCredentials();
});
});
......
......@@ -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
......
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 = () => {
......
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 });
}
......@@ -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 });
}
......@@ -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 });
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment