repasscloud / AirAtlasPro

AirAtlasPro: Your Airbnb Host's Ultimate Guide & Management Solution for Exceptional Guest Experiences!
0 stars 0 forks source link

User Registration and Authentication #3

Closed danijeljw-RPC closed 11 months ago

danijeljw-RPC commented 11 months ago

User sign-up and login functionality with secure authentication using Auth0

danijeljw-RPC commented 11 months ago

This function, will only ever return an email address to identify the user. This should be used to generate the in-house server-side data alone. Nothing else.

For this, assign the value in the /account/settings page, and the save button will submit the data, and either create or update the record. Pass this along to Stripe when the user signs-up for a paid service plan too.

danijeljw-RPC commented 11 months ago

File _Host.cshtml was original at https://github.com/repasscloud/AirAtlasPro/commit/e2acd8d90543eb484019bb69c41dfe4c012baafa

danijeljw-RPC commented 11 months ago

Boilerplate code for every default page in project:

@code {
    private string pageTitle = "AirAtlasPro";

    private bool showLogin = true;

    private string Username = "Anonymous User";
    private string Picture = string.Empty;
    private bool? hasBillingRole;
    private bool? hasSupportRole;

    [CascadingParameter]
    private Task<AuthenticationState>? authenticationState { get; set; }

    protected override async Task OnInitializedAsync()
    {
        if (authenticationState is not null)
        {
            var state = await authenticationState;

            Username = state?.User?.Identity?.Name ?? string.Empty;

            Picture = state?.User.Claims
                        .Where(c => c.Type.Equals("picture"))
                        .Select(c => c.Value)
                        .FirstOrDefault() ?? string.Empty;

            hasBillingRole = state?.User.IsInRole("Billing");
            hasSupportRole = state?.User.IsInRole("Support");

            if (!string.IsNullOrEmpty(state?.User?.Identity?.Name))
            {
                showLogin = false;
            }
        }
        await base.OnInitializedAsync();
    }

    // Redirect to login page if not already logged in
    private void NavigateToLogin()
    {
        NavigationManager.NavigateTo("/login?redirectUri=/");
    }
}
danijeljw-RPC commented 11 months ago

Boilerplate code for each page should also include:

@page "/<path>"
@attribute [Authorize]
@inject IJSRuntime JSRuntime
@inject ISupportTicketService SupportTicketService
@inject IToastService ToastService
@inject NavigationManager NavigationManager

<PageTitle>@pageTitle</PageTitle>

<h2>@pageTitle</h2>

@if (showLogin)
{
    NavigateToLogin();
}
danijeljw-RPC commented 11 months ago

Full boilerplate code:

@page "/account/settings"
@attribute [Authorize]
@inject IJSRuntime JSRuntime
@inject ISupportTicketService SupportTicketService
@inject IToastService ToastService
@inject NavigationManager NavigationManager

<PageTitle>@pageTitle</PageTitle>

<h2>@pageTitle</h2>

@if (showLogin)
{
    NavigateToLogin();
}

@*content goes here*@

@code {
    private string pageTitle = "";

    private User user = new User();

    private bool showLogin = true;

    private string Username = "Anonymous User";
    private string Picture = string.Empty;
    private bool? hasBillingRole;
    private bool? hasSupportRole;

    [CascadingParameter]
    private Task<AuthenticationState>? authenticationState { get; set; }

    protected override async Task OnInitializedAsync()
    {
        if (authenticationState is not null)
        {
            var state = await authenticationState;

            Username = state?.User?.Identity?.Name ?? string.Empty;

            Picture = state?.User.Claims
                        .Where(c => c.Type.Equals("picture"))
                        .Select(c => c.Value)
                        .FirstOrDefault() ?? string.Empty;

            hasBillingRole = state?.User.IsInRole("Billing");
            hasSupportRole = state?.User.IsInRole("Support");

            if (!string.IsNullOrEmpty(state?.User?.Identity?.Name))
            {
                showLogin = false;
            }
        }
        await base.OnInitializedAsync();
    }

    // Redirect to login page if not already logged in
    private void NavigateToLogin()
    {
        NavigationManager.NavigateTo("/login?redirectUri=/");
    }

    @*additional functions go here*@
}