import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, RouterLink } from '@angular/router';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
import { MatCardModule } from '@angular/material/card';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatButtonModule } from '@angular/material/button';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { AuthService } from '../../core/services/auth.service';
import { NgIf } from '@angular/common';
@Component({
selector: 'app-login',
standalone: true,
imports: [
RouterLink, ReactiveFormsModule, NgIf,
MatCardModule, MatFormFieldModule, MatInputModule, MatButtonModule,
MatProgressSpinnerModule,
],
template: `
Indie
Sign in to your account
Your account is ready. Please sign in.
Don't have an account?
Register
`,
})
export class LoginComponent implements OnInit {
loginForm = this.fb.group({
email: ['', [Validators.required, Validators.email]],
password: ['', Validators.required],
});
loading = false;
error = '';
showResendLink = false;
resendMessage = '';
showVerifiedMessage = false;
private unverifiedEmail = '';
constructor(
private fb: FormBuilder,
private authService: AuthService,
private router: Router,
private route: ActivatedRoute,
) {}
ngOnInit(): void {
if (this.route.snapshot.queryParams['verified'] === 'true') {
this.showVerifiedMessage = true;
}
}
onSubmit(): void {
if (this.loginForm.invalid) return;
this.loading = true;
this.error = '';
this.showResendLink = false;
this.resendMessage = '';
const { email, password } = this.loginForm.value;
this.authService.login(email!, password!).subscribe({
next: () => this.router.navigate(['/dashboard']),
error: (err) => {
const msg = err.error?.detail || err.error?.message || 'Invalid email or password';
if (msg.toLowerCase().includes('verify your email')) {
this.error = 'Please verify your email address before logging in.';
this.showResendLink = true;
this.unverifiedEmail = email!;
} else {
this.error = msg;
}
this.loading = false;
},
});
}
resendVerification(): void {
if (!this.unverifiedEmail) return;
this.authService.resendVerification(this.unverifiedEmail).subscribe({
next: () => {
this.resendMessage = 'Verification email resent.';
},
error: () => {
this.resendMessage = 'Failed to resend. Please try again later.';
},
});
}
}