File size: 5,264 Bytes
0acba57
 
 
 
4433399
0acba57
 
 
 
 
 
 
 
6aad87c
0acba57
6aad87c
0acba57
 
6aad87c
 
0acba57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6aad87c
 
0acba57
 
 
 
6aad87c
0acba57
6aad87c
0acba57
 
 
 
 
6aad87c
0acba57
 
 
 
 
 
 
 
 
 
 
 
6aad87c
 
0acba57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4433399
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
@page "/login"

@using BlazorFrontend.Services
@using BlazorFrontend.Providers
@using LibraryManagement.Shared.Models
@using Microsoft.AspNetCore.Components.Authorization
@inject HttpClient Http
@inject AuthenticationStateProvider AuthStateProvider
@inject NavigationManager Navigation

<PageTitle>Sign In</PageTitle>

<div class="min-h-[calc(100vh-200px)] flex items-center justify-center py-12 px-4 sm:px-6 lg:px-8">
    <div class="max-w-md w-full space-y-8 bg-white p-10 rounded-2xl border border-mystic-200 shadow-xl">
        <div>
            <div class="mx-auto h-12 w-12 bg-mystic-900 rounded-xl flex items-center justify-center text-white mb-4">
                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"/><polyline points="10 17 15 12 10 7"/><line x1="15" y1="12" x2="3" y2="12"/></svg>
            </div>
            <h2 class="text-center text-3xl font-extrabold text-mystic-900 tracking-tight">Welcome back</h2>
            <p class="mt-2 text-center text-sm text-mystic-500">
                Continue where you left off.
            </p>
        </div>

        @if (!string.IsNullOrEmpty(_errorMessage))
        {
            <div class="bg-red-50 text-red-600 p-3 rounded-lg border border-red-200 text-sm mb-4">
                @_errorMessage
            </div>
        }

        <EditForm Model="@_loginRequest" OnValidSubmit="HandleLogin" FormName="loginForm" class="mt-8 space-y-6">
            <DataAnnotationsValidator />
            
            <div class="space-y-4">
                <div>
                    <label class="block text-sm font-medium text-mystic-700 mb-1">Email Address</label>
                    <InputText @bind-Value="_loginRequest.Email" type="email" required class="appearance-none relative block w-full px-3 py-2 border border-mystic-300 placeholder-mystic-400 text-mystic-900 rounded-md focus:outline-none focus:ring-mystic-900 focus:border-mystic-900 focus:z-10 sm:text-sm" placeholder="name@example.com" />
                    <ValidationMessage For="@(() => _loginRequest.Email)" class="text-xs text-red-500 mt-1" />
                </div>
                <div>
                    <div class="flex items-center justify-between mb-1">
                        <label class="block text-sm font-medium text-mystic-700">Password</label>
                    </div>
                    <InputText type="password" @bind-Value="_loginRequest.Password" required class="appearance-none relative block w-full px-3 py-2 border border-mystic-300 placeholder-mystic-400 text-mystic-900 rounded-md focus:outline-none focus:ring-mystic-900 focus:border-mystic-900 focus:z-10 sm:text-sm" placeholder="••••••••" />
                    <ValidationMessage For="@(() => _loginRequest.Password)" class="text-xs text-red-500 mt-1" />
                </div>
            </div>

            <div>
                <button type="submit" disabled="@_isLoading" class="group relative w-full flex justify-center py-2.5 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-mystic-900 hover:bg-mystic-800 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-mystic-900 transition-all disabled:opacity-50">
                    @if (_isLoading)
                    {
                        <span>Signing in...</span>
                    }
                    else
                    {
                        <span>Sign In</span>
                    }
                </button>
            </div>
            
            <div class="text-center">
                <span class="text-sm text-mystic-500">Don't have an account?</span>
                <a href="/register" class="text-sm font-medium text-mystic-900 hover:underline ml-1">Create one for free</a>
            </div>
        </EditForm>
    </div>
</div>

@code {
    [SupplyParameterFromForm]
    private LoginRequest _loginRequest { get; set; } = new();
    
    private string? _errorMessage;
    private bool _isLoading;

    private async Task HandleLogin()
    {
        try
        {
            _isLoading = true;
            _errorMessage = null;

            var res = await Http.PostAsJsonAsync("api/auth/login", _loginRequest);
            
            if (res.IsSuccessStatusCode)
            {
                var response = await res.Content.ReadFromJsonAsync<AuthResponse>();
                if (response != null && !string.IsNullOrEmpty(response.Token))
                {
                    var jwtProvider = (JwtAuthenticationStateProvider)AuthStateProvider;
                    await jwtProvider.LoginAsync(response.Token);
                    
                    // ensure storage is ready
                    await Task.Delay(100);

                    Navigation.NavigateTo("/");
                    return;
                }
            }
            
            _errorMessage = "Invalid credentials or server error.";
        }
        catch (Exception ex)
        {
            _errorMessage = "An error occurred: " + ex.Message;
        }
        finally
        {
            _isLoading = false;
        }
    }
}