basic auth guard
This commit is contained in:
parent
a3a9e57dea
commit
723c5f55b8
@ -1,13 +1,27 @@
|
|||||||
import { Routes } from '@angular/router';
|
import {Routes} from '@angular/router';
|
||||||
import {LoginComponent} from './components/login/login.component';
|
import {LoginComponent} from './components/login/login.component';
|
||||||
|
import {HomeComponent} from './components/home/home.component';
|
||||||
|
import {authGuard} from './guards/auth.guard';
|
||||||
|
|
||||||
export const routes: Routes = [
|
export const routes: Routes = [
|
||||||
{
|
{
|
||||||
|
title: 'Login',
|
||||||
path: 'login',
|
path: 'login',
|
||||||
component: LoginComponent,
|
component: LoginComponent,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: '',
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
title: 'Home',
|
||||||
|
path: '',
|
||||||
|
component: HomeComponent,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
canActivate: [authGuard],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
path: '**',
|
path: '**',
|
||||||
redirectTo: 'login',
|
redirectTo: '',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
@ -15,16 +15,20 @@ export class AuthService extends GandalfClient {
|
|||||||
|
|
||||||
private base = '/api/auth';
|
private base = '/api/auth';
|
||||||
|
|
||||||
check(): Observable<boolean> {
|
check$(onError?: (error: any) => void): Observable<boolean> {
|
||||||
return this.handleRequest(this.http.get<boolean>(this.base + '/check'));
|
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);
|
return this.handleRequest(this.http.post<void>(this.base + '/login', command), onError);
|
||||||
}
|
}
|
||||||
|
|
||||||
loginAsync(command: ValidateCredentialsCommand, onError?: (error: any) => void): Promise<void> {
|
loginAsync(command: ValidateCredentialsCommand, onError?: (error: any) => void): Promise<void> {
|
||||||
return lastValueFrom(this.login(command, onError));
|
return lastValueFrom(this.login$(command, onError));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@ -1,6 +1,7 @@
|
|||||||
import {Component, inject, signal} from '@angular/core';
|
import {Component, inject, signal} from '@angular/core';
|
||||||
import {FormControl, FormGroup, FormsModule, FormSubmittedEvent, ReactiveFormsModule} from '@angular/forms';
|
import {FormControl, FormGroup, FormsModule, FormSubmittedEvent, ReactiveFormsModule} from '@angular/forms';
|
||||||
import {AuthService, ValidateCredentialsCommand} from '../../clients/gandalf/mithrandir/auth.service';
|
import {AuthService, ValidateCredentialsCommand} from '../../clients/gandalf/mithrandir/auth.service';
|
||||||
|
import {Router} from '@angular/router';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-login',
|
selector: 'app-login',
|
||||||
@ -15,6 +16,7 @@ import {AuthService, ValidateCredentialsCommand} from '../../clients/gandalf/mit
|
|||||||
export class LoginComponent {
|
export class LoginComponent {
|
||||||
|
|
||||||
private auth = inject(AuthService);
|
private auth = inject(AuthService);
|
||||||
|
private router = inject(Router);
|
||||||
|
|
||||||
protected loginFormGroup = new FormGroup({
|
protected loginFormGroup = new FormGroup({
|
||||||
usernameOrEmail: new FormControl('housemasterr'),
|
usernameOrEmail: new FormControl('housemasterr'),
|
||||||
@ -38,5 +40,6 @@ export class LoginComponent {
|
|||||||
});
|
});
|
||||||
|
|
||||||
console.log('login successful');
|
console.log('login successful');
|
||||||
|
await this.router.navigate(['/']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,9 +21,10 @@ public class AuthController : ControllerBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("[action]")]
|
[HttpGet("[action]")]
|
||||||
public async Task<IActionResult> Check()
|
public IActionResult Check()
|
||||||
{
|
{
|
||||||
return Ok(true);
|
var sessionExists = Request.Cookies.ContainsKey("MithrandirSession");
|
||||||
|
return Ok(sessionExists);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("[action]")]
|
[HttpPost("[action]")]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user