diff --git a/public/favicon.ico b/public/favicon.ico index 57614f9..a5128e8 100644 Binary files a/public/favicon.ico and b/public/favicon.ico differ diff --git a/src/app/app.html b/src/app/app.html index 2db4341..a5c83b5 100644 --- a/src/app/app.html +++ b/src/app/app.html @@ -1 +1,5 @@ - +@if (isAuthenticated()) { + +} @else { + +} diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index 4cfa961..ae19abb 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -1,16 +1,40 @@ import { Routes } from '@angular/router'; import { HomePage } from './pages/home/home.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 = [ + { + path: 'login', + title: 'ورود', + component: LoginPage, + // canActivate: [redirectAuthenticated], + }, { path: '', title: 'دستیار هوشمند', component: HomePage, + // canActivate: [authGuard], }, { path: 'settings', title: 'تنظیمات', component: SettingsPage, + // canActivate: [authGuard], }, ]; diff --git a/src/app/app.ts b/src/app/app.ts index e1512fb..f7c86c1 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -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 { LoginPage } from './pages/login/login.page'; +import { AuthService } from './services/auth/auth.service'; @Component({ selector: 'app-root', - imports: [LayoutComponent], + standalone: true, + imports: [CommonModule, LayoutComponent, LoginPage], templateUrl: './app.html', - styleUrl: './app.css' + styleUrl: './app.css', }) -export class App {} +export class App { + private readonly authService = inject(AuthService); + + readonly isAuthenticated = signal(this.authService.isAuthenticated()); + + constructor() { + this.authService.currentUserSubject.subscribe({ + next: () => { + this.isAuthenticated.set(this.authService.isAuthenticated()); + }, + }); + } +} diff --git a/src/app/pages/login/login.page.css b/src/app/pages/login/login.page.css new file mode 100644 index 0000000..9588c00 --- /dev/null +++ b/src/app/pages/login/login.page.css @@ -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; + } +} diff --git a/src/app/pages/login/login.page.html b/src/app/pages/login/login.page.html new file mode 100644 index 0000000..e7baa5f --- /dev/null +++ b/src/app/pages/login/login.page.html @@ -0,0 +1,225 @@ +
+ + + +
diff --git a/src/app/pages/login/login.page.ts b/src/app/pages/login/login.page.ts new file mode 100644 index 0000000..8813bf3 --- /dev/null +++ b/src/app/pages/login/login.page.ts @@ -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>; + +@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({}); + readonly generalError = signal(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 }); + } +} diff --git a/src/app/services/auth/auth.service.ts b/src/app/services/auth/auth.service.ts index 4c75444..6df61c2 100644 --- a/src/app/services/auth/auth.service.ts +++ b/src/app/services/auth/auth.service.ts @@ -1,65 +1,112 @@ 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 { HttpService } from '../http-request/http.service'; -interface ApiResult { - hasError: boolean; - messages: string[]; - result: T; -} - -interface LoginResponse { - auth: boolean; - token: string; +export interface AuthUser { + id: string; username: string; + role: string; + organization_id: string; } -interface LoginResult extends ApiResult { - licenseValid?: boolean; +export interface LoginResponse { + user: AuthUser; + csrf_token: string; + expires_at: string; +} + +export interface CurrentUserResponse { + user: AuthUser; + csrf_token: string; + expires_at: string; } @Injectable({ providedIn: 'root', }) export class AuthService { - public currentUserSubject = new BehaviorSubject( - localStorage.getItem('username'), - ); + public currentUserSubject = new BehaviorSubject(null); + private csrfToken: string | null = null; + constructor( private httpService: HttpService, ) { - let previousUser: string | null = this.currentUserSubject.value; - this.currentUserSubject.subscribe((username) => { - if (username !== previousUser) { - previousUser = username; + const savedUser = localStorage.getItem('current_user'); + const savedCsrf = localStorage.getItem('csrf_token'); + if (savedUser) { + try { + this.currentUserSubject.next(JSON.parse(savedUser)); + } catch { + // ignore corrupt data } - }); + } + if (savedCsrf) { + this.csrfToken = savedCsrf; + } } login(credentials: { username: string; password: string; - rememberMe: boolean; - }): Observable { - return this.httpService.post('users/login', credentials).pipe( - tap((response: ApiResult) => { - if (!response.hasError && response.result?.auth) { - localStorage.setItem('access_token', response.result.token); - localStorage.setItem('username', response.result.username); - this.currentUserSubject.next(response.result.username); - } + }): Observable { + return this.httpService.postRaw('v1/auth/login', credentials).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); + }), + catchError((err) => { + this.clearAuth(); + return throwError(() => err); }), ); } - logout(): void { - localStorage.removeItem('access_token'); - localStorage.removeItem('username'); - this.currentUserSubject.next(null); + logout(): Observable { + const headers: Record = {}; + if (this.csrfToken) { + headers['X-CSRF-Token'] = this.csrfToken; + } + return this.httpService.postRaw('v1/auth/logout', {}, { headers }).pipe( + tap(() => { + this.clearAuth(); + }), + catchError((err) => { + this.clearAuth(); + return throwError(() => err); + }), + ); + } + + getCurrentUser(): Observable { + return this.httpService.getRaw('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 { - 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'); } } diff --git a/src/app/services/http-request/http.service.ts b/src/app/services/http-request/http.service.ts index d40cdfa..01c7bba 100644 --- a/src/app/services/http-request/http.service.ts +++ b/src/app/services/http-request/http.service.ts @@ -94,6 +94,13 @@ export class HttpService { 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) { try { const apiError = error.error as ApiResult; @@ -254,6 +261,24 @@ export class HttpService { ); } + postRaw(endpoint: string, data: any, options: HttpOptions = {}): Observable { + const url = `${this.baseUrl}/${endpoint}`; + const headers = options.headers ? new HttpHeaders(options.headers) : new HttpHeaders(); + return this.http.post(url, data, { + headers, + params: options.params, + }); + } + + getRaw(endpoint: string, options: HttpOptions = {}): Observable { + const url = `${this.baseUrl}/${endpoint}`; + const headers = options.headers ? new HttpHeaders(options.headers) : new HttpHeaders(); + return this.http.get(url, { + headers, + params: options.params, + }); + } + put(endpoint: string, data: any, options: HttpOptions = {}): Observable> { const url = `${this.baseUrl}/${endpoint}`; return this.http