mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
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:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user