mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-20 00:02:28 +00:00
Re-do formatting
This commit is contained in:
parent
95a1fd35a5
commit
173addfd03
74 changed files with 1286 additions and 905 deletions
|
@ -8,8 +8,10 @@ using LBPUnion.ProjectLighthouse.Types.Settings;
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse {
|
||||
public class Database : DbContext {
|
||||
namespace LBPUnion.ProjectLighthouse
|
||||
{
|
||||
public class Database : DbContext
|
||||
{
|
||||
public DbSet<User> Users { get; set; }
|
||||
public DbSet<Location> Locations { get; set; }
|
||||
public DbSet<Slot> Slots { get; set; }
|
||||
|
@ -21,21 +23,20 @@ namespace LBPUnion.ProjectLighthouse {
|
|||
|
||||
public DbSet<LastMatch> LastMatches { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseMySql(
|
||||
ServerSettings.DbConnectionString,
|
||||
MySqlServerVersion.LatestSupportedServerVersion
|
||||
);
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
||||
=> options.UseMySql(ServerSettings.DbConnectionString, MySqlServerVersion.LatestSupportedServerVersion);
|
||||
|
||||
public async Task<User> CreateUser(string username) {
|
||||
public async Task<User> CreateUser(string username)
|
||||
{
|
||||
User user;
|
||||
if((user = await this.Users.Where(u => u.Username == username).FirstOrDefaultAsync()) != null)
|
||||
return user;
|
||||
if ((user = await this.Users.Where(u => u.Username == username).FirstOrDefaultAsync()) != null) return user;
|
||||
|
||||
Location l = new(); // store to get id after submitting
|
||||
this.Locations.Add(l); // add to table
|
||||
await this.SaveChangesAsync(); // saving to the database returns the id and sets it on this entity
|
||||
|
||||
user = new User {
|
||||
user = new User
|
||||
{
|
||||
Username = username,
|
||||
LocationId = l.Id,
|
||||
Biography = username + " hasn't introduced themselves yet.",
|
||||
|
@ -46,14 +47,15 @@ namespace LBPUnion.ProjectLighthouse {
|
|||
|
||||
return user;
|
||||
}
|
||||
|
||||
#nullable enable
|
||||
public async Task<Token?> AuthenticateUser(LoginData loginData) {
|
||||
// TODO: don't use psn name to authenticate
|
||||
User user = await this.Users.FirstOrDefaultAsync(u => u.Username == loginData.Username)
|
||||
?? await this.CreateUser(loginData.Username);
|
||||
|
||||
Token token = new() {
|
||||
#nullable enable
|
||||
public async Task<Token?> AuthenticateUser(LoginData loginData)
|
||||
{
|
||||
// TODO: don't use psn name to authenticate
|
||||
User user = await this.Users.FirstOrDefaultAsync(u => u.Username == loginData.Username) ?? await this.CreateUser(loginData.Username);
|
||||
|
||||
Token token = new()
|
||||
{
|
||||
UserToken = HashHelper.GenerateAuthToken(),
|
||||
UserId = user.UserId,
|
||||
};
|
||||
|
@ -64,19 +66,18 @@ namespace LBPUnion.ProjectLighthouse {
|
|||
return token;
|
||||
}
|
||||
|
||||
public async Task<User?> UserFromAuthToken(string authToken) {
|
||||
public async Task<User?> UserFromAuthToken(string authToken)
|
||||
{
|
||||
Token? token = await this.Tokens.FirstOrDefaultAsync(t => t.UserToken == authToken);
|
||||
if(token == null) return null;
|
||||
return await this.Users
|
||||
.Include(u => u.Location)
|
||||
.FirstOrDefaultAsync(u => u.UserId == token.UserId);
|
||||
if (token == null) return null;
|
||||
|
||||
return await this.Users.Include(u => u.Location).FirstOrDefaultAsync(u => u.UserId == token.UserId);
|
||||
}
|
||||
|
||||
public async Task<User?> UserFromRequest(HttpRequest request) {
|
||||
if(!request.Cookies.TryGetValue("MM_AUTH", out string? mmAuth) || mmAuth == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<User?> UserFromRequest(HttpRequest request)
|
||||
{
|
||||
if (!request.Cookies.TryGetValue("MM_AUTH", out string? mmAuth) || mmAuth == null) return null;
|
||||
|
||||
return await this.UserFromAuthToken(mmAuth);
|
||||
}
|
||||
#nullable disable
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue