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:
PeymanHonarjoo
2026-10-08 12:49:13 +03:30
parent 4db40bfc64
commit 447849289e
9 changed files with 1470 additions and 39 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

+4
View File
@@ -1 +1,5 @@
@if (isAuthenticated()) {
<app-layout></app-layout> <app-layout></app-layout>
} @else {
<app-login></app-login>
}
+24
View File
@@ -1,16 +1,40 @@
import { Routes } from '@angular/router'; import { Routes } from '@angular/router';
import { HomePage } from './pages/home/home.page'; import { HomePage } from './pages/home/home.page';
import { SettingsPage } from './pages/settings/settings.page'; import { SettingsPage } from './pages/settings/settings.page';
import { LoginPage } from './pages/login/login.page';
import { AuthService } from './services/auth/auth.service';
import { authGuard } from './services/auth/auth.guard';
import { Router } from '@angular/router';
import { inject } from '@angular/core';
const redirectAuthenticated = () => {
const authService = inject(AuthService);
const router = inject(Router);
if (authService.isAuthenticated()) {
router.navigate(['/']);
return false;
}
return true;
};
export const routes: Routes = [ export const routes: Routes = [
{
path: 'login',
title: 'ورود',
component: LoginPage,
// canActivate: [redirectAuthenticated],
},
{ {
path: '', path: '',
title: 'دستیار هوشمند', title: 'دستیار هوشمند',
component: HomePage, component: HomePage,
// canActivate: [authGuard],
}, },
{ {
path: 'settings', path: 'settings',
title: 'تنظیمات', title: 'تنظیمات',
component: SettingsPage, component: SettingsPage,
// canActivate: [authGuard],
}, },
]; ];
+20 -4
View File
@@ -1,10 +1,26 @@
import { Component } from '@angular/core'; import { Component, inject, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LayoutComponent } from './layout/layout.component'; import { LayoutComponent } from './layout/layout.component';
import { LoginPage } from './pages/login/login.page';
import { AuthService } from './services/auth/auth.service';
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
imports: [LayoutComponent], standalone: true,
imports: [CommonModule, LayoutComponent, LoginPage],
templateUrl: './app.html', templateUrl: './app.html',
styleUrl: './app.css' styleUrl: './app.css',
}) })
export class App {} export class App {
private readonly authService = inject(AuthService);
readonly isAuthenticated = signal<boolean>(this.authService.isAuthenticated());
constructor() {
this.authService.currentUserSubject.subscribe({
next: () => {
this.isAuthenticated.set(this.authService.isAuthenticated());
},
});
}
}
+894
View File
@@ -0,0 +1,894 @@
.login-page {
position: relative;
display: flex;
align-items: center;
justify-content: center;
min-height: 100dvh;
padding: 1.5rem 1rem;
box-sizing: border-box;
overflow: hidden;
background: #0f172a;
transition: background 0.6s ease;
}
.login-page.shaking {
overflow: hidden;
}
.login-card {
position: relative;
z-index: 1;
width: 100%;
max-width: 440px;
perspective: 1200px;
will-change: transform;
transform: translate3d(0, 0, 0);
transition: transform 0.5s cubic-bezier(0.22, 1, 0.36, 1);
}
.login-card.enter {
animation: cardEntrance 1s cubic-bezier(0.22, 1, 0.36, 1) both;
}
.login-card.shaking {
animation: shake 0.7s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}
.login-ambient-bg {
position: absolute;
inset: 0;
overflow: hidden;
pointer-events: none;
z-index: 0;
}
.particles {
position: absolute;
inset: 0;
background-image:
radial-gradient(circle, rgba(148, 163, 184, 0.08) 1px, transparent 1px),
radial-gradient(circle, rgba(148, 163, 184, 0.06) 1px, transparent 1px);
background-size: 50px 50px, 80px 80px;
background-position: 0 0, 25px 25px;
}
.particles-spotlight {
position: absolute;
inset: 0;
background-image:
radial-gradient(circle, rgba(148, 163, 184, 0.35) 1px, transparent 1px),
radial-gradient(circle, rgba(148, 163, 184, 0.25) 1px, transparent 1px);
background-size: 50px 50px, 80px 80px;
background-position: 0 0, 25px 25px;
mask-image: radial-gradient(
circle 200px at var(--mouse-x) var(--mouse-y),
black 0%,
black 25%,
transparent 65%
);
-webkit-mask-image: radial-gradient(
circle 200px at var(--mouse-x) var(--mouse-y),
black 0%,
black 25%,
transparent 65%
);
transition: mask-image 0.12s ease-out;
}
@keyframes particlesFloat {
0% {
transform: translateY(0);
}
100% {
transform: translateY(-50px);
}
}
.blob {
position: absolute;
border-radius: 50%;
filter: blur(90px);
opacity: 0.55;
animation: floatBlob 14s ease-in-out infinite;
will-change: transform;
}
.blob-1 {
width: 520px;
height: 520px;
background: radial-gradient(circle, rgba(59, 130, 246, 0.5), transparent 70%);
top: -18%;
right: -12%;
animation-delay: 0s;
animation-duration: 16s;
}
.blob-2 {
width: 420px;
height: 420px;
background: radial-gradient(circle, rgba(139, 92, 246, 0.4), transparent 70%);
bottom: -14%;
left: -10%;
animation-delay: -6s;
animation-duration: 18s;
}
.blob-3 {
width: 360px;
height: 360px;
background: radial-gradient(circle, rgba(59, 130, 246, 0.35), transparent 70%);
top: 45%;
left: 45%;
animation-delay: -11s;
animation-duration: 20s;
}
@keyframes floatBlob {
0%,
100% {
transform: translate(0, 0) scale(1);
}
25% {
transform: translate(50px, -40px) scale(1.1);
}
50% {
transform: translate(-30px, 35px) scale(0.92);
}
75% {
transform: translate(-45px, -20px) scale(1.06);
}
}
.login-card-inner {
position: relative;
background: rgba(15, 23, 42, 0.65);
border: 1px solid rgba(148, 163, 184, 0.15);
border-radius: 1.5rem;
padding: 2.5rem 2rem 2rem;
box-shadow:
0 0 0 1px rgba(255, 255, 255, 0.05) inset,
0 20px 50px -12px rgba(0, 0, 0, 0.5),
0 0 80px -20px rgba(59, 130, 246, 0.3);
transition:
background 0.6s ease,
border-color 0.6s ease,
box-shadow 0.6s ease,
transform 0.4s cubic-bezier(0.22, 1, 0.36, 1);
animation: innerReveal 1.1s cubic-bezier(0.22, 1, 0.36, 1) 0.12s both;
transform-style: preserve-3d;
}
.login-card-inner:hover {
transform: translateY(-4px) rotateX(2deg);
box-shadow:
0 0 0 1px rgba(255, 255, 255, 0.08) inset,
0 30px 70px -15px rgba(0, 0, 0, 0.6),
0 0 100px -20px rgba(59, 130, 246, 0.4);
}
.login-spotlight {
position: absolute;
inset: 0;
border-radius: inherit;
background: radial-gradient(
700px circle at var(--mouse-x) var(--mouse-y),
rgba(59, 130, 246, 0.2),
transparent 40%
);
pointer-events: none;
z-index: 0;
opacity: 0;
transition: opacity 0.5s ease;
mix-blend-mode: screen;
}
.login-card-inner:hover .login-spotlight {
opacity: 1;
}
.login-glow-border {
position: absolute;
inset: -1px;
border-radius: inherit;
background: radial-gradient(
500px circle at var(--mouse-x) var(--mouse-y),
rgba(59, 130, 246, 0.7),
transparent 50%
);
pointer-events: none;
z-index: -1;
opacity: 0;
transition: opacity 0.5s ease;
mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
mask-composite: exclude;
padding: 1px;
}
.login-card-inner:hover .login-glow-border {
opacity: 1;
}
.login-header {
position: relative;
z-index: 1;
text-align: center;
margin-bottom: 2rem;
animation: fadeInUp 0.9s cubic-bezier(0.22, 1, 0.36, 1) 0.2s both;
}
.login-logo-wrapper {
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
width: 96px;
height: 96px;
border-radius: 50%;
background: rgba(59, 130, 246, 0.1);
margin-bottom: 1.25rem;
padding: 0.625rem;
cursor: pointer;
transition: all 0.5s cubic-bezier(0.22, 1, 0.36, 1);
animation: logoEntrance 1.1s cubic-bezier(0.22, 1, 0.36, 1) 0.25s both;
}
.login-logo-wrapper:hover {
transform: scale(1.12) rotate(10deg);
background: rgba(59, 130, 246, 0.2);
box-shadow: 0 0 50px rgba(59, 130, 246, 0.4);
}
.logo-ring {
position: absolute;
inset: -6px;
border-radius: 50%;
border: 2px solid transparent;
background: linear-gradient(135deg, rgba(59, 130, 246, 0.7), rgba(139, 92, 246, 0.4), rgba(59, 130, 246, 0.7)) border-box;
mask: linear-gradient(#fff 0 0) padding-box, linear-gradient(#fff 0 0);
mask-composite: exclude;
-webkit-mask-composite: xor;
animation: logoRingPulse 3s ease-in-out infinite;
}
@keyframes logoRingPulse {
0%,
100% {
opacity: 0.5;
transform: scale(1) rotate(0deg);
}
50% {
opacity: 1;
transform: scale(1.15) rotate(180deg);
}
}
.login-logo {
position: relative;
z-index: 1;
width: 100%;
height: 100%;
object-fit: contain;
display: block;
transition: transform 0.5s cubic-bezier(0.22, 1, 0.36, 1);
filter: drop-shadow(0 4px 16px rgba(0, 0, 0, 0.4));
}
.login-logo-wrapper:hover .login-logo {
transform: scale(1.1) rotate(-8deg);
}
.login-title {
margin: 0 0 0.5rem;
font-size: 1.75rem;
font-weight: 700;
color: #f1f5f9;
line-height: 1.3;
letter-spacing: -0.02em;
text-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
animation: titleGlow 4s ease-in-out infinite;
}
@keyframes titleGlow {
0%,
100% {
text-shadow: 0 2px 8px rgba(0, 0, 0, 0.4), 0 0 0 transparent;
}
50% {
text-shadow: 0 2px 8px rgba(0, 0, 0, 0.4), 0 0 40px rgba(59, 130, 246, 0.5), 0 0 80px rgba(139, 92, 246, 0.3);
}
}
.login-subtitle {
margin: 0;
font-size: 0.9375rem;
color: #94a3b8;
line-height: 1.5;
animation: fadeInUp 0.9s cubic-bezier(0.22, 1, 0.36, 1) 0.3s both;
}
.login-form {
position: relative;
z-index: 1;
display: flex;
flex-direction: column;
gap: 1.25rem;
animation: formReveal 1s cubic-bezier(0.22, 1, 0.36, 1) 0.4s both;
}
@keyframes formReveal {
from {
opacity: 0;
transform: translateY(32px) scale(0.94);
}
to {
opacity: 1;
transform: translateY(0) scale(1);
}
}
.form-field {
position: relative;
display: flex;
flex-direction: column;
gap: 0.5rem;
animation: fieldSlideIn 0.8s cubic-bezier(0.22, 1, 0.36, 1) both;
}
.form-field:nth-child(1) {
animation-delay: 0.5s;
}
.form-field:nth-child(2) {
animation-delay: 0.65s;
}
@keyframes fieldSlideIn {
from {
opacity: 0;
transform: translateX(-20px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
.form-label {
display: block;
font-size: 0.875rem;
font-weight: 500;
color: #e2e8f0;
line-height: 1.4;
transition: color 0.3s ease;
}
.form-field:focus-within .form-label {
color: #60a5fa;
}
.input-wrapper {
position: relative;
display: flex;
align-items: center;
}
.input-icon {
position: absolute;
right: 0.875rem;
top: 50%;
transform: translateY(-50%);
color: #64748b;
pointer-events: none;
display: flex;
align-items: center;
transition: all 0.3s ease;
}
.form-field:focus-within .input-icon {
color: #60a5fa;
transform: translateY(-50%) scale(1.2);
}
.login-page .form-input {
width: 100%;
padding: 0.875rem 2.75rem 0.875rem 0.875rem;
border: 1.5px solid rgba(148, 163, 184, 0.2);
border-radius: 0.75rem;
background-color: rgba(15, 23, 42, 0.6) !important;
background: rgba(15, 23, 42, 0.6) !important;
color: #f1f5f9 !important;
font-family: inherit;
font-size: 0.9375rem;
line-height: 1.5;
transition: all 0.3s cubic-bezier(0.22, 1, 0.36, 1);
box-sizing: border-box;
appearance: none !important;
-webkit-appearance: none !important;
-moz-appearance: none !important;
}
.login-page .form-input:-webkit-autofill,
.login-page .form-input:-webkit-autofill:hover,
.login-page .form-input:-webkit-autofill:focus,
.login-page .form-input:-webkit-autofill:active {
-webkit-box-shadow: 0 0 0 1000px rgba(15, 23, 42, 0.85) inset !important;
box-shadow: 0 0 0 1000px rgba(15, 23, 42, 0.85) inset !important;
-webkit-text-fill-color: #f1f5f9 !important;
caret-color: #f1f5f9 !important;
border-color: rgba(148, 163, 184, 0.2) !important;
transition: background-color 5000s ease-in-out 0s;
}
.login-page .form-input:autofill {
background-color: rgba(15, 23, 42, 0.85) !important;
color: #f1f5f9 !important;
-webkit-text-fill-color: #f1f5f9 !important;
}
.login-page .form-input:disabled:-webkit-autofill {
-webkit-box-shadow: 0 0 0 1000px rgba(15, 23, 42, 0.4) inset !important;
box-shadow: 0 0 0 1000px rgba(15, 23, 42, 0.4) inset !important;
}
.login-page .form-input::placeholder {
color: #64748b;
opacity: 0.8;
}
.login-page .form-input:focus {
outline: none;
border-color: #3b82f6;
background-color: rgba(15, 23, 42, 0.8) !important;
background: rgba(15, 23, 42, 0.8) !important;
box-shadow:
0 0 0 4px rgba(59, 130, 246, 0.15),
0 0 40px rgba(59, 130, 246, 0.25),
0 0 0 1px rgba(59, 130, 246, 0.4) inset;
transform: translateY(-2px);
}
.login-page .form-input:disabled {
opacity: 0.5;
cursor: not-allowed;
background-color: rgba(15, 23, 42, 0.4) !important;
background: rgba(15, 23, 42, 0.4) !important;
}
.login-page .form-field.has-error .form-input {
border-color: #ef4444;
box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.15);
animation: errorPulse 2s ease-in-out infinite;
}
@keyframes errorPulse {
0%,
100% {
box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.15);
}
50% {
box-shadow: 0 0 0 8px rgba(239, 68, 68, 0.08);
}
}
.form-field.has-error .input-icon {
color: #ef4444;
}
.field-error {
display: flex;
align-items: center;
gap: 0.375rem;
font-size: 0.8125rem;
color: #f87171;
line-height: 1.4;
animation: errorShake 0.5s ease both;
}
@keyframes errorShake {
0%,
100% {
transform: translateX(0);
}
20% {
transform: translateX(-5px);
}
40% {
transform: translateX(5px);
}
60% {
transform: translateX(-4px);
}
80% {
transform: translateX(4px);
}
}
.password-toggle {
position: absolute;
left: 0.625rem;
top: 50%;
transform: translateY(-50%);
display: inline-flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
border: none;
background: transparent;
color: #64748b;
border-radius: 0.5rem;
cursor: pointer;
transition: all 0.2s ease;
padding: 0;
}
.password-toggle:hover {
color: #e2e8f0;
background: rgba(59, 130, 246, 0.15);
transform: translateY(-50%) scale(1.2);
}
.password-toggle:active {
transform: translateY(-50%) scale(0.9);
}
.password-toggle:disabled {
opacity: 0.4;
cursor: not-allowed;
}
.form-options {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
animation: fadeInUp 0.9s cubic-bezier(0.22, 1, 0.36, 1) 0.8s both;
}
.remember-me {
display: flex;
align-items: center;
gap: 0.5rem;
cursor: pointer;
user-select: none;
position: relative;
}
.remember-checkbox {
width: 1.125rem;
height: 1.125rem;
accent-color: #3b82f6;
cursor: pointer;
flex-shrink: 0;
transition: transform 0.2s ease;
filter: invert(1) brightness(0.9);
}
.remember-checkbox:checked {
filter: invert(0) brightness(1);
}
.remember-me:hover .remember-checkbox {
transform: scale(1.25);
}
.remember-label {
font-size: 0.875rem;
color: #cbd5e1;
line-height: 1.4;
}
.general-error {
display: flex;
align-items: center;
gap: 0.625rem;
padding: 0.875rem 1rem;
background: rgba(239, 68, 68, 0.1);
border: 1px solid rgba(239, 68, 68, 0.25);
border-radius: 0.75rem;
color: #fca5a5;
font-size: 0.875rem;
line-height: 1.4;
animation: errorReveal 0.5s cubic-bezier(0.22, 1, 0.36, 1) both;
box-shadow: 0 0 25px rgba(239, 68, 68, 0.2);
}
@keyframes errorReveal {
from {
opacity: 0;
transform: translateY(-10px) scale(0.95);
}
to {
opacity: 1;
transform: translateY(0) scale(1);
}
}
.login-submit {
position: relative;
display: flex;
align-items: center;
justify-content: center;
gap: 0.625rem;
width: 100%;
padding: 0.9375rem 1.5rem;
border: none;
border-radius: 0.75rem;
background: linear-gradient(135deg, #3b82f6 0%, #6366f1 50%, #8b5cf6 100%);
background-size: 200% 200%;
color: #ffffff;
font-family: inherit;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
overflow: hidden;
transition: all 0.4s cubic-bezier(0.22, 1, 0.36, 1);
box-shadow:
0 0 0 1px rgba(255, 255, 255, 0.1) inset,
0 4px 20px rgba(59, 130, 246, 0.5),
0 0 50px rgba(99, 102, 241, 0.4);
animation: submitEntrance 1s cubic-bezier(0.22, 1, 0.36, 1) 0.75s both;
}
.login-submit::before {
content: '';
position: absolute;
inset: 0;
background: linear-gradient(
135deg,
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 0.3) 50%,
rgba(255, 255, 255, 0) 100%
);
transform: translateX(-100%) skewX(-25deg);
transition: transform 0.8s ease;
}
.login-submit:hover:not(:disabled)::before {
transform: translateX(100%) skewX(-25deg);
}
.login-submit::after {
content: '';
position: absolute;
inset: 0;
background: radial-gradient(
circle at center,
rgba(255, 255, 255, 0.25) 0%,
transparent 70%
);
opacity: 0;
transition: opacity 0.3s ease;
}
.login-submit:active:not(:disabled)::after {
opacity: 1;
}
.login-submit:hover:not(:disabled) {
transform: translateY(-3px);
background-position: 100% 0;
box-shadow:
0 0 0 1px rgba(255, 255, 255, 0.2) inset,
0 10px 30px rgba(59, 130, 246, 0.6),
0 0 80px rgba(99, 102, 241, 0.5);
}
.login-submit:active:not(:disabled) {
transform: translateY(0) scale(0.96);
box-shadow:
0 0 0 1px rgba(255, 255, 255, 0.1) inset,
0 2px 10px rgba(59, 130, 246, 0.5),
0 0 40px rgba(99, 102, 241, 0.3);
transition: all 0.1s ease;
}
.login-submit:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none;
}
.login-submit.loading {
pointer-events: none;
}
.btn-shimmer {
position: absolute;
inset: 0;
background: linear-gradient(
90deg,
transparent 0%,
rgba(255, 255, 255, 0.2) 50%,
transparent 100%
);
animation: shimmer 3s ease-in-out infinite;
}
@keyframes shimmer {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(100%);
}
}
.btn-text {
position: relative;
z-index: 1;
}
.btn-arrow {
position: relative;
z-index: 1;
display: inline-flex;
transition: transform 0.4s cubic-bezier(0.22, 1, 0.36, 1);
}
.login-submit:hover:not(:disabled) .btn-arrow {
transform: translateX(8px);
}
.spinner {
width: 1.25rem;
height: 1.25rem;
border: 2.5px solid rgba(255, 255, 255, 0.2);
border-top-color: #ffffff;
border-radius: 50%;
animation: spin 0.7s linear infinite;
flex-shrink: 0;
position: relative;
z-index: 1;
box-shadow: 0 0 15px rgba(255, 255, 255, 0.4);
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(24px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes logoEntrance {
from {
opacity: 0;
transform: scale(0.4) translateY(30px) rotate(-20deg);
}
to {
opacity: 1;
transform: scale(1) translateY(0) rotate(0deg);
}
}
@keyframes cardEntrance {
from {
opacity: 0;
transform: translateY(60px) scale(0.8) rotateX(15deg);
}
to {
opacity: 1;
transform: translateY(0) scale(1) rotateX(0deg);
}
}
@keyframes innerReveal {
from {
opacity: 0;
transform: scale(0.88);
}
to {
opacity: 1;
transform: scale(1);
}
}
@keyframes shake {
0%,
100% {
transform: translate3d(0, 0, 0);
}
12% {
transform: translate3d(-10px, 0, 0);
}
24% {
transform: translate3d(10px, 0, 0);
}
36% {
transform: translate3d(-8px, 0, 0);
}
48% {
transform: translate3d(8px, 0, 0);
}
60% {
transform: translate3d(-5px, 0, 0);
}
72% {
transform: translate3d(5px, 0, 0);
}
84% {
transform: translate3d(-2px, 0, 0);
}
}
@media (max-width: 480px) {
.login-page {
padding: 1rem 0.75rem;
align-items: flex-start;
padding-top: 3.5rem;
}
.login-card-inner {
padding: 2rem 1.25rem 1.5rem;
border-radius: 1.25rem;
}
.login-logo-wrapper {
width: 80px;
height: 80px;
padding: 0.5rem;
}
.login-title {
font-size: 1.375rem;
}
.login-subtitle {
font-size: 0.875rem;
}
.form-input {
padding: 0.75rem 2.5rem 0.75rem 0.75rem;
font-size: 0.875rem;
}
.login-submit {
padding: 0.8125rem 1.25rem;
font-size: 0.9375rem;
}
}
@media (min-width: 1024px) {
.login-page {
padding: 2rem;
}
.login-card-inner {
padding: 3rem 2.5rem 2.5rem;
}
}
@media (prefers-reduced-motion: reduce) {
.login-page,
.login-card,
.login-card-inner,
.login-header,
.login-logo-wrapper,
.login-form,
.form-options,
.login-submit,
.form-field {
animation: none !important;
transition: none !important;
}
.blob {
animation: none !important;
}
.login-submit::before,
.btn-shimmer,
.login-spotlight {
display: none;
}
}
+225
View File
@@ -0,0 +1,225 @@
<div class="login-page" (mousemove)="onCardMouseMove($event)" (mouseleave)="onCardMouseLeave()">
<div class="login-ambient-bg" aria-hidden="true">
<span class="blob blob-1"></span>
<span class="blob blob-2"></span>
<span class="blob blob-3"></span>
<div
class="particles"
aria-hidden="true"
[style.--mouse-x]="mousePosition().x + '%'"
[style.--mouse-y]="mousePosition().y + '%'"
>
<div class="particles-spotlight" aria-hidden="true"></div>
</div>
</div>
<div class="login-card" [class.enter]="isEntering()" [class.shaking]="isShaking()">
<div
class="login-card-inner"
[style.--mouse-x]="mousePosition().x + '%'"
[style.--mouse-y]="mousePosition().y + '%'"
>
<div class="login-spotlight" aria-hidden="true"></div>
<div class="login-glow-border" aria-hidden="true"></div>
<div class="login-header">
<div class="login-logo-wrapper">
<div class="logo-ring" aria-hidden="true"></div>
<img
class="login-logo"
src="assets/images/RaymandLogo.png"
alt="Raymand"
(click)="onLogoClick()"
/>
</div>
<h1 class="login-title">ورود به حساب کاربری</h1>
</div>
<form class="login-form" (ngSubmit)="onSubmit()" novalidate autocomplete="on">
<div class="form-field" [class.has-error]="errors().username">
<label for="login-username" class="form-label"> نام کاربری </label>
<div class="input-wrapper">
<span class="input-icon" aria-hidden="true">
<svg
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2" />
<circle cx="12" cy="7" r="4" />
</svg>
</span>
<input
id="login-username"
type="text"
class="form-input"
[value]="username()"
(input)="username.set($any($event.target).value)"
(blur)="onFieldBlur('username')"
placeholder="نام کاربری خود را وارد کنید"
autocomplete="username"
[disabled]="isLoading()"
aria-invalid="!!errors().username"
aria-describedby="username-error"
/>
</div>
@if (errors().username) {
<span id="username-error" class="field-error" role="alert">
{{ errors().username }}
</span>
}
</div>
<div class="form-field" [class.has-error]="errors().password">
<label for="login-password" class="form-label"> رمز عبور </label>
<div class="input-wrapper">
<span class="input-icon" aria-hidden="true">
<svg
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<rect x="3" y="11" width="18" height="11" rx="2" ry="2" />
<path d="M7 11V7a5 5 0 0 1 10 0v4" />
</svg>
</span>
<input
id="login-password"
[type]="showPassword() ? 'text' : 'password'"
class="form-input"
[value]="password()"
(input)="password.set($any($event.target).value)"
(blur)="onFieldBlur('password')"
placeholder="رمز عبور خود را وارد کنید"
autocomplete="current-password"
[disabled]="isLoading()"
aria-invalid="!!errors().password"
aria-describedby="password-error"
/>
<button
type="button"
class="password-toggle"
(click)="togglePasswordVisibility()"
[attr.aria-label]="showPassword() ? 'مخفی کردن رمز عبور' : 'نمایش رمز عبور'"
[disabled]="isLoading()"
>
@if (showPassword()) {
<svg
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<path
d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94"
/>
<path
d="M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19"
/>
<path d="M14.12 14.12a3 3 0 1 1-4.24-4.24" />
<line x1="1" y1="1" x2="23" y2="23" />
</svg>
} @else {
<svg
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" />
<circle cx="12" cy="12" r="3" />
</svg>
}
</button>
</div>
@if (errors().password) {
<span id="password-error" class="field-error" role="alert">
{{ errors().password }}
</span>
}
</div>
<div class="form-options">
<label class="remember-me">
<input
type="checkbox"
class="remember-checkbox"
[checked]="rememberMe()"
(change)="rememberMe.set($any($event.target).checked)"
[disabled]="isLoading()"
/>
<span class="remember-label">مرا به خاطر بسپار</span>
</label>
</div>
@if (generalError()) {
<div class="general-error" role="alert">
<svg
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<circle cx="12" cy="12" r="10" />
<line x1="12" y1="8" x2="12" y2="12" />
<line x1="12" y1="16" x2="12.01" y2="16" />
</svg>
<span>{{ generalError() }}</span>
</div>
}
<button
type="submit"
class="login-submit"
[class.loading]="isLoading()"
[disabled]="isLoading()"
>
<span class="btn-shimmer" aria-hidden="true"></span>
@if (isLoading()) {
<span class="spinner" aria-hidden="true"></span>
<span class="btn-text">در حال ورود...</span>
} @else {
<span class="btn-text">ورود</span>
<span class="btn-arrow" aria-hidden="true">
<svg
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2.5"
stroke-linecap="round"
stroke-linejoin="round"
>
<path d="M5 12h14M12 5l7 7-7 7" />
</svg>
</span>
}
</button>
</form>
</div>
</div>
</div>
+196
View File
@@ -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 });
}
}
+81 -34
View File
@@ -1,65 +1,112 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable, BehaviorSubject, forkJoin, of } from 'rxjs'; import { Observable, BehaviorSubject, forkJoin, of, throwError } from 'rxjs';
import { tap, map, switchMap, catchError } from 'rxjs/operators'; import { tap, map, switchMap, catchError } from 'rxjs/operators';
import { HttpService } from '../http-request/http.service'; import { HttpService } from '../http-request/http.service';
interface ApiResult<T> { export interface AuthUser {
hasError: boolean; id: string;
messages: string[];
result: T;
}
interface LoginResponse {
auth: boolean;
token: string;
username: string; username: string;
role: string;
organization_id: string;
} }
interface LoginResult extends ApiResult<LoginResponse> { export interface LoginResponse {
licenseValid?: boolean; user: AuthUser;
csrf_token: string;
expires_at: string;
}
export interface CurrentUserResponse {
user: AuthUser;
csrf_token: string;
expires_at: string;
} }
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class AuthService { export class AuthService {
public currentUserSubject = new BehaviorSubject<string | null>( public currentUserSubject = new BehaviorSubject<AuthUser | null>(null);
localStorage.getItem('username'), private csrfToken: string | null = null;
);
constructor( constructor(
private httpService: HttpService, private httpService: HttpService,
) { ) {
let previousUser: string | null = this.currentUserSubject.value; const savedUser = localStorage.getItem('current_user');
this.currentUserSubject.subscribe((username) => { const savedCsrf = localStorage.getItem('csrf_token');
if (username !== previousUser) { if (savedUser) {
previousUser = username; try {
this.currentUserSubject.next(JSON.parse(savedUser));
} catch {
// ignore corrupt data
}
}
if (savedCsrf) {
this.csrfToken = savedCsrf;
} }
});
} }
login(credentials: { login(credentials: {
username: string; username: string;
password: string; password: string;
rememberMe: boolean; }): Observable<LoginResponse> {
}): Observable<LoginResult> { return this.httpService.postRaw<LoginResponse>('v1/auth/login', credentials).pipe(
return this.httpService.post<LoginResponse>('users/login', credentials).pipe( tap((response) => {
tap((response: ApiResult<LoginResponse>) => { this.currentUserSubject.next(response.user);
if (!response.hasError && response.result?.auth) { this.csrfToken = response.csrf_token;
localStorage.setItem('access_token', response.result.token); localStorage.setItem('current_user', JSON.stringify(response.user));
localStorage.setItem('username', response.result.username); localStorage.setItem('csrf_token', response.csrf_token);
this.currentUserSubject.next(response.result.username); }),
} catchError((err) => {
this.clearAuth();
return throwError(() => err);
}), }),
); );
} }
logout(): void { logout(): Observable<void> {
localStorage.removeItem('access_token'); const headers: Record<string, string> = {};
localStorage.removeItem('username'); if (this.csrfToken) {
this.currentUserSubject.next(null); headers['X-CSRF-Token'] = this.csrfToken;
}
return this.httpService.postRaw<void>('v1/auth/logout', {}, { headers }).pipe(
tap(() => {
this.clearAuth();
}),
catchError((err) => {
this.clearAuth();
return throwError(() => err);
}),
);
}
getCurrentUser(): Observable<CurrentUserResponse> {
return this.httpService.getRaw<CurrentUserResponse>('v1/me').pipe(
tap((response) => {
this.currentUserSubject.next(response.user);
this.csrfToken = response.csrf_token;
localStorage.setItem('current_user', JSON.stringify(response.user));
localStorage.setItem('csrf_token', response.csrf_token);
}),
);
} }
isAuthenticated(): boolean { isAuthenticated(): boolean {
return !!localStorage.getItem('access_token'); return !!this.currentUserSubject.value;
}
getCurrentUserValue(): AuthUser | null {
return this.currentUserSubject.value;
}
getCsrfToken(): string | null {
return this.csrfToken;
}
private clearAuth(): void {
this.currentUserSubject.next(null);
this.csrfToken = null;
localStorage.removeItem('current_user');
localStorage.removeItem('csrf_token');
} }
} }
@@ -94,6 +94,13 @@ export class HttpService {
return throwError(() => new Error('Unauthorized')); return throwError(() => new Error('Unauthorized'));
} }
if (error.status === 429 && !skipToast) {
if (this.shouldDisplayMessage('تلاش‌های بیش از حد مجاز. لطفاً بعداً تلاش کنید.')) {
this.toast.warning('تلاش‌های بیش از حد مجاز. لطفاً بعداً تلاش کنید.');
}
return throwError(() => new Error('RATE_LIMITED'));
}
if (error.status === 422 && error.error && !skipToast) { if (error.status === 422 && error.error && !skipToast) {
try { try {
const apiError = error.error as ApiResult<any>; const apiError = error.error as ApiResult<any>;
@@ -254,6 +261,24 @@ export class HttpService {
); );
} }
postRaw<T>(endpoint: string, data: any, options: HttpOptions = {}): Observable<T> {
const url = `${this.baseUrl}/${endpoint}`;
const headers = options.headers ? new HttpHeaders(options.headers) : new HttpHeaders();
return this.http.post<T>(url, data, {
headers,
params: options.params,
});
}
getRaw<T>(endpoint: string, options: HttpOptions = {}): Observable<T> {
const url = `${this.baseUrl}/${endpoint}`;
const headers = options.headers ? new HttpHeaders(options.headers) : new HttpHeaders();
return this.http.get<T>(url, {
headers,
params: options.params,
});
}
put<T>(endpoint: string, data: any, options: HttpOptions = {}): Observable<ApiResult<T>> { put<T>(endpoint: string, data: any, options: HttpOptions = {}): Observable<ApiResult<T>> {
const url = `${this.baseUrl}/${endpoint}`; const url = `${this.baseUrl}/${endpoint}`;
return this.http return this.http