jklink/src/app/login/login.component.ts

45 lines
1.1 KiB
TypeScript

import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import PocketBase from 'pocketbase';
import { environment } from '../../environments/environment';
import { Router } from '@angular/router';
@Component({
selector: 'app-login',
imports: [ReactiveFormsModule],
templateUrl: './login.component.html',
styleUrl: './login.component.css',
})
export class LoginComponent {
public loginForm!: FormGroup;
public invalidCredentials = false;
private pb = new PocketBase(environment.POCKETBASE);
constructor(private router: Router) {}
ngOnInit(): void {
this.loginForm = new FormGroup({
email: new FormControl(''),
password: new FormControl(''),
});
if (this.pb.authStore.isValid) {
this.router.navigate(['dashboard']);
}
}
submit() {
this.pb
.collection('users')
.authWithPassword(
this.loginForm.get('email')?.value,
this.loginForm.get('password')?.value,
)
.then(() => {
this.router.navigate(['dashboard']);
})
.catch(() => {
this.invalidCredentials = true;
});
}
}