Files
kontor/kontor-angular/src/app/common/auth/auth.ts
T
2026-01-29 23:50:41 +01:00

56 lines
1.4 KiB
TypeScript

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { NgForm } from '@angular/forms';
import { AuthService, TokenData } from './auth-service';
import { LoadingSpinner } from "../../shared/loading-spinner/loading-spinner";
import { Observable } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';
@Component({
selector: 'app-auth',
imports: [FormsModule, LoadingSpinner],
templateUrl: './auth.html',
styleUrl: './auth.css'
})
export class Auth {
isLoginMode = true;
isLoading = false;
error: string | null = null;
constructor(private authService: AuthService) {}
onSwitchMode() {
this.isLoginMode =!this.isLoginMode;
}
onSubmit(form: NgForm) {
if (!form.valid) {
return;
}
const email = form.value.email;
const password = form.value.password;
let authObservable: Observable<TokenData>;
this.isLoading = true;
if (this.isLoginMode) {
authObservable = this.authService.login(email, password);
} else {
authObservable = this.authService.signup(email, password)
}
authObservable.subscribe({
next: token => {
console.log(token);
localStorage.setItem('userToken', JSON.stringify(token));
this.isLoading = false;
},
error: err => {
console.log(err);
this.isLoading = false;
}
});
form.reset();
}
}