Login route, database compose file
This commit is contained in:
@ -45,16 +45,16 @@ AuthenticationSettings? authSettings = authSettingsSection.Get<AuthenticationSet
|
||||
// ValidateAudience = false
|
||||
// };
|
||||
// });
|
||||
|
||||
builder.Services.AddAuthorization(options =>
|
||||
{
|
||||
options.AddPolicy(nameof(UserRole.Developer), policy => policy.RequireRole(nameof(UserRole.Developer)));
|
||||
options.AddPolicy(nameof(UserRole.User), policy =>
|
||||
{
|
||||
// Also allow Developers to do anything a user can do.
|
||||
policy.RequireRole(nameof(UserRole.User), nameof(UserRole.Developer));
|
||||
});
|
||||
});
|
||||
//
|
||||
// builder.Services.AddAuthorization(options =>
|
||||
// {
|
||||
// options.AddPolicy(nameof(UserRole.Developer), policy => policy.RequireRole(nameof(UserRole.Developer)));
|
||||
// options.AddPolicy(nameof(UserRole.User), policy =>
|
||||
// {
|
||||
// // Also allow Developers to do anything a user can do.
|
||||
// policy.RequireRole(nameof(UserRole.User), nameof(UserRole.Developer));
|
||||
// });
|
||||
// });
|
||||
|
||||
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
|
||||
@ -145,6 +145,27 @@ app.MapGet("/user", (ClaimsPrincipal user) =>
|
||||
Results.Ok(new { message = $"Authenticated as { user?.Identity?.Name }" });
|
||||
}).RequireAuthorization(nameof(UserRole.User));
|
||||
|
||||
app.MapGet("/auth/validate", async (HttpContext context, UserManager<IdentityUser> userManager) =>
|
||||
{
|
||||
if (!context.User.Identity?.IsAuthenticated ?? true)
|
||||
{
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
IdentityUser? user = await userManager.GetUserAsync(context.User);
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
return Results.InternalServerError("User not found?!");
|
||||
}
|
||||
|
||||
return Results.Ok(new
|
||||
{
|
||||
user.Id,
|
||||
user.Email
|
||||
});
|
||||
}).RequireAuthorization();
|
||||
|
||||
app.MapGet("/ephemeral_token", async () =>
|
||||
{
|
||||
//if (apiKey == null)
|
||||
|
||||
13
docker-compose.yml
Normal file
13
docker-compose.yml
Normal file
@ -0,0 +1,13 @@
|
||||
# Use postgres/example user/password credentials
|
||||
|
||||
services:
|
||||
db:
|
||||
image: postgres
|
||||
restart: always
|
||||
# set shared memory limit when using docker compose
|
||||
shm_size: 128mb
|
||||
environment:
|
||||
POSTGRES_USER: us-entry-agent-dev
|
||||
POSTGRES_PASSWORD: example
|
||||
ports:
|
||||
- 5432:5432
|
||||
@ -5,6 +5,7 @@ import useLoginToken from "./Hooks/useLoginToken.tsx";
|
||||
import Home from "./Components/Home.tsx";
|
||||
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
|
||||
import ProtectedRoute from "./Components/ProtectedRoute.tsx";
|
||||
import {CookiesProvider} from "react-cookie";
|
||||
|
||||
export default function App()
|
||||
{
|
||||
@ -22,6 +23,7 @@ export default function App()
|
||||
<>
|
||||
{/*<Navigation/>*/}
|
||||
|
||||
<CookiesProvider defaultSetOptions={{ path: '/' }}>
|
||||
<BrowserRouter>
|
||||
<Routes>
|
||||
<Route index path="/" element={ <Home /> } />
|
||||
@ -32,6 +34,7 @@ export default function App()
|
||||
<Route path="*" element={ <p>Deine Mamma ist so dick, sie hat diese Seite gefressen. </p> } />
|
||||
</Routes>
|
||||
</BrowserRouter>
|
||||
</CookiesProvider>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@ -1,8 +1,54 @@
|
||||
import { Outlet } from "react-router-dom";
|
||||
import Login from "./Login.tsx";
|
||||
import { useEffect } from "react";
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
const AuthValidateResult = z.object({
|
||||
id: z.string(),
|
||||
email: z.string()
|
||||
});
|
||||
|
||||
export default function ProtectedRoute({user, setUser} : {user: string | null, setUser: (user: string | null) => void})
|
||||
{
|
||||
|
||||
async function authenticateUser ()
|
||||
{
|
||||
try
|
||||
{
|
||||
console.log("Checkineasdjkf")
|
||||
const response = await fetch('/auth/validate', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok)
|
||||
{
|
||||
console.error(`Failed to validate session: ${response.status} ${response.statusText}`);
|
||||
setUser(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const responseJson:unknown = await response.json();
|
||||
const parsedUser = AuthValidateResult.parse(responseJson);
|
||||
|
||||
setUser(JSON.stringify(parsedUser));
|
||||
|
||||
console.log("User authorized!")
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
console.error(e);
|
||||
setUser(null);
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() =>
|
||||
{
|
||||
void authenticateUser();
|
||||
});
|
||||
|
||||
if (user === null)
|
||||
{
|
||||
return <Login setToken={setUser}/>;
|
||||
|
||||
@ -44,7 +44,7 @@ const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_H
|
||||
env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'https://localhost:7085';
|
||||
|
||||
// Define a list of all existing backend routes.
|
||||
const backendRoutes = ['/login', '/register', '/ephemeral_token'];
|
||||
const backendRoutes = ['/login', '/register', '/ephemeral_token', '/auth/validate'];
|
||||
|
||||
// For development, we have a node.js server running that delivers our frontend.
|
||||
// We have to configure proxies for the backend calls, so that node.js will forward them to the backend.
|
||||
|
||||
Reference in New Issue
Block a user