Enhance login form with keep signed in functionality and disable button during submission

This commit is contained in:
Christian Werner 2025-10-26 02:33:55 +01:00
parent 55ca5f572f
commit 49853d99c6
3 changed files with 33 additions and 3 deletions

View File

@ -20,12 +20,12 @@
</div> </div>
<div class="form-field full-width"> <div class="form-field full-width">
<label for="checkbox">Keep me signed in</label> <label for="checkbox">Keep me signed in</label>
<input type="checkbox" id="checkbox" name="checkbox" /> <input type="checkbox" id="checkbox" name="checkbox" formControlName="keepSignedIn" />
</div> </div>
</div> </div>
</div> </div>
<div class="actions"> <div class="actions">
<button type="submit" class="primary">Login</button> <button type="submit" class="primary" [disabled]="submitting()">Login</button>
</div> </div>
</form> </form>

View File

@ -2,6 +2,7 @@ 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'; import {Router} from '@angular/router';
import {takeUntilDestroyed, toObservable} from '@angular/core/rxjs-interop';
@Component({ @Component({
selector: 'app-login', selector: 'app-login',
@ -21,13 +22,31 @@ export class LoginComponent {
protected loginFormGroup = new FormGroup({ protected loginFormGroup = new FormGroup({
usernameOrEmail: new FormControl('housemaster'), usernameOrEmail: new FormControl('housemaster'),
password: new FormControl('kR0pNCspBKx8lOzAIch5'), password: new FormControl('kR0pNCspBKx8lOzAIch5'),
keep: new FormControl(false), keepSignedIn: new FormControl(false),
}); });
protected errors = signal<string[]>([]); protected errors = signal<string[]>([]);
protected submitting = signal<boolean>(true);
constructor() {
toObservable(this.submitting)
.pipe(
takeUntilDestroyed(),
)
.subscribe(x => {
if (x) {
this.loginFormGroup.disable();
} else {
this.loginFormGroup.enable();
}
})
}
protected async onSubmit($event: Event): Promise<void> { protected async onSubmit($event: Event): Promise<void> {
$event.preventDefault(); $event.preventDefault();
this.submitting.set(true)
this.errors.set([]); this.errors.set([]);
@ -37,6 +56,7 @@ export class LoginComponent {
await this.auth.loginAsync(this.loginFormGroup.value as unknown as ValidateCredentialsCommand, error => { await this.auth.loginAsync(this.loginFormGroup.value as unknown as ValidateCredentialsCommand, error => {
this.errors.set([...this.errors(), error.error.trim()]); this.errors.set([...this.errors(), error.error.trim()]);
this.submitting.set(false)
}); });
console.log('login successful'); console.log('login successful');

View File

@ -43,6 +43,16 @@ form {
padding: 0.5em; padding: 0.5em;
border-radius: 0.5em; border-radius: 0.5em;
background-color: var(--neutral-80); background-color: var(--neutral-80);
&:disabled {
color: var(--neutral-60);
}
}
&:has(input[disabled]) {
label {
color: var(--neutral-50);
}
} }
&:has(input[required]:not([type="radio"])) { &:has(input[required]:not([type="radio"])) {