format on save, resolve compile errors, functionality 1. stage
This commit is contained in:
@@ -1,16 +1,15 @@
|
||||
import { Injectable, Signal, WritableSignal, computed, effect, signal } from '@angular/core';
|
||||
import { Component } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable, Signal, computed, effect, signal } from '@angular/core';
|
||||
import { jwtDecode } from 'jwt-decode';
|
||||
import { Observable, distinctUntilChanged, filter, from, lastValueFrom, map } from 'rxjs';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { KeycloakService } from './keycloak.service';
|
||||
import { environment } from '../../environments/environment';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import urlcat from 'urlcat';
|
||||
import { User } from '../../../../bizmatch-server/src/models/db.model';
|
||||
import { JwtToken, ListingCriteria } from '../../../../bizmatch-server/src/models/main.model';
|
||||
import { environment } from '../../environments/environment';
|
||||
import { KeycloakService } from './keycloak.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class UserService {
|
||||
private apiBaseUrl = environment.apiBaseUrl;
|
||||
@@ -18,9 +17,9 @@ export class UserService {
|
||||
// Keycloak services
|
||||
// -----------------------------
|
||||
private user$ = new Observable<User>();
|
||||
private user:User
|
||||
public $isLoggedIn : Signal<boolean>;
|
||||
constructor(public keycloak:KeycloakService,private http: HttpClient){
|
||||
private user: User;
|
||||
public $isLoggedIn: Signal<boolean>;
|
||||
constructor(public keycloak: KeycloakService, private http: HttpClient) {
|
||||
this.user$ = from(this.keycloak.getToken()).pipe(
|
||||
filter(t => !!t),
|
||||
distinctUntilChanged(),
|
||||
@@ -30,18 +29,18 @@ export class UserService {
|
||||
// this.analyticsService.identify(u);
|
||||
// }),
|
||||
);
|
||||
this.$isLoggedIn = signal(false)
|
||||
this.$isLoggedIn = signal(false);
|
||||
this.$isLoggedIn = computed(() => {
|
||||
return keycloak.isLoggedIn()
|
||||
})
|
||||
|
||||
return keycloak.isLoggedIn();
|
||||
});
|
||||
|
||||
effect(async () => {
|
||||
if (this.$isLoggedIn()){
|
||||
this.updateTokenDetails()
|
||||
if (this.$isLoggedIn()) {
|
||||
this.updateTokenDetails();
|
||||
} else {
|
||||
this.user=null;
|
||||
this.user = null;
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
private async refreshToken(): Promise<void> {
|
||||
@@ -57,54 +56,57 @@ export class UserService {
|
||||
this.user = this.map2User(token);
|
||||
}
|
||||
|
||||
|
||||
private map2User(jwt:string):User{
|
||||
private map2User(jwt: string): User {
|
||||
const token = jwtDecode<JwtToken>(jwt);
|
||||
return {
|
||||
id:token.user_id,
|
||||
firstname:token.given_name,
|
||||
lastname:token.family_name,
|
||||
email:token.email
|
||||
}
|
||||
id: token.user_id,
|
||||
firstname: token.given_name,
|
||||
lastname: token.family_name,
|
||||
email: token.email,
|
||||
};
|
||||
}
|
||||
|
||||
isLoggedIn():boolean{
|
||||
isLoggedIn(): boolean {
|
||||
return this.$isLoggedIn();
|
||||
}
|
||||
getUser():User{
|
||||
getUser(): User {
|
||||
return this.user;
|
||||
}
|
||||
getUserObservable():Observable<User>{
|
||||
getUserObservable(): Observable<User> {
|
||||
return this.user$;
|
||||
}
|
||||
logout(){
|
||||
logout() {
|
||||
this.keycloak.logout(window.location.origin + '/home');
|
||||
}
|
||||
async login(url:string){
|
||||
async login(url: string) {
|
||||
await this.keycloak.login({
|
||||
redirectUri: url
|
||||
redirectUri: url,
|
||||
});
|
||||
}
|
||||
getUserRoles(){
|
||||
getUserRoles() {
|
||||
return this.keycloak.getUserRoles(true);
|
||||
}
|
||||
hasAdminRole(){
|
||||
hasAdminRole() {
|
||||
return this.keycloak.getUserRoles(true).includes('ADMIN');
|
||||
}
|
||||
register(url:string){
|
||||
this.keycloak.register({redirectUri:url});
|
||||
register(url: string) {
|
||||
this.keycloak.register({ redirectUri: url });
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Redis services
|
||||
// DB services
|
||||
// -----------------------------
|
||||
async save(user:User):Promise<User>{
|
||||
return await lastValueFrom(this.http.post<User>(`${this.apiBaseUrl}/bizmatch/user`,user));
|
||||
async save(user: User): Promise<User> {
|
||||
return await lastValueFrom(this.http.post<User>(`${this.apiBaseUrl}/bizmatch/user`, user));
|
||||
}
|
||||
async getById(id:string):Promise<User>{
|
||||
async getById(id: string): Promise<User> {
|
||||
return await lastValueFrom(this.http.get<User>(`${this.apiBaseUrl}/bizmatch/user/${id}`));
|
||||
}
|
||||
async search(criteria?:ListingCriteria){
|
||||
return await lastValueFrom(this.http.post<User[]>(`${this.apiBaseUrl}/bizmatch/user/search`,criteria));
|
||||
async getByMail(mail: string): Promise<User> {
|
||||
const url = urlcat(`${this.apiBaseUrl}/bizmatch/user`, { mail });
|
||||
return await lastValueFrom(this.http.get<User>(url));
|
||||
}
|
||||
async search(criteria?: ListingCriteria) {
|
||||
return await lastValueFrom(this.http.post<User[]>(`${this.apiBaseUrl}/bizmatch/user/search`, criteria));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user