app login
This commit is contained in:
@@ -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);
|
||||
|
||||
16
idp_client/src/app/auth/user.service.spec.ts
Normal file
16
idp_client/src/app/auth/user.service.spec.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { UserService } from './user.service';
|
||||
|
||||
describe('UserService', () => {
|
||||
let service: UserService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(UserService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
21
idp_client/src/app/auth/user.service.ts
Normal file
21
idp_client/src/app/auth/user.service.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { User } from '../model/user.interface';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserService {
|
||||
|
||||
private _user: User;
|
||||
|
||||
constructor() { }
|
||||
|
||||
|
||||
set user(user: User) {
|
||||
this._user = user;
|
||||
}
|
||||
|
||||
get user(): User {
|
||||
return this._user;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Component, inject, OnInit } from '@angular/core';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { User } from '../model/user.interface';
|
||||
import { UserService } from '../auth/user.service';
|
||||
import { Router } from '@angular/router';
|
||||
|
||||
@Component({
|
||||
selector: 'app-dashboard',
|
||||
@@ -11,42 +10,17 @@ import { User } from '../model/user.interface';
|
||||
styleUrl: './dashboard.component.scss'
|
||||
})
|
||||
export class DashboardComponent implements OnInit {
|
||||
private router: Router = inject(Router);
|
||||
private http: HttpClient = inject(HttpClient);
|
||||
private user: User;
|
||||
|
||||
accessToken: string;
|
||||
private userService: UserService = inject(UserService);
|
||||
private router: Router = inject(Router);
|
||||
|
||||
|
||||
ngOnInit(): void {
|
||||
|
||||
const refresh = sessionStorage.getItem('access');
|
||||
if (!refresh || refresh.length == 0) {
|
||||
this.router.navigate(['login']);
|
||||
} else {
|
||||
this.accessToken = refresh;
|
||||
this.refreshToken(refresh);
|
||||
this.getClients();
|
||||
if (!this.userService.user) {
|
||||
this.router.navigateByUrl("/login");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
refreshToken(token: string) {
|
||||
this.http.post('api/auth/verify', { access_token: token}).subscribe({
|
||||
next: res => {
|
||||
console.log(res)
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
getClients() {
|
||||
this.http.get('api/client', {headers: { auth: 'Bearer ' + this.accessToken}}).subscribe({
|
||||
next: result => {
|
||||
console.log(result);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,4 +4,6 @@ export interface User {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
createdAt: string;
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
}
|
||||
Reference in New Issue
Block a user