frontend-smartfarming/agrilink_vocpro/src/app/pages/auth/auth.component.ts

41 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-09-17 08:14:28 +00:00
import { Component } from '@angular/core';
import { Router, RouterModule } from '@angular/router';
import { AuthService } from '../../cores/services/auth.service';
import { FormsModule } from '@angular/forms';
import { LoginData } from '../../cores/interface/auth';
import { ToastrService } from 'ngx-toastr';
2024-09-17 08:14:28 +00:00
@Component({
selector: 'app-auth',
standalone: true,
imports: [FormsModule, RouterModule],
2024-09-17 08:14:28 +00:00
templateUrl: './auth.component.html',
styleUrls: ['./auth.component.scss']
2024-09-17 08:14:28 +00:00
})
export class AuthComponent {
email: string = '';
password: string = '';
rememberMe: boolean = false;
constructor(private authService: AuthService, private router: Router, private toastr: ToastrService) {}
2024-09-17 08:14:28 +00:00
onSubmit() {
const loginData: LoginData = {
email: this.email,
password: this.password,
rememberMe: this.rememberMe
};
this.authService.login(loginData).subscribe(
(response) => {
this.authService.saveTokens(response.data.token);
this.router.navigate(['/dashboard']);
this.toastr.success('Login successful');
},
(error) => {
this.toastr.error(error.error.message);
}
);
}
}