feat(auth): implement login page and authentication flow
Implement a complete authentication system including a new login page, updated authentication service, and conditional routing. - Add `LoginPage` component with animated UI and form validation - Update `AuthService` to handle user sessions, CSRF tokens, and enhanced user data structures - Implement conditional rendering in `App` component based on authentication state - Add `postRaw` and `getRaw` methods to `HttpService` for direct API interaction - Update routing to include login path and prepare for auth guards - Add rate limiting error handling (429) in `HttpService`
This commit is contained in:
@@ -0,0 +1,196 @@
|
||||
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { Router, NavigationEnd } from '@angular/router';
|
||||
import { filter, Subscription } from 'rxjs';
|
||||
import { AuthService, LoginResponse } from '../../services/auth/auth.service';
|
||||
|
||||
type LoginField = 'username' | 'password';
|
||||
type LoginErrors = Partial<Record<LoginField, string>>;
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
standalone: true,
|
||||
imports: [CommonModule, FormsModule],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
templateUrl: './login.page.html',
|
||||
styleUrl: './login.page.css',
|
||||
})
|
||||
export class LoginPage {
|
||||
private readonly authService = inject(AuthService);
|
||||
private readonly router = inject(Router);
|
||||
|
||||
readonly username = signal('');
|
||||
readonly password = signal('');
|
||||
readonly rememberMe = signal(false);
|
||||
readonly showPassword = signal(false);
|
||||
readonly isLoading = signal(false);
|
||||
readonly errors = signal<LoginErrors>({});
|
||||
readonly generalError = signal<string | null>(null);
|
||||
readonly isShaking = signal(false);
|
||||
readonly isEntering = signal(true);
|
||||
readonly mousePosition = signal({ x: 0, y: 0 });
|
||||
|
||||
private routerSubscription?: Subscription;
|
||||
|
||||
constructor() {
|
||||
this.routerSubscription = this.router.events
|
||||
.pipe(filter((event) => event instanceof NavigationEnd))
|
||||
.subscribe(() => {
|
||||
this.resetForm();
|
||||
});
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.restoreCredentials();
|
||||
setTimeout(() => this.isEntering.set(false), 1100);
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.routerSubscription?.unsubscribe();
|
||||
}
|
||||
|
||||
private restoreCredentials(): void {
|
||||
const saved = localStorage.getItem('remembered_credentials');
|
||||
if (saved) {
|
||||
try {
|
||||
const parsed = JSON.parse(saved);
|
||||
if (parsed?.username) {
|
||||
this.username.set(parsed.username);
|
||||
this.rememberMe.set(true);
|
||||
}
|
||||
} catch {
|
||||
// ignore corrupt data
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private saveCredentials(): void {
|
||||
if (this.rememberMe()) {
|
||||
localStorage.setItem('remembered_credentials', JSON.stringify({ username: this.username() }));
|
||||
} else {
|
||||
localStorage.removeItem('remembered_credentials');
|
||||
}
|
||||
}
|
||||
|
||||
onCardMouseMove(event: MouseEvent): void {
|
||||
const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
|
||||
const x = ((event.clientX - rect.left) / rect.width) * 100;
|
||||
const y = ((event.clientY - rect.top) / rect.height) * 100;
|
||||
this.mousePosition.set({ x, y });
|
||||
}
|
||||
|
||||
onCardMouseLeave(): void {
|
||||
this.mousePosition.set({ x: 50, y: 50 });
|
||||
}
|
||||
|
||||
onFieldBlur(field: LoginField): void {
|
||||
this.validateField(field);
|
||||
}
|
||||
|
||||
private validateField(field: LoginField): void {
|
||||
const currentErrors = { ...this.errors() };
|
||||
|
||||
switch (field) {
|
||||
case 'username':
|
||||
if (!this.username().trim()) {
|
||||
currentErrors.username = 'نام کاربری الزامی است';
|
||||
} else {
|
||||
delete currentErrors.username;
|
||||
}
|
||||
break;
|
||||
case 'password':
|
||||
if (!this.password()) {
|
||||
currentErrors.password = 'رمز عبور الزامی است';
|
||||
} else {
|
||||
delete currentErrors.password;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
this.errors.set(currentErrors);
|
||||
}
|
||||
|
||||
validateAllFields(): LoginErrors {
|
||||
const allErrors: LoginErrors = {};
|
||||
|
||||
if (!this.username().trim()) {
|
||||
allErrors.username = 'نام کاربری الزامی است';
|
||||
}
|
||||
|
||||
if (!this.password()) {
|
||||
allErrors.password = 'رمز عبور الزامی است';
|
||||
}
|
||||
|
||||
this.errors.set(allErrors);
|
||||
return allErrors;
|
||||
}
|
||||
|
||||
onSubmit(): void {
|
||||
const allErrors = this.validateAllFields();
|
||||
|
||||
if (Object.keys(allErrors).length > 0) {
|
||||
this.triggerShake();
|
||||
return;
|
||||
}
|
||||
|
||||
this.isLoading.set(true);
|
||||
this.generalError.set(null);
|
||||
this.errors.set({});
|
||||
|
||||
const credentials = {
|
||||
username: this.username().trim(),
|
||||
password: this.password(),
|
||||
};
|
||||
|
||||
this.authService.login(credentials).subscribe({
|
||||
next: (response: LoginResponse) => {
|
||||
this.isLoading.set(false);
|
||||
|
||||
if (!response?.user?.username) {
|
||||
this.generalError.set('نام کاربری یا رمز عبور اشتباه است');
|
||||
this.triggerShake();
|
||||
return;
|
||||
}
|
||||
|
||||
this.saveCredentials();
|
||||
this.router.navigate(['/']);
|
||||
},
|
||||
error: (err) => {
|
||||
this.isLoading.set(false);
|
||||
const message =
|
||||
(err as any)?.error?.message ||
|
||||
(err as any)?.message ||
|
||||
'خطا در اتصال به سرور. لطفاً دوباره تلاش کنید.';
|
||||
this.generalError.set(message);
|
||||
this.triggerShake();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private triggerShake(): void {
|
||||
this.isShaking.set(true);
|
||||
setTimeout(() => this.isShaking.set(false), 700);
|
||||
}
|
||||
|
||||
togglePasswordVisibility(): void {
|
||||
this.showPassword.update((v) => !v);
|
||||
}
|
||||
|
||||
onLogoClick(): void {
|
||||
this.router.navigate(['/login']);
|
||||
}
|
||||
|
||||
private resetForm(): void {
|
||||
this.username.set('');
|
||||
this.password.set('');
|
||||
this.rememberMe.set(false);
|
||||
this.showPassword.set(false);
|
||||
this.isLoading.set(false);
|
||||
this.errors.set({});
|
||||
this.generalError.set(null);
|
||||
this.isShaking.set(false);
|
||||
this.isEntering.set(false);
|
||||
this.mousePosition.set({ x: 50, y: 50 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user