Convert more authentication apis to controllers and session management

- Updated `AuthController.cs` to change unauthenticated response to `Unauthorized` and added a new `Logout` method for session handling.
- Removed the old logout endpoint in `Program.cs` and centralized logout logic in `AuthController.cs`.
- Added `GetSession` and `DeleteSession` methods in `DatabaseService.cs` to enhance session management.
- Updated API endpoints in `dash.js` to reflect new routing structure for authorized account info and logout.
This commit is contained in:
whoswhip
2025-07-06 18:59:39 -04:00
parent 73248e723a
commit 89e9673a00
4 changed files with 93 additions and 51 deletions
@@ -378,6 +378,52 @@ namespace SharpbinV2.Server.Services
}
return session;
}
public async Task<Session?> GetSession(string token)
{
if (string.IsNullOrEmpty(token))
return null;
using (var connection = new SqliteConnection(_connectionString))
{
await connection.OpenAsync();
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT * FROM sessions WHERE Token = @Token;";
command.Parameters.AddWithValue("@Token", token);
using (var reader = await command.ExecuteReaderAsync())
{
if (!reader.HasRows)
return null;
await reader.ReadAsync();
return new Session
{
UUID = reader.GetString(0),
UserUUID = reader.GetString(1),
Token = reader.GetString(2),
Created = reader.GetInt64(3),
Expirary = reader.GetInt64(4),
Ip = reader.GetString(5),
UserAgent = reader.GetString(6)
};
}
}
}
}
public async Task<bool> DeleteSession(string token)
{
if (string.IsNullOrEmpty(token))
return false;
using (var connection = new SqliteConnection(_connectionString))
{
await connection.OpenAsync();
using (var command = connection.CreateCommand())
{
command.CommandText = "DELETE FROM sessions WHERE Token = @Token;";
command.Parameters.AddWithValue("@Token", token);
await command.ExecuteNonQueryAsync();
}
return true;
}
}
public async Task<User?> CreateUser(string username, string email, string password_hash, string uuid)
{
if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password_hash))