keep plan selection during keycloak hop

This commit is contained in:
2024-09-17 16:51:30 +02:00
parent c00c2caccc
commit eaa8a5064f
9 changed files with 108 additions and 42 deletions

View File

@@ -1,4 +1,6 @@
import { Controller, Get, UseGuards } from '@nestjs/common';
import { Body, Controller, Get, Param, Put, UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from 'src/jwt-auth/jwt-auth.guard';
import { KeycloakUser } from 'src/models/main.model';
import { AdminAuthGuard } from '../jwt-auth/admin-auth.guard';
import { AuthService } from './auth.service';
@@ -18,12 +20,16 @@ export class AuthController {
return this.authService.getUsers();
}
// @UseGuards(AdminAuthGuard)
// @Get('user/:userid')
// getUser(@Param('userid') userId: string): any {
// return this.authService.getUser(userId);
// }
@UseGuards(JwtAuthGuard)
@Get('users/:userid')
getUser(@Param('userid') userId: string): any {
return this.authService.getUser(userId);
}
@UseGuards(JwtAuthGuard)
@Put('users/:userid')
updateKeycloakUser(@Body() keycloakUser: KeycloakUser): any {
return this.authService.updateKeycloakUser(keycloakUser);
}
// @UseGuards(AdminAuthGuard)
// @Get('user/:userid/lastlogin') //e0811669-c7eb-4e5e-a699-e8334d5c5b01 -> aknuth
// getLastLogin(@Param('userid') userId: string): any {

View File

@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { KeycloakUser } from 'src/models/main.model';
import urlcat from 'urlcat';
@Injectable()
export class AuthService {
public async getAccessToken() {
@@ -50,21 +50,40 @@ export class AuthService {
const data = await response.json();
return data as KeycloakUser[];
}
// public async getUser(userid: string) {
// const token = await this.getAccessToken();
// const URLPATH = `${process.env.KEYCLOAK_ADMIN_REALM}${process.env.REALM}${process.env.KEYCLOAK_USER_URL}`;
// const URL = urlcat(process.env.KEYCLOAK_HOST, URLPATH, { userid });
// const response = await ky
// .get(URL, {
// headers: {
// 'Content-Type': 'application/x-www-form-urlencoded',
// Authorization: `Bearer ${token}`,
// },
// })
// .json();
// return response;
// }
public async getUser(userid: string): Promise<KeycloakUser> {
const token = await this.getAccessToken();
const URLPATH = `${process.env.KEYCLOAK_ADMIN_REALM}${process.env.REALM}${process.env.KEYCLOAK_USER_URL}`;
const URL = urlcat(process.env.KEYCLOAK_HOST, URLPATH, { userid });
const response = await fetch(URL, {
method: 'GET',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Bearer ${token}`,
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data as KeycloakUser;
}
public async updateKeycloakUser(keycloakUser: KeycloakUser): Promise<void> {
const token = await this.getAccessToken();
const userid = keycloakUser.id;
const URLPATH = `${process.env.KEYCLOAK_ADMIN_REALM}${process.env.REALM}${process.env.KEYCLOAK_USER_URL}`;
const URL = urlcat(process.env.KEYCLOAK_HOST, URLPATH, { userid });
const response = await fetch(URL, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(keycloakUser),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
}
// public async getLastLogin(userid: string) {
// const token = await this.getAccessToken();
// const URLPATH = `${process.env.KEYCLOAK_ADMIN_REALM}${process.env.REALM}${process.env.KEYCLOAK_LASTLOGIN_URL}`;

View File

@@ -134,8 +134,8 @@ export const commercials = pgTable(
// });
export const listingEvents = pgTable('listing_events', {
id: uuid('id').primaryKey().defaultRandom().notNull(),
listingId: uuid('listing_id'), // Assuming listings are referenced by UUID, adjust as necessary
email: varchar('email', { length: 255 }).references(() => users.email),
listingId: varchar('listing_id', { length: 255 }), // Assuming listings are referenced by UUID, adjust as necessary
email: varchar('email', { length: 255 }),
eventType: varchar('event_type', { length: 50 }), // 'view', 'print', 'email', 'facebook', 'x', 'linkedin', 'contact'
eventTimestamp: timestamp('event_timestamp').defaultNow(),
userIp: varchar('user_ip', { length: 45 }), // Optional if you choose to track IP in frontend or backend

View File

@@ -34,7 +34,7 @@ export const CustomerTypeEnum = z.enum(['buyer', 'seller', 'professional']);
export const SubscriptionTypeEnum = z.enum(['free', 'professional', 'broker']);
export const CustomerSubTypeEnum = z.enum(['broker', 'cpa', 'attorney', 'titleCompany', 'surveyor', 'appraiser']);
export const ListingsCategoryEnum = z.enum(['commercialProperty', 'business']);
export const ZodEventTypeEnum = z.enum(['view', 'print', 'email', 'facebook', 'x', 'linkedin', 'contact', 'favorite']);
export const ZodEventTypeEnum = z.enum(['view', 'print', 'email', 'facebook', 'x', 'linkedin', 'contact', 'favorite', 'emailus', 'pricing']);
export type EventTypeEnum = z.infer<typeof ZodEventTypeEnum>;
const PropertyTypeEnum = z.enum(['retail', 'land', 'industrial', 'office', 'mixedUse', 'multifamily', 'uncategorized']);
const TypeEnum = z.enum([
@@ -331,7 +331,7 @@ export type ShareByEMail = z.infer<typeof ShareByEMailSchema>;
export const ListingEventSchema = z.object({
id: z.string().uuid(), // UUID für das Event
listingId: z.string().uuid(), // UUID für das Listing
listingId: z.string().uuid().optional().nullable(), // UUID für das Listing
email: z.string().email().optional().nullable(), // EMail des den Benutzer, optional, wenn kein Benutzer eingeloggt ist
eventType: ZodEventTypeEnum, // Die Event-Typen
eventTimestamp: z.string().datetime().or(z.date()), // Der Zeitstempel des Events, kann ein String im ISO-Format oder ein Date-Objekt sein

View File

@@ -120,6 +120,7 @@ export interface KeycloakUser {
requiredActions?: any[];
notBefore?: number;
access?: Access;
attributes?: Attributes;
}
export interface JwtUser {
userId: string;
@@ -128,6 +129,10 @@ export interface JwtUser {
lastname: string;
roles: string[];
}
interface Attributes {
[key: string]: any;
priceID: any;
}
export interface Access {
manageGroupMembership: boolean;
view: boolean;
@@ -174,6 +179,7 @@ export interface JwtToken {
family_name: string;
email: string;
user_id: string;
price_id: string;
}
export interface JwtPayload {
sub: string;