basic auth guard

This commit is contained in:
Christian Werner 2025-07-14 23:55:01 +02:00
parent a3a9e57dea
commit 723c5f55b8
5 changed files with 30 additions and 31 deletions

View File

@ -1,13 +1,27 @@
import {Routes} from '@angular/router';
import {LoginComponent} from './components/login/login.component';
import {HomeComponent} from './components/home/home.component';
import {authGuard} from './guards/auth.guard';
export const routes: Routes = [
{
title: 'Login',
path: 'login',
component: LoginComponent,
},
{
path: '',
children: [
{
title: 'Home',
path: '',
component: HomeComponent,
},
],
canActivate: [authGuard],
},
{
path: '**',
redirectTo: 'login',
redirectTo: '',
},
];

View File

@ -15,16 +15,20 @@ export class AuthService extends GandalfClient {
private base = '/api/auth';
check(): Observable<boolean> {
return this.handleRequest(this.http.get<boolean>(this.base + '/check'));
check$(onError?: (error: any) => void): Observable<boolean> {
return this.handleRequest(this.http.get<boolean>(this.base + '/check'), onError);
}
login(command: ValidateCredentialsCommand, onError?: (error: any) => void): Observable<void> {
checkAsync(onError?: (error: any) => void): Promise<boolean> {
return lastValueFrom(this.check$(onError));
}
login$(command: ValidateCredentialsCommand, onError?: (error: any) => void): Observable<void> {
return this.handleRequest(this.http.post<void>(this.base + '/login', command), onError);
}
loginAsync(command: ValidateCredentialsCommand, onError?: (error: any) => void): Promise<void> {
return lastValueFrom(this.login(command, onError));
return lastValueFrom(this.login$(command, onError));
}
}

View File

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

View File

@ -1,6 +1,7 @@
import {Component, inject, signal} from '@angular/core';
import {FormControl, FormGroup, FormsModule, FormSubmittedEvent, ReactiveFormsModule} from '@angular/forms';
import {AuthService, ValidateCredentialsCommand} from '../../clients/gandalf/mithrandir/auth.service';
import {Router} from '@angular/router';
@Component({
selector: 'app-login',
@ -15,6 +16,7 @@ import {AuthService, ValidateCredentialsCommand} from '../../clients/gandalf/mit
export class LoginComponent {
private auth = inject(AuthService);
private router = inject(Router);
protected loginFormGroup = new FormGroup({
usernameOrEmail: new FormControl('housemasterr'),
@ -38,5 +40,6 @@ export class LoginComponent {
});
console.log('login successful');
await this.router.navigate(['/']);
}
}

View File

@ -21,9 +21,10 @@ public class AuthController : ControllerBase
}
[HttpGet("[action]")]
public async Task<IActionResult> Check()
public IActionResult Check()
{
return Ok(true);
var sessionExists = Request.Cookies.ContainsKey("MithrandirSession");
return Ok(sessionExists);
}
[HttpPost("[action]")]