app login

This commit is contained in:
Bastian Wagner
2024-09-05 11:46:15 +02:00
parent afeea2f0f2
commit 767e3cbb41
19 changed files with 315 additions and 67 deletions

View File

@@ -4,6 +4,8 @@ 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',
@@ -17,6 +19,7 @@ export class LoginComponent {
private route: ActivatedRoute = inject(ActivatedRoute);
private toast: HotToastService = inject(HotToastService);
private router: Router = inject(Router);
private userService: UserService = inject(UserService);
redirectUri = null;
client: string = "";
@@ -38,7 +41,9 @@ export class LoginComponent {
const id = window.localStorage.getItem("auth_session_key");
if (!id ||id.length < 2) { return; }
this.http.post('api/auth/login-with-session-id', {
const url = this.client_id ? 'api/auth/login-with-session-id' : 'api/auth/login-with-session-id/userlogin'
this.http.post(url, {
code: id,
client_id: this.client_id
}).pipe(
@@ -49,17 +54,7 @@ export class LoginComponent {
})
).subscribe({
next: (data) => {
if (data["code"] != null) {
if (this.redirectUri) {
location.href = this.redirectUri + "?code=" + data["code"];
} else {
if (data['user'] && data['user']['access']) {
sessionStorage.setItem('access', data['user']['access']);
this.router.navigate(['dashboard'])
}
}
}
this.handleLoginData(data);
},
error: (error) => {
console.error(error);
@@ -67,12 +62,28 @@ export class LoginComponent {
});
}
private handleLoginData(data: any) {
if (data["code"] != null) {
if (this.redirectUri) {
location.href = this.redirectUri + "?code=" + data["code"];
}
} else 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; }
this.http.get<any>('api/auth/', {
params
}).subscribe({
@@ -88,7 +99,9 @@ export class LoginComponent {
login() {
this.isLoading = true;
this.http.post('api/auth/login?'+ 'client_id=' + this.client_id, this.loginForm.value).
const url = this.client_id ? `api/auth/login?client_id=${this.client_id}` : 'api/app/login';
console.log(url, this.client_id)
this.http.post(url, this.loginForm.value).
pipe(
this.toast.observe({
loading: 'Logging in...',
@@ -98,17 +111,7 @@ export class LoginComponent {
)
.subscribe({
next: (data) => {
if (data["code"] != null) {
window.localStorage.setItem("auth_session_key", data["session_key"]);
if (this.redirectUri) {
location.href = this.redirectUri + "?code=" + data["code"];
} else {
if (data['user'] && data['user']['access']) {
sessionStorage.setItem('access', data['user']['access']);
this.router.navigate(['dashboard'])
}
}
}
this.handleLoginData(data);
},
error: (error) => {
console.error(error);