Skip to content

Commit

Permalink
changes from class
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron.allen authored and aaron.allen committed Nov 15, 2023
1 parent c093b1f commit 9ffaea3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Data/IUserRoleService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;

namespace MythicalToyMachine.Data;

public interface IUserRoleService
{
//
public bool IsAuthenticated { get; }
public IEnumerable<string> Roles { get; }
public Task LookUpUser(string email, string name, string surname);
public void ResetUser();
}

public class UserRoleService : IUserRoleService
{
public UserRoleService(PostgresContext context)
{
this.context = context;
}
public bool IsAuthenticated { get; private set; }

public IEnumerable<string> Roles => roles;

private PostgresContext context;

private List<string> roles = new();


public void ResetUser()
{
throw new NotImplementedException();
}

public async Task LookUpUser( string email, string name, string surname)
{
if (email is not null)
{
string? eCompare = email;
var lCustomer = await context.Customers.FirstOrDefaultAsync(c => c.Useremail == email);




if (lCustomer is null)
{
//Make a new Customer
Customer newCustomer = new();
newCustomer.Surname = surname;
newCustomer.Firstname = name;
newCustomer.Useremail = email;

//add it to database
context.Customers.Add(newCustomer);
await context.SaveChangesAsync();
}
//Else, if cEmail is NOT null then the user already exists in the database, so we don't have to add them to the database a second time
IsAuthenticated = true;
}
}
}
11 changes: 11 additions & 0 deletions Pages/Identity/Login.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using MythicalToyMachine.Data;
using System.Security.Claims;

namespace MythicalToyMachine.Pages.Identity
{
[AllowAnonymous]
public class LoginModel : PageModel
{
private readonly IUserRoleService userRoleService;

public LoginModel(IUserRoleService userRoleService)
{
this.userRoleService = userRoleService;
}
public IActionResult OnGetAsync(string returnUrl = null)

Check warning on line 20 in Pages/Identity/Login.cshtml.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
string provider = "Google";
Expand All @@ -29,6 +36,10 @@ public async Task<IActionResult> OnGetCallbackAsync(
var GoogleUser = this.User.Identities.FirstOrDefault();
if (GoogleUser.IsAuthenticated)
{
await userRoleService.LookUpUser(GoogleUser.FindFirst(ClaimTypes.Email).Value,
GoogleUser.FindFirst(ClaimTypes.Name).Value,
GoogleUser.FindFirst(ClaimTypes.Surname).Value
);
var authProperties = new AuthenticationProperties
{
IsPersistent = true,
Expand Down

0 comments on commit 9ffaea3

Please sign in to comment.