Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions .rest
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,31 @@ GET http://localhost:5001/api/user/Test
Content-Type: application/json

###

### 2. Create a New User (POST)
POST http://localhost:5001/api/user/create
Content-Type: application/json

{
"FirstName": "J",
"LastName": "Doe",
"Email": "john.doe@example.com",
"Password": "password123",
"RecurringDays": 7,
"Role": 3,
"Points": {
"AllTimePoints": 1000,
"PointAmount": 0,
"Items": []
}
}

### Get User Details
GET http://localhost:5001/api/user/fromToken
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJIZWxsby5kb2VAZXhhbXBsZS5jb20iLCJqdGkiOiI0ODc5MTAzNi0zOTE3LTQ4NDktYjJiOS0wMDM2ZDZiNDJkNzQiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjkwZTNlMzdiLTQ3OTQtNDYzOS1hODg2LTFjZmFlY2VmYWU4MCIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6IkFkbWluIiwiZXhwIjoxNzM2NjA3NDM0LCJpc3MiOiJDYWxlbmRlcmZ5IiwiYXVkIjoiQ2FsZW5kZXJmeS1Vc2VycyJ9.L7555TMGpJugu44umJDUlUmO1U11mtCWDJeqHo3zEIU
###

### 3. Get All Users (GET)
GET http://localhost:5001/api/user/all
//Authorization: Bearer YOUR_ACCESS_TOKEN_HERE

###
PUT http://localhost:5001/api/user/me
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJIZWxsby5kb2VAZXhhbXBsZS5jb20iLCJqdGkiOiIyNDE0MDY5NS02MmM4LTQ3NmEtOWE4Ni0xMDk1ODJkNjdkODkiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjkwZTNlMzdiLTQ3OTQtNDYzOS1hODg2LTFjZmFlY2VmYWU4MCIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6IkFkbWluIiwiZXhwIjoxNzM2NjAxODIzLCJpc3MiOiJDYWxlbmRlcmZ5IiwiYXVkIjoiQ2FsZW5kZXJmeS1Vc2VycyJ9.8PX8uRslM8PnCI-Zbd1iHRC45g6JrUX4-jupl_GYQ7g
Content-Type: application/json

{
"firstname": "jaaaaaaassss",
"lastname": "Dossssssssssssssse",
"email": "johnieeee.doe@example.com",
"password": "newpassword"
}


### 4. Get User by ID (GET)
GET http://localhost:5001/api/user/{079b196b-ccb3-4654-8f07-53b251b8079d}
//Authorization: Bearer YOUR_ACCESS_TOKEN_HERE
GET http://localhost:5001/api/user/{49012e45-51cb-4dc4-a9bf-7dceddbe7fcf}
//Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJIZWxsby5kb2VAZXhhbXBsZS5jb20iLCJqdGkiOiJkOTcyYjYxOC1hYTdlLTRkNDctOWJhNS04OGFlYzRmM2ZjNGQiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjAwMzlhNGFmLTE3MmQtNDE1OC05MGE2LWJkYzNiZGUyNDJiZCIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6IkFkbWluIiwiZXhwIjoxNzM2NDUyMjc0LCJpc3MiOiJDYWxlbmRlcmZ5IiwiYXVkIjoiQ2FsZW5kZXJmeS1Vc2VycyJ9.bXoXoBoe9h10MUauJgwvw6E7RIKjsvk7b_kNSGXuj7Y

Content-Type: application/json

Expand All @@ -56,7 +51,7 @@ Content-Type: application/json
###

### 6. Delete User by ID (DELETE)
DELETE http://localhost:5001/api/user/{1113d1a7-3ccc-4fc0-b329-f600e314b0e3}
DELETE http://localhost:5001/api/user/{16daad2f-098c-4f07-b0da-3716403b7162}
//Authorization: Bearer YOUR_ACCESS_TOKEN_HERE

//CHANGE READOUT, WORKS!
Expand All @@ -69,19 +64,22 @@ POST http://localhost:5001/api/auth/login
Content-Type: application/json

{
"Email": "john.doe@example.com",
"Email": "Hello.doe@example.com",
"Password": "password123"
}

###
###



### 8. Login with Wrong Password
POST http://localhost:5001/api/auth/login
Content-Type: application/json

{
"Email": "john.doe@example.com",
"Password": "password122"
"Email": "Hello.doe@example.com",
"Password": "passwordxass123"
}

###
Expand All @@ -92,14 +90,14 @@ Authorization: Bearer YOUR_ACCESS_TOKEN_HERE

###
### 1. Get User Points
GET http://localhost:5001/api/points/{e4f85926-770f-4b45-80a2-99d323ebeb52}
GET http://localhost:5001/api/points/{9DF14F27-31DA-4FB8-A5FE-501E442A14D0}
Content-Type: application/json

###

### 2. Add Points to User
### 2. Add Points to User
POST http://localhost:5001/api/points/{e4f85926-770f-4b45-80a2-99d323ebeb52}/add
POST http://localhost:5001/api/points/{9DF14F27-31DA-4FB8-A5FE-501E442A14D0}/add
Content-Type: application/json

100
Expand Down
Binary file modified app.db
Binary file not shown.
Binary file modified app.db-shm
Binary file not shown.
Binary file modified app.db-wal
Binary file not shown.
Binary file modified bin/Debug/net8.0/Web-Development.dll
Binary file not shown.
Binary file modified bin/Debug/net8.0/Web-Development.pdb
Binary file not shown.
2 changes: 1 addition & 1 deletion controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<IActionResult> Login([FromBody] LoginModel model)

var token = GenerateJwtToken(user);

return Ok(new { token });
return Ok(new {token});
}

private async Task<User?> AuthenticateUser(string email, string password)
Expand Down
118 changes: 104 additions & 14 deletions controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Microsoft.IdentityModel.Tokens;

[Route("api/user")]
[ApiController]
public class UserController : ControllerBase
{
private readonly IUserService _userService;
private readonly IUserService _userService;

public UserController(IUserService userService)
{
Expand All @@ -24,37 +26,125 @@ public IActionResult Test()
[HttpPost("create")]
public async Task<IActionResult> CreateUser([FromBody] User user)
{
var createdUser = await _userService.CreateUserAsync(user);
return CreatedAtAction(nameof(GetUserById), new { id = Guid.NewGuid() }, createdUser);
try
{
var createdUser = await _userService.CreateUserAsync(user);
return CreatedAtAction(nameof(GetUserById), new { id = createdUser.Id }, createdUser);
}
catch (InvalidOperationException ex)
{
return BadRequest(new { message = ex.Message });
}
}

[HttpGet("all")]
//[Authorize(Policies.RequireUserRole)]
public async Task<ActionResult<IEnumerable<User>>> GetAllUsers()
{
return Ok(await _userService.GetAllUsersAsync());
}

[HttpGet("{id}")]
//[Authorize(Policies.RequireUserRole)]
public async Task<ActionResult<User>> GetUserById(Guid id)
{
var user = await _userService.GetUserAsync(id);
if (user == null) return NotFound();
return Ok(user);
}

[HttpPut("{id}")]
//[Authorize(Policies.RequireUserRole)]
public async Task<IActionResult> UpdateUser(Guid id, [FromBody] User user)
[HttpGet("fromToken")]
public async Task<ActionResult<User>> GetUserFromToken()
{
var updatedUser = await _userService.UpdateUserAsync(id, user);
if (updatedUser == null) return NotFound();
return Ok(updatedUser);
var authorizationHeader = Request.Headers["Authorization"].ToString();
if (string.IsNullOrWhiteSpace(authorizationHeader) || !authorizationHeader.StartsWith("Bearer "))
{
return Unauthorized(new { message = "Token is missing or invalid" });
}

var token = authorizationHeader.Substring("Bearer ".Length).Trim();

try
{
var userId = _userService.GetUserIdFromToken(token);
var user = await _userService.GetUserAsync(userId);

if (user == null)
return NotFound(new { message = "User not found" });

return Ok(new
{
user.Id,
user.Firstname,
user.Lastname,
user.Email,
user.Role,
Points = new
{
user.Points.PointAmount,
user.Points.AllTimePoints,
Items = user.Points.Items
}
});
}
catch (SecurityTokenException ex)
{
return Unauthorized(new { message = ex.Message });
}
}


[HttpPut("me")]
public async Task<IActionResult> UpdateUserDetails([FromBody] User updatedUser)
{
// Get the Authorization header and extract the token
var authorizationHeader = Request.Headers["Authorization"].ToString();
if (string.IsNullOrWhiteSpace(authorizationHeader) || !authorizationHeader.StartsWith("Bearer "))
{
return Unauthorized(new { message = "Token is missing or invalid" });
}

var token = authorizationHeader.Substring("Bearer ".Length).Trim();

try
{
var userId = _userService.GetUserIdFromToken(token);

var user = await _userService.GetUserAsync(userId);
if (user == null)
{
return NotFound(new { message = "User not found" });
}

user.Firstname = updatedUser.Firstname;
user.Lastname = updatedUser.Lastname;
user.Email = updatedUser.Email;
user.Password = updatedUser.Password;

await _userService.UpdateUserAsync(userId, user);

return Ok(new
{
message = "User updated successfully",
user = new
{
user.Id,
user.Firstname,
user.Lastname,
user.Email,
user.Points
}
});
}
catch (SecurityTokenException ex)
{
return Unauthorized(new { message = ex.Message });
}
catch (Exception ex)
{
return StatusCode(500, new { message = "An error occurred", details = ex.Message });
}
}

[HttpDelete("{id}")]
//[Authorize(Policies.RequireAdminRole)]
public async Task<IActionResult> DeleteUser(Guid id)
{
var result = await _userService.DeleteUserAsync(id);
Expand Down
21 changes: 14 additions & 7 deletions frontend/clientapp/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import PointShop from './components/shop/PointsShop';
import Login from './components/auth/login';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import PointShop from './components/shop/PointsShop';
import Login from './components/auth/login';
import Dashboard from './components/shop/Dashboard'; // Zorg dat dit pad correct is
import SignUp from './components/auth/Signup'; // Zorg dat dit pad correct is
import Settings from './components/auth/Settings'; // Zorg dat dit pad correct is


function App() {
return (
<Router> {/* Wrap the app with Router */}
<Router>
<div className="min-h-screen bg-gray-50">
<Routes>
<Route path="/" element={<Login />} />
<Route path="/point-shop" element={<PointShop />} />
<Route path="/" element={<Login />} />
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/point-shop" element={<PointShop />} />
<Route path="/signup" element={<SignUp />} />
<Route path="/settings" element={<Settings />} />
</Routes>
</div>
</Router>
);
}

export default App;
export default App;
Loading