feat(login): update login form with material design elements

This commit is contained in:
Jan Gleytenhoover 2025-01-21 14:10:39 +01:00
parent 121eb07203
commit b218697867
Signed by: jank
GPG key ID: 50620ADD22CD330B
2 changed files with 64 additions and 78 deletions

View file

@ -1,75 +1,21 @@
<div class="flex min-h-full flex-col justify-center px-6 py-12 lg:px-8"> <div class="mx-auto pt-3 container">
<div class="sm:mx-auto sm:w-full sm:max-w-sm"> <mat-card appearance="outlined">
<img <mat-card-content>
class="mx-auto h-10 w-auto" <h1>Login</h1>
src="https://tailwindui.com/plus/img/logos/mark.svg?color=indigo&shade=600"
alt="Your Company"
/>
<h2
class="mt-10 text-center text-2xl/9 font-bold tracking-tight text-gray-900"
>
Sign in to your account
</h2>
</div>
<div class="mt-10 sm:mx-auto sm:w-full sm:max-w-sm"> <form class="flex flex-col" [formGroup]="loginForm">
<form [formGroup]="loginForm" class="space-y-6" action="#" method="POST"> <mat-form-field appearance="outline">
@if (invalidCredentials) { <mat-error>{{ errorMessages["email"] }}</mat-error>
<div class="mt-2"> <mat-label>Email</mat-label>
<p <input formControlName="email" type="email" matInput placeholder="banana@banana.com" />
class="block w-full rounded-md bg-white px-3 py-1.5 text-base text-red-500 outline outline-red-500 outline-2 -outline-offset-2 sm:text-sm/6" </mat-form-field>
> <mat-form-field appearance="outline">
Invalid Credentials <mat-error>{{ errorMessages["password"] }}</mat-error>
</p> <mat-label>Password</mat-label>
</div> <input formControlName="password" matInput type="password" placeholder="Aurelius14" />
} </mat-form-field>
<div> <button mat-flat-button type="submit" (click)="submit()">Login</button>
<label for="email" class="block text-sm/6 font-medium text-gray-900"
>Email address</label
>
<div class="mt-2">
<input
formControlName="email"
type="email"
name="email"
id="email"
autocomplete="email"
required
class="block w-full rounded-md bg-white px-3 py-1.5 text-base text-gray-900 outline outline-1 -outline-offset-1 outline-gray-300 placeholder:text-gray-400 focus:outline focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600 sm:text-sm/6"
/>
</div>
</div>
<div>
<div class="flex items-center justify-between">
<label
for="password"
class="block text-sm/6 font-medium text-gray-900"
>Password</label
>
</div>
<div class="mt-2">
<input
formControlName="password"
type="password"
name="password"
id="password"
autocomplete="current-password"
required
class="block w-full rounded-md bg-white px-3 py-1.5 text-base text-gray-900 outline outline-1 -outline-offset-1 outline-gray-300 placeholder:text-gray-400 focus:outline focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600 sm:text-sm/6"
/>
</div>
</div>
<div>
<button
(click)="submit()"
type="submit"
class="flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm/6 font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
Sign in
</button>
</div>
</form> </form>
</div> </mat-card-content>
</mat-card>
</div> </div>

View file

@ -1,12 +1,17 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import PocketBase from 'pocketbase'; import PocketBase from 'pocketbase';
import { environment } from '../../environments/environment'; import { environment } from '../../environments/environment';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { MatCardModule } from '@angular/material/card';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule, MatLabel } from '@angular/material/input';
import { MatButton, MatButtonModule } from '@angular/material/button';
import { MatSnackBar } from '@angular/material/snack-bar';
@Component({ @Component({
selector: 'app-login', selector: 'app-login',
imports: [ReactiveFormsModule], imports: [ReactiveFormsModule, MatCardModule, MatInputModule, MatLabel, MatFormFieldModule, MatButtonModule],
templateUrl: './login.component.html', templateUrl: './login.component.html',
styleUrl: './login.component.css', styleUrl: './login.component.css',
}) })
@ -14,21 +19,52 @@ export class LoginComponent {
public loginForm!: FormGroup; public loginForm!: FormGroup;
public invalidCredentials = false; public invalidCredentials = false;
private pb = new PocketBase(environment.POCKETBASE); private pb = new PocketBase(environment.POCKETBASE);
public errorMessages: Record<string, string> = {};
constructor(private router: Router) {} constructor(private router: Router, private snackBar: MatSnackBar) { }
private validationErrorMessages: Record<string, string> = {
required: 'This field is required',
};
updateErrorMessages(): void {
this.errorMessages = {};
Object.keys(this.loginForm.controls).forEach((field) => {
const control = this.loginForm.get(field);
if (control && control.errors) {
this.errorMessages[field] = Object.keys(control.errors)
.map(
(errorKey) =>
this.validationErrorMessages[errorKey] ||
`Unknown error: ${errorKey}`,
)
.join(' ');
}
});
}
ngOnInit(): void { ngOnInit(): void {
this.loginForm = new FormGroup({ this.loginForm = new FormGroup({
email: new FormControl(''), email: new FormControl('', Validators.required),
password: new FormControl(''), password: new FormControl('', Validators.required),
}); });
if (this.pb.authStore.isValid) { if (this.pb.authStore.isValid) {
this.router.navigate(['dashboard']); this.router.navigate(['dashboard']);
} }
this.loginForm.valueChanges.subscribe(() => {
this.updateErrorMessages();
});
} }
submit() { submit() {
if (!this.loginForm.valid) {
this.updateErrorMessages();
}
this.pb this.pb
.collection('users') .collection('users')
.authWithPassword( .authWithPassword(
@ -40,6 +76,10 @@ export class LoginComponent {
}) })
.catch(() => { .catch(() => {
this.invalidCredentials = true; this.invalidCredentials = true;
const error = this.snackBar.open('Invalid Credentials');
setTimeout(() => {
error.dismiss();
}, 5000);
}); });
} }
} }