From 886c207f1eeca37b4c21adb3e4a21e5c930d00f9 Mon Sep 17 00:00:00 2001 From: Fergal Moran Date: Mon, 23 Jan 2023 20:51:14 +0000 Subject: [PATCH] Pre scaffold --- Areas/Identity/Data/NukeItterUser.cs | 4 +- Controllers/UserController.cs | 30 +++++++++++- Pages/Profile.razor | 2 + Pages/Setup.razor | 72 ++++++++++++++++++---------- 4 files changed, 81 insertions(+), 27 deletions(-) diff --git a/Areas/Identity/Data/NukeItterUser.cs b/Areas/Identity/Data/NukeItterUser.cs index bb66858..6a56ecc 100644 --- a/Areas/Identity/Data/NukeItterUser.cs +++ b/Areas/Identity/Data/NukeItterUser.cs @@ -3,4 +3,6 @@ using Microsoft.AspNetCore.Identity; namespace Nukitter.Web.Areas.Identity.Data; // Add profile data for application users by adding properties to the NukeItterUser class -public class NukeItterUser : IdentityUser { } +public class NukeItterUser : IdentityUser { + public NukeItterUser(string userName) : base(userName) { } +} diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs index 56faff6..ce054ee 100644 --- a/Controllers/UserController.cs +++ b/Controllers/UserController.cs @@ -1,16 +1,42 @@ using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Twitter; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; +using Nukitter.Web.Areas.Identity.Data; namespace Nukitter.Web.Controllers; [ApiController] [Route("[controller]")] public class UserController : ControllerBase { + private readonly UserManager _userManager; + + public UserController(UserManager userManager) { + _userManager = userManager; + } + [HttpGet("twitter")] public async Task TwitterSignIn() { await HttpContext.ChallengeAsync(TwitterDefaults.AuthenticationScheme, new AuthenticationProperties { - RedirectUri = "https://nukeitter.dev.fergl.ie:5001/profile" + RedirectUri = "https://nukeitter.dev.fergl.ie:5001/user/twitter-redirect" }); } -} \ No newline at end of file + + [HttpGet("twitter-redirect")] + public async Task TwitterRedirect() { + var twitterHandle = HttpContext.User.Identity?.Name; + if (string.IsNullOrEmpty(twitterHandle)) { + return NotFound(); + } + + var user = await _userManager.FindByNameAsync(twitterHandle); + + if (user is null) { + await _userManager.CreateAsync(new NukeItterUser(twitterHandle)); + } + + await HttpContext.SignInAsync(HttpContext.User); + Console.WriteLine($"Let's see what we get?\n{HttpContext.User}"); + return Redirect("/setup"); + } +} diff --git a/Pages/Profile.razor b/Pages/Profile.razor index be36819..9e77728 100644 --- a/Pages/Profile.razor +++ b/Pages/Profile.razor @@ -6,6 +6,8 @@
+

This is your profile

+
@claim
@code { diff --git a/Pages/Setup.razor b/Pages/Setup.razor index 76502de..5ff3d49 100644 --- a/Pages/Setup.razor +++ b/Pages/Setup.razor @@ -1,42 +1,66 @@ @page "/setup" +@using System.Security.Claims @inject NavigationManager _navigationManager Twit Nuke
- -
+ class="container mx-auto flex flex-col md:flex-row items-center my-12 md:my-24"> + +
+ +
-

- Let's get started -

+

+ Let's get started +

- We need access to your Twitter account, promise we won't do anything icky!! + We need access to your Twitter account, promise we won't do anything icky!!

-
- -
- Radioactive -
+ + +

+ Hey $" + @{@User.Identity.Name} + " + - let's get you setup +

+
+ +
+ +
+ Radioactive +
@code { - private void TwitterSignin() { - _navigationManager.NavigateTo("user/twitter", true); - } + [CascadingParameter] + private Task auth { get; set; } + + public ClaimsPrincipal User { get; set; } + + protected override async Task OnInitializedAsync() { + var authState = await auth; + User = authState.User; + } + + + private void TwitterSignin() { + _navigationManager.NavigateTo("user/twitter", true); + } } \ No newline at end of file