63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
import { Component } from '@angular/core';
|
|
import {DevelopmentStore} from "../../store/DevelopmentStore";
|
|
import {FormsModule} from "@angular/forms";
|
|
import axios from "axios";
|
|
import {NgClass, NgIf} from "@angular/common";
|
|
import {AuthStore} from "../../store/authStore";
|
|
import {firstValueFrom} from "rxjs";
|
|
import {Router} from "@angular/router";
|
|
|
|
@Component({
|
|
selector: 'app-login',
|
|
standalone: true,
|
|
imports: [
|
|
FormsModule,
|
|
NgClass,
|
|
NgIf
|
|
],
|
|
templateUrl: './login.component.html',
|
|
styleUrl: './login.component.scss'
|
|
})
|
|
export class LoginComponent {
|
|
inputUsername: string = "";
|
|
inputPassword: string = "";
|
|
loginFailed: boolean = false;
|
|
loginSuccessful: boolean = false;
|
|
|
|
constructor(private developmentStore: DevelopmentStore, private authStore: AuthStore, private router: Router) {
|
|
}
|
|
|
|
tryToLogin() {
|
|
console.log("Trying to login with username: " + this.inputUsername + " and password: " + this.inputPassword);
|
|
|
|
axios({
|
|
method: 'post',
|
|
url: this.developmentStore.getBaseUrl() + 'api/v1/auth/login',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
data: {
|
|
username: this.inputUsername,
|
|
password: this.inputPassword
|
|
}
|
|
})
|
|
.then(async response => {
|
|
console.log(response);
|
|
console.log(response.data);
|
|
if(response.data.token) {
|
|
this.loginSuccessful = true;
|
|
this.authStore.setToken(response.data.token);
|
|
this.authStore.setUsername(this.inputUsername);
|
|
|
|
//timeout
|
|
setTimeout(() => {
|
|
this.router.navigate(['/secure/administration']);
|
|
}, 500);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
this.loginFailed = true;
|
|
});
|
|
}
|
|
}
|