Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logout code not included. #450

Closed
JanOlsmar opened this issue Jan 15, 2024 · 3 comments · Fixed by #465
Closed

Logout code not included. #450

JanOlsmar opened this issue Jan 15, 2024 · 3 comments · Fixed by #465

Comments

@JanOlsmar
Copy link

JanOlsmar commented Jan 15, 2024

The logout code from Microsoft admin pages are not included. Template 0.6.5

(Had hoped for a Mud admin )

@Unskilledcrab
Copy link

+1

@Unskilledcrab
Copy link

Solved by add the following to Components -> Account -> Pages

@page "/Account/Logout"

@using Microsoft.AspNetCore.Identity

@inject SignInManager<ApplicationUser> SignInManager
@inject NavigationManager NavigationManager

@code {
    protected override async Task OnInitializedAsync()
    {
        await SignInManager.SignOutAsync();
        NavigationManager.NavigateTo("/");
    }
}

And then you can logout a user from the app with the following:

<MudNavLink Href="Account/Logout" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.ExitToApp">Logout</MudNavLink>

It's very basic and there's room for improvement but for someone just trying to get it working quickly, it functions well enough

Unskilledcrab pushed a commit to Unskilledcrab/chain-generator that referenced this issue Feb 10, 2024
@kepham
Copy link

kepham commented May 2, 2024

`@implements IDisposable
@Inject NavigationManager NavigationManager

Home Counter Weather Auth Required
<AuthorizeView>
    <Authorized>
        <MudNavLink Href="Account/Manage" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">@context.User.Identity?.Name</MudNavLink>
        <form action="Account/Logout" method="post">
            <AntiforgeryToken />
            <input type="hidden" name="ReturnUrl" value="@currentUrl" />
            <button class="mud-nav-link" type="submit">
                <MudIcon Icon="@Icons.Material.Filled.Logout" Title="Add" />
                <div class="mud-nav-link-text">Logout</div>
            </button>
        </form>
    </Authorized>
    <NotAuthorized>
        <MudNavLink Href="Account/Register" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">Register</MudNavLink>
        <MudNavLink Href="Account/Login" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">Login</MudNavLink>
    </NotAuthorized>
</AuthorizeView>

@code {
private string? currentUrl;

protected override void OnInitialized()
{
    currentUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
    NavigationManager.LocationChanged += OnLocationChanged;
}

private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
{
    currentUrl = NavigationManager.ToBaseRelativePath(e.Location);
    StateHasChanged();
}

public void Dispose()
{
    NavigationManager.LocationChanged -= OnLocationChanged;
}

}

This uses the same convention as the official Blazor Template`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants