feat: add dashboard component and routing logic

This commit is contained in:
Jan Gleytenhoover 2025-01-19 20:46:37 +01:00
parent 89083f832f
commit 6bec12b45a
Signed by: jank
GPG key ID: 50620ADD22CD330B
7 changed files with 80 additions and 7 deletions

View file

@ -1,13 +1,20 @@
import { Routes } from '@angular/router'; import { Routes } from '@angular/router';
import { LoginComponent } from './login/login.component'; import { LoginComponent } from './login/login.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { AuthGuard } from './service/auth.service';
export const routes: Routes = [ export const routes: Routes = [
{ {
path: 'login', path: '',
component: LoginComponent, component: LoginComponent,
}, },
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [AuthGuard],
},
{ {
path: "**", path: "**",
redirectTo: "login", redirectTo: "",
}, },
]; ];

View file

@ -0,0 +1 @@
<p>dashboard works!</p>

View file

@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { DashboardComponent } from './dashboard.component';
describe('DashboardComponent', () => {
let component: DashboardComponent;
let fixture: ComponentFixture<DashboardComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [DashboardComponent]
})
.compileComponents();
fixture = TestBed.createComponent(DashboardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View file

@ -0,0 +1,11 @@
import { Component } from '@angular/core';
@Component({
selector: 'app-dashboard',
imports: [],
templateUrl: './dashboard.component.html',
styleUrl: './dashboard.component.css'
})
export class DashboardComponent {
}

View file

@ -13,6 +13,7 @@ import { Router } from '@angular/router';
export class LoginComponent { export class LoginComponent {
public loginForm!: FormGroup; public loginForm!: FormGroup;
public invalidCredentials = false; public invalidCredentials = false;
private pb = new PocketBase(environment.POCKETBASE);
constructor(private router: Router) { }; constructor(private router: Router) { };
@ -21,16 +22,18 @@ export class LoginComponent {
email: new FormControl(''), email: new FormControl(''),
password: new FormControl(''), password: new FormControl(''),
}); });
if (this.pb.authStore.isValid) {
this.router.navigate(['dashboard']);
}
} }
submit() { submit() {
const pb = new PocketBase(environment.POCKETBASE); this.pb.collection("users").authWithPassword(
pb.collection("users").authWithPassword(
this.loginForm.get('email')?.value, this.loginForm.get('email')?.value,
this.loginForm.get('password')?.value this.loginForm.get('password')?.value
).then(response => { ).then(() => {
this.invalidCredentials = false; this.router.navigate(['dashboard']);
}).catch(() => { }).catch(() => {
this.invalidCredentials = true; this.invalidCredentials = true;
}); });

View file

@ -0,0 +1,28 @@
import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivate,
GuardResult,
MaybeAsync,
Router,
RouterStateSnapshot
} from '@angular/router';
import PocketBase from 'pocketbase';
import { environment } from '../../environments/environment.development';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private router: Router) { };
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
const pb = new PocketBase(environment.POCKETBASE);
if (pb.authStore.isValid) {
return true;
}
this.router.navigate(['']);
return false;
}
}