114 lines
2.9 KiB
TypeScript
114 lines
2.9 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { Component, inject } from '@angular/core';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import { CommonModule } from '@angular/common';
|
|
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
|
|
import { HotToastService } from '@ngxpert/hot-toast';
|
|
import { UserService } from '../user.service';
|
|
import { User } from '../../model/user.interface';
|
|
|
|
@Component({
|
|
selector: 'app-login',
|
|
standalone: true,
|
|
imports: [CommonModule, FormsModule, ReactiveFormsModule],
|
|
templateUrl: './login.component.html',
|
|
styleUrl: '../auth.scss'
|
|
})
|
|
export class LoginComponent {
|
|
private http: HttpClient = inject(HttpClient);
|
|
private route: ActivatedRoute = inject(ActivatedRoute);
|
|
private toast: HotToastService = inject(HotToastService);
|
|
private router: Router = inject(Router);
|
|
private userService: UserService = inject(UserService);
|
|
|
|
redirectUri = null;
|
|
client: string = "";
|
|
client_id = null;
|
|
|
|
isLoading = false;
|
|
|
|
loginForm = new FormGroup({
|
|
username: new FormControl(''),
|
|
password: new FormControl(''),
|
|
})
|
|
|
|
constructor() {
|
|
this.getclient();
|
|
}
|
|
|
|
|
|
|
|
private handleLoginData(data: any) {
|
|
console.log(data)
|
|
if (data["session_key"]) {
|
|
localStorage.setItem('auth_session_key', data.session_key)
|
|
}
|
|
if (data["code"] != null) {
|
|
if (this.redirectUri) {
|
|
location.href = this.redirectUri + "?code=" + data["code"];
|
|
}
|
|
}
|
|
|
|
if (data["id"] != null) {
|
|
this.userService.user = data as User;
|
|
|
|
this.navigateToDashboard();
|
|
}
|
|
}
|
|
|
|
private navigateToDashboard() {
|
|
this.router.navigateByUrl("/dashboard");
|
|
}
|
|
|
|
getclient() {
|
|
const params = (this.route.snapshot.queryParamMap as any)["params"];
|
|
this.redirectUri = params.redirect_uri;
|
|
this.client_id = params.client_id;
|
|
|
|
if (!this.client_id) { return; }
|
|
console.log(params);
|
|
this.http.get<any>('api/client', {
|
|
params
|
|
}).subscribe({
|
|
next: (client) => {
|
|
this.client = client.clientName;
|
|
},
|
|
error: (error) => {
|
|
console.error(error);
|
|
this.toast.error('Invalid client');
|
|
}
|
|
})
|
|
}
|
|
|
|
login() {
|
|
this.isLoading = true;
|
|
const url = this.client_id ? `api/login?client_id=${this.client_id}` : 'api/app/authorize';
|
|
console.log(url, this.client_id)
|
|
this.http.post(url, this.loginForm.value).
|
|
pipe(
|
|
this.toast.observe({
|
|
loading: 'Logging in...',
|
|
success: 'Login successfull',
|
|
error: 'Invalid login'
|
|
})
|
|
)
|
|
.subscribe({
|
|
next: (data) => {
|
|
this.handleLoginData(data);
|
|
},
|
|
error: (error) => {
|
|
console.error(error);
|
|
this.isLoading = false;
|
|
}
|
|
})
|
|
}
|
|
|
|
toRegister() {
|
|
this.router.navigate(['/register'], { queryParams: this.route.snapshot.queryParams });
|
|
}
|
|
|
|
toResetPw() {
|
|
this.router.navigateByUrl('/pw-reset')
|
|
}
|
|
}
|