Adding users endpoint to return all users to authorized users

This commit is contained in:
hz
2025-05-13 15:10:28 -07:00
parent be255ef384
commit a923dfadc2
2 changed files with 13 additions and 0 deletions
+9
View File
@@ -108,6 +108,15 @@ public class AuthController : ControllerBase
return Ok($"{request.Username}'s role updated to {request.NewRole}");
}
[HttpGet("users")]
[Authorize(Roles = "SUPER")]
public async Task<IActionResult> GetAllUsers()
{
var users = await _userService.GetAllAsync();
return Ok(users);
}
private string GenerateJwtToken(User user)
{
+4
View File
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using GameAuthApi.Models;
using Microsoft.Extensions.Configuration;
@@ -19,6 +20,9 @@ public class UserService
public async Task<User> GetByUsernameAsync(string username) =>
await _users.Find(u => u.Username == username).FirstOrDefaultAsync();
public async Task<List<User>> GetAllAsync() =>
await _users.Find(_ => true).ToListAsync();
public async Task CreateAsync(User user) =>
await _users.InsertOneAsync(user);