2021-10-26 22:52:39 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
|
|
using Microsoft.AspNetCore.WebUtilities;
|
|
|
|
|
|
|
|
using Selector.Model;
|
2021-10-27 17:31:29 +01:00
|
|
|
using SpotifyAPI.Web;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-10-26 22:52:39 +01:00
|
|
|
|
|
|
|
namespace Selector.Web.Areas.Identity.Pages.Account.Manage
|
|
|
|
{
|
|
|
|
public partial class SpotifyModel : PageModel
|
|
|
|
{
|
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
2021-10-27 17:31:29 +01:00
|
|
|
private readonly ILogger<SpotifyModel> Logger;
|
|
|
|
private readonly RootOptions Config;
|
2021-10-26 22:52:39 +01:00
|
|
|
|
|
|
|
public SpotifyModel(
|
2021-10-27 17:31:29 +01:00
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
ILogger<SpotifyModel> logger,
|
|
|
|
IOptions<RootOptions> config
|
|
|
|
)
|
2021-10-26 22:52:39 +01:00
|
|
|
{
|
|
|
|
_userManager = userManager;
|
2021-10-27 17:31:29 +01:00
|
|
|
Logger = logger;
|
|
|
|
Config = config.Value;
|
2021-10-26 22:52:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[BindProperty]
|
|
|
|
public bool SpotifyIsLinked { get; set; }
|
|
|
|
|
|
|
|
[TempData]
|
|
|
|
public string StatusMessage { get; set; }
|
|
|
|
|
|
|
|
public async Task<IActionResult> OnGetAsync()
|
|
|
|
{
|
|
|
|
var user = await _userManager.GetUserAsync(User);
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
SpotifyIsLinked = user.SpotifyIsLinked;
|
|
|
|
|
|
|
|
return Page();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IActionResult> OnPostLinkAsync()
|
|
|
|
{
|
2021-10-27 17:31:29 +01:00
|
|
|
var user = await _userManager.GetUserAsync(User);
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Config.ClientId is null)
|
|
|
|
{
|
|
|
|
Logger.LogError($"Cannot link user, no Spotify client ID");
|
|
|
|
StatusMessage = "Could not link Spotify, no app credentials";
|
|
|
|
return RedirectToPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Config.ClientSecret is null)
|
|
|
|
{
|
|
|
|
Logger.LogError($"Cannot link user, no Spotify client secret");
|
|
|
|
StatusMessage = "Could not link Spotify, no app credentials";
|
|
|
|
return RedirectToPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
var loginRequest = new LoginRequest(
|
|
|
|
new Uri(Config.SpotifyCallback),
|
|
|
|
Config.ClientId,
|
|
|
|
LoginRequest.ResponseType.Code
|
|
|
|
) {
|
|
|
|
Scope = new[] {
|
|
|
|
Scopes.UserReadPlaybackState
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return Redirect(loginRequest.ToUri().ToString());
|
2021-10-26 22:52:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IActionResult> OnPostUnlinkAsync()
|
|
|
|
{
|
2021-10-27 17:31:29 +01:00
|
|
|
var user = await _userManager.GetUserAsync(User);
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: stop users Spotify-linked resources (watchers)
|
|
|
|
|
|
|
|
user.SpotifyIsLinked = false;
|
|
|
|
|
|
|
|
user.SpotifyAccessToken = null;
|
|
|
|
user.SpotifyRefreshToken = null;
|
|
|
|
user.SpotifyTokenExpiry = 0;
|
|
|
|
user.SpotifyLastRefresh = default;
|
|
|
|
|
|
|
|
await _userManager.UpdateAsync(user);
|
|
|
|
|
2021-10-26 22:52:39 +01:00
|
|
|
StatusMessage = "Spotify Unlinked";
|
|
|
|
return RedirectToPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|