initial commit
This commit is contained in:
@@ -0,0 +1,270 @@
|
||||
@page "/account/login"
|
||||
@layout AuthenticationLayout
|
||||
@using Indotalent.Shared.Consts
|
||||
@using Microsoft.JSInterop
|
||||
@using Indotalent.Infrastructure.Authentication.Identity
|
||||
@using Microsoft.Extensions.Options
|
||||
@inject IJSRuntime JSRuntime
|
||||
@inject IOptions<IdentitySettingsModel> IdentityOptions
|
||||
@inject NavigationManager Navigation
|
||||
@inject ISnackbar Snackbar
|
||||
|
||||
<PageTitle>Sign In - @GlobalConsts.AppInitial</PageTitle>
|
||||
|
||||
<div class="page-header">
|
||||
<h2>Sign In</h2>
|
||||
<p>Enter your credentials to access your account</p>
|
||||
</div>
|
||||
|
||||
<MudForm @ref="form" @bind-IsValid="@success" Validation="@(new Func<EditContext, Task<bool>>(ValidateForm))" Class="w-100">
|
||||
|
||||
<label class="form-label">Email Address</label>
|
||||
<MudTextField T="string"
|
||||
@bind-Value="model.Email"
|
||||
Variant="Variant.Outlined"
|
||||
Placeholder="you@company.com"
|
||||
Margin="Margin.Dense"
|
||||
Adornment="Adornment.Start"
|
||||
AdornmentIcon="@Icons.Material.Filled.Email"
|
||||
Required="true"
|
||||
For="@(() => model.Email)"
|
||||
Class="mb-4" />
|
||||
|
||||
<div class="form-label-row">
|
||||
<label class="form-label">Password</label>
|
||||
<a class="forgot-link" href="/account/forgot-password">Forgot password?</a>
|
||||
</div>
|
||||
<MudTextField T="string"
|
||||
@bind-Value="model.Password"
|
||||
Variant="Variant.Outlined"
|
||||
Placeholder="Enter your password"
|
||||
Margin="Margin.Dense"
|
||||
Adornment="Adornment.Start"
|
||||
AdornmentIcon="@Icons.Material.Filled.Lock"
|
||||
InputType="@(showPassword ? InputType.Text : InputType.Password)"
|
||||
AdornmentEndIcon="@(showPassword? Icons.Material.Filled.Visibility : Icons.Material.Filled.VisibilityOff)"
|
||||
OnAdornmentEndClick="() => showPassword = !showPassword"
|
||||
Required="true"
|
||||
For="@(() => model.Password)"
|
||||
Class="mb-2" />
|
||||
|
||||
<div class="remember-row">
|
||||
<MudCheckBox T="bool"
|
||||
@bind-Value="rememberMe"
|
||||
Label="Remember me"
|
||||
Color="Color.Primary"
|
||||
Dense="true" />
|
||||
</div>
|
||||
|
||||
<MudButton Variant="Variant.Filled"
|
||||
Color="Color.Primary"
|
||||
Size="Size.Large"
|
||||
FullWidth="true"
|
||||
Disabled="!success || isProcessing"
|
||||
OnClick="HandleLogin"
|
||||
Style="text-transform:none; border-radius: 8px; height: 48px; font-weight: 600;">
|
||||
@if (isProcessing)
|
||||
{
|
||||
<MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true" Color="Color.Inherit" />
|
||||
<span class="ms-2">Signing in...</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span>Sign In</span>
|
||||
}
|
||||
</MudButton>
|
||||
|
||||
@if (IdentityOptions.Value.SsoFirebase.IsUsed)
|
||||
{
|
||||
<div class="divider-or">
|
||||
<span>Or continue with</span>
|
||||
</div>
|
||||
|
||||
<MudButton Variant="Variant.Outlined"
|
||||
FullWidth="true"
|
||||
Size="Size.Large"
|
||||
Disabled="isProcessing"
|
||||
OnClick="HandleFirebaseGoogleLogin"
|
||||
StartIcon="@Icons.Custom.Brands.Google"
|
||||
Style="text-transform:none; border-radius: 8px; background-color: white; height: 44px; border: 1.5px solid #e2e8f0; font-weight: 500; color: #475569;">
|
||||
Sign in with Google
|
||||
</MudButton>
|
||||
}
|
||||
</MudForm>
|
||||
|
||||
<div style="text-align: center; margin-top: 1.5rem;">
|
||||
<span style="font-size: 0.875rem; color: #64748b;">
|
||||
Don't have an account?
|
||||
<a class="auth-link" href="/account/register">Create one</a>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
private MudForm form = default!;
|
||||
private bool success;
|
||||
private bool isProcessing;
|
||||
private bool showPassword;
|
||||
private bool rememberMe;
|
||||
private LoginModel model = new();
|
||||
|
||||
private async Task<bool> ValidateForm(EditContext context) => await Task.FromResult(true);
|
||||
|
||||
private async Task HandleLogin()
|
||||
{
|
||||
await form.Validate();
|
||||
if (!success) return;
|
||||
isProcessing = true;
|
||||
StateHasChanged();
|
||||
|
||||
var result = await JSRuntime.InvokeAsync<ApiJSRuntimeResponse>("apiAccountSignIn", model.Email, model.Password, rememberMe);
|
||||
ProcessLoginResult(result);
|
||||
}
|
||||
|
||||
private async Task HandleFirebaseGoogleLogin()
|
||||
{
|
||||
isProcessing = true;
|
||||
StateHasChanged();
|
||||
|
||||
var firebaseUser = await JSRuntime.InvokeAsync<FirebaseUserResponse>("signInWithGoogle");
|
||||
|
||||
if (firebaseUser == null || string.IsNullOrEmpty(firebaseUser.Email))
|
||||
{
|
||||
Snackbar.Add("Google Sign-In failed or cancelled.", Severity.Error);
|
||||
isProcessing = false;
|
||||
return;
|
||||
}
|
||||
|
||||
var checkResult = await JSRuntime.InvokeAsync<ActiveCheckResponse>("apiCheckActiveUser", firebaseUser.Email);
|
||||
bool openForPublic = IdentityOptions.Value.SsoFirebase.OpenForPublic;
|
||||
|
||||
if (checkResult.Exists)
|
||||
{
|
||||
if (checkResult.IsActive)
|
||||
{
|
||||
await ExecuteSsoSignIn(firebaseUser.Email);
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add("Account is registered but not active.", Severity.Warning);
|
||||
isProcessing = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var registerResult = await JSRuntime.InvokeAsync<ApiJSRuntimeResponse>(
|
||||
"apiAccountSignUpSso",
|
||||
firebaseUser.Email,
|
||||
firebaseUser.Email,
|
||||
openForPublic
|
||||
);
|
||||
|
||||
if (registerResult.Status == 200)
|
||||
{
|
||||
if (openForPublic)
|
||||
{
|
||||
await ExecuteSsoSignIn(firebaseUser.Email);
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add("Registration successful. Please wait for admin approval.", Severity.Info);
|
||||
isProcessing = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add("Failed to auto-register account.", Severity.Error);
|
||||
isProcessing = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ExecuteSsoSignIn(string email)
|
||||
{
|
||||
var result = await JSRuntime.InvokeAsync<ApiJSRuntimeResponse>("apiAccountSsoSignIn", email);
|
||||
ProcessLoginResult(result);
|
||||
}
|
||||
|
||||
private void ProcessLoginResult(ApiJSRuntimeResponse result)
|
||||
{
|
||||
if (result.Status == 200)
|
||||
{
|
||||
Snackbar.Add(result.Title ?? "Login successful!", Severity.Success);
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(500);
|
||||
Navigation.NavigateTo("/account/tenant-selection", forceLoad: true);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add(result.Title ?? "Sign in failed.", Severity.Error);
|
||||
isProcessing = false;
|
||||
}
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private class ApiJSRuntimeResponse { public int? Status { get; set; } public string? Title { get; set; } public string? Message { get; set; } }
|
||||
private class FirebaseUserResponse { public string? Email { get; set; } }
|
||||
private class ActiveCheckResponse { public bool Exists { get; set; } public bool IsActive { get; set; } }
|
||||
private class LoginModel { public string Email { get; set; } = ""; public string Password { get; set; } = ""; }
|
||||
}
|
||||
|
||||
<script>
|
||||
window.apiAccountSignIn = async (email, password, rememberMe) => {
|
||||
try {
|
||||
const response = await fetch('/api/account/signin', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ email, password, rememberMe }),
|
||||
credentials: 'include'
|
||||
});
|
||||
const result = await response.json();
|
||||
return { status: response.status, title: result.title };
|
||||
} catch (error) {
|
||||
return { status: 500, title: 'Network error' };
|
||||
}
|
||||
};
|
||||
|
||||
window.apiCheckActiveUser = async (email) => {
|
||||
try {
|
||||
const response = await fetch(`/api/account/active-user-exists?email=${encodeURIComponent(email)}`);
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
return { exists: false, isActive: false };
|
||||
}
|
||||
};
|
||||
|
||||
window.apiAccountSignUpSso = async (email, fullName, isOpenForPublic) => {
|
||||
try {
|
||||
const response = await fetch('/api/account/signup', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
email: email,
|
||||
fullName: fullName,
|
||||
password: 'SSO_AUTO_GENERATED_PASSWORD_123!',
|
||||
isActive: isOpenForPublic,
|
||||
emailConfirmed: isOpenForPublic
|
||||
})
|
||||
});
|
||||
const result = await response.json();
|
||||
return { status: response.status, message: result.message };
|
||||
} catch (error) {
|
||||
return { status: 500 };
|
||||
}
|
||||
};
|
||||
|
||||
window.apiAccountSsoSignIn = async (email) => {
|
||||
try {
|
||||
const response = await fetch(`/api/account/signin-sso?email=${encodeURIComponent(email)}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'include'
|
||||
});
|
||||
const result = await response.json();
|
||||
return { status: response.status, title: result.title };
|
||||
} catch (error) {
|
||||
return { status: 500, title: 'SSO Network error' };
|
||||
}
|
||||
};
|
||||
</script>
|
||||
@@ -0,0 +1,74 @@
|
||||
using Indotalent.ConfigFrontEnd.Service;
|
||||
using Indotalent.Data.Entities;
|
||||
using Indotalent.Infrastructure.Authentication.Identity;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Indotalent.Features.Account.Login.Cqrs;
|
||||
|
||||
public class LoginRequest
|
||||
{
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
public bool RememberMe { get; set; }
|
||||
}
|
||||
|
||||
public class LoginResponse
|
||||
{
|
||||
public bool Succeeded { get; set; }
|
||||
public string Message { get; set; } = string.Empty;
|
||||
public bool IsNotAllowed { get; set; }
|
||||
public string? Token { get; set; }
|
||||
public string? RefreshToken { get; set; }
|
||||
}
|
||||
|
||||
public record LoginCommand(LoginRequest Data) : IRequest<LoginResponse>;
|
||||
|
||||
public class LoginHandler : IRequestHandler<LoginCommand, LoginResponse>
|
||||
{
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly JwtSettingsModel _jwtSettings;
|
||||
|
||||
public LoginHandler(
|
||||
SignInManager<ApplicationUser> signInManager,
|
||||
UserManager<ApplicationUser> userManager,
|
||||
IOptions<JwtSettingsModel> jwtSettings)
|
||||
{
|
||||
_signInManager = signInManager;
|
||||
_userManager = userManager;
|
||||
_jwtSettings = jwtSettings.Value;
|
||||
}
|
||||
|
||||
public async Task<LoginResponse> Handle(LoginCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var user = await _userManager.FindByEmailAsync(request.Data.Email);
|
||||
if (user == null) return new LoginResponse { Succeeded = false, Message = "Invalid credentials" };
|
||||
|
||||
var result = await _signInManager.PasswordSignInAsync(user, request.Data.Password, request.Data.RememberMe, false);
|
||||
|
||||
if (result.Succeeded)
|
||||
{
|
||||
var token = JwtService.GenerateNewJwt(user, _jwtSettings);
|
||||
|
||||
var refreshToken = Guid.NewGuid().ToString().Replace("-", "");
|
||||
|
||||
user.RefreshToken = refreshToken;
|
||||
user.LastLoginAt = DateTime.Now;
|
||||
await _userManager.UpdateAsync(user);
|
||||
|
||||
return new LoginResponse
|
||||
{
|
||||
Succeeded = true,
|
||||
Message = "Login successful",
|
||||
Token = token,
|
||||
RefreshToken = refreshToken
|
||||
};
|
||||
}
|
||||
|
||||
return new LoginResponse { Succeeded = false, Message = "Invalid credentials" };
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user