Added hopframe backend

This commit is contained in:
2025-11-30 19:01:38 +01:00
parent 825bd80ef0
commit 5d1fc1f347
19 changed files with 523 additions and 17 deletions

View File

@@ -1,10 +1,11 @@
using Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore;
using SpotifyAPI.Web;
using SpotiParty.Web.Models;
namespace SpotiParty.Web.Services;
public sealed class AuthorizationHandler(NavigationManager navigator, DatabaseContext context) {
public sealed class AuthorizationHandler(NavigationManager navigator, DatabaseContext context, ClientSideStorage storage) {
private async Task<(string clientId, string clientSecret)> GetClientSecrets() {
var fileLines = await File.ReadAllLinesAsync(Path.Combine(Environment.CurrentDirectory, ".dev-token"));
@@ -45,13 +46,25 @@ public sealed class AuthorizationHandler(NavigationManager navigator, DatabaseCo
var client = new SpotifyClient(response.AccessToken);
var spotiUser = await client.UserProfile.Current();
var user = new User {
DisplayName = spotiUser.DisplayName,
RefreshToken = response.RefreshToken
};
await context.Users.AddAsync(user);
await context.SaveChangesAsync();
var user = await context.Users.FirstOrDefaultAsync(u => u.SpotifyUserId == spotiUser.Id);
if (user is null) {
user = new User {
DisplayName = spotiUser.DisplayName,
RefreshToken = response.RefreshToken,
SpotifyUserId = spotiUser.Id,
IsAdmin = await context.Users.CountAsync() == 0
};
await context.Users.AddAsync(user);
await context.SaveChangesAsync();
}
else {
user.RefreshToken = response.RefreshToken;
await context.SaveChangesAsync();
}
storage.SaveUserToken(response.RefreshToken);
}
}