main #12
3 changed files with 102 additions and 69 deletions
|
@ -1,75 +1,35 @@
|
|||
<div class="flex min-h-full flex-col justify-center px-6 py-12 lg:px-8">
|
||||
<div class="sm:mx-auto sm:w-full sm:max-w-sm">
|
||||
<img
|
||||
class="mx-auto h-10 w-auto"
|
||||
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="mx-auto pt-3 container">
|
||||
<mat-card appearance="outlined">
|
||||
<mat-card-content>
|
||||
<h1>Login</h1>
|
||||
|
||||
<div class="mt-10 sm:mx-auto sm:w-full sm:max-w-sm">
|
||||
<form [formGroup]="loginForm" class="space-y-6" action="#" method="POST">
|
||||
@if (invalidCredentials) {
|
||||
<div class="mt-2">
|
||||
<p
|
||||
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"
|
||||
>
|
||||
Invalid Credentials
|
||||
</p>
|
||||
</div>
|
||||
}
|
||||
<div>
|
||||
<label for="email" class="block text-sm/6 font-medium text-gray-900"
|
||||
>Email address</label
|
||||
>
|
||||
<div class="mt-2">
|
||||
<form class="flex flex-col" [formGroup]="loginForm">
|
||||
<mat-form-field appearance="outline">
|
||||
<mat-error>{{ errorMessages["email"] }}</mat-error>
|
||||
<mat-label>Email</mat-label>
|
||||
<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"
|
||||
matInput
|
||||
placeholder="banana@banana.com"
|
||||
/>
|
||||
</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">
|
||||
</mat-form-field>
|
||||
<mat-form-field appearance="outline">
|
||||
<mat-error>{{ errorMessages["password"] }}</mat-error>
|
||||
<mat-label>Password</mat-label>
|
||||
<input
|
||||
formControlName="password"
|
||||
matInput
|
||||
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"
|
||||
placeholder="Aurelius14"
|
||||
/>
|
||||
</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
|
||||
</mat-form-field>
|
||||
<button class="mb-3" mat-flat-button type="submit" (click)="submit()">Login</button>
|
||||
<mat-divider></mat-divider>
|
||||
<button mat-flat-button [style.backgroundColor]="'#FD4B2D'" class="mt-3 mat-authentik" (click)="loginWithAuthentik()">
|
||||
Log in with Authentik
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</form>
|
||||
</mat-card-content>
|
||||
</mat-card>
|
||||
</div>
|
||||
|
|
|
@ -1,12 +1,31 @@
|
|||
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 { environment } from '../../environments/environment';
|
||||
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';
|
||||
import {MatDividerModule} from '@angular/material/divider';
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
imports: [ReactiveFormsModule],
|
||||
imports: [
|
||||
ReactiveFormsModule,
|
||||
MatCardModule,
|
||||
MatInputModule,
|
||||
MatLabel,
|
||||
MatFormFieldModule,
|
||||
MatButtonModule,
|
||||
MatDividerModule,
|
||||
],
|
||||
templateUrl: './login.component.html',
|
||||
styleUrl: './login.component.css',
|
||||
})
|
||||
|
@ -14,21 +33,69 @@ export class LoginComponent {
|
|||
public loginForm!: FormGroup;
|
||||
public invalidCredentials = false;
|
||||
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 {
|
||||
this.loginForm = new FormGroup({
|
||||
email: new FormControl(''),
|
||||
password: new FormControl(''),
|
||||
email: new FormControl('', Validators.required),
|
||||
password: new FormControl('', Validators.required),
|
||||
});
|
||||
|
||||
if (this.pb.authStore.isValid) {
|
||||
this.router.navigate(['dashboard']);
|
||||
}
|
||||
|
||||
this.loginForm.valueChanges.subscribe(() => {
|
||||
this.updateErrorMessages();
|
||||
});
|
||||
}
|
||||
|
||||
loginWithAuthentik() {
|
||||
this.pb.collection('users').authWithOAuth2({ provider: 'oidc' })
|
||||
.then(() => {
|
||||
this.router.navigate(['dashboard']);
|
||||
})
|
||||
.catch(() => {
|
||||
this.invalidCredentials = true;
|
||||
const error = this.snackBar.open('Invalid Credentials');
|
||||
setTimeout(() => {
|
||||
error.dismiss();
|
||||
}, 5000);
|
||||
});
|
||||
}
|
||||
|
||||
submit() {
|
||||
if (!this.loginForm.valid) {
|
||||
this.updateErrorMessages();
|
||||
}
|
||||
|
||||
this.pb
|
||||
.collection('users')
|
||||
.authWithPassword(
|
||||
|
@ -40,6 +107,10 @@ export class LoginComponent {
|
|||
})
|
||||
.catch(() => {
|
||||
this.invalidCredentials = true;
|
||||
const error = this.snackBar.open('Invalid Credentials');
|
||||
setTimeout(() => {
|
||||
error.dismiss();
|
||||
}, 5000);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@ html {
|
|||
@apply underline text-blue-500 cursor-pointer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
html,
|
||||
body {
|
||||
height: 100%;
|
||||
|
|
Loading…
Add table
Reference in a new issue