direct login
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import { BadRequestException, HttpException, Injectable, Logger } from "@nestjs/common";
|
||||
import { HttpService } from "@nestjs/axios";
|
||||
import { Inject } from "@nestjs/common";
|
||||
import { AxiosError } from "axios";
|
||||
import { catchError, firstValueFrom, throwError } from "rxjs";
|
||||
import { SubscriptionsService } from "../subscriptions/providers/subscriptions.service";
|
||||
import { IDPageConfig } from "../../configs/dpage.config";
|
||||
import { LoginDto } from './dto/login.dto';
|
||||
import { DPAGE_CONFIG } from "./constants";
|
||||
// import { UsersService } from "../users/providers/users.service";
|
||||
// import { UpdateDpageDto } from './dto/update-dpage.dto';
|
||||
|
||||
@Injectable()
|
||||
export class DPageService {
|
||||
private readonly logger = new Logger(DPageService.name);
|
||||
constructor(
|
||||
@Inject(DPAGE_CONFIG) private readonly config: IDPageConfig,
|
||||
private readonly httpService: HttpService,
|
||||
private readonly subscriptionService: SubscriptionsService,
|
||||
// private readonly usersService: UsersService,
|
||||
) { }
|
||||
|
||||
private getHeaders(): Record<string, string> {
|
||||
const credentials = Buffer.from(`${this.config.username}:${this.config.password}`).toString("base64");
|
||||
return {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Basic ${credentials}`,
|
||||
};
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return `This action returns all dpage`;
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
return `This action returns a #${id} dpage`;
|
||||
}
|
||||
|
||||
// update(id: number, updateDpageDto: UpdateDpageDto) {
|
||||
// return `This action updates a #${id} dpage`;
|
||||
// }
|
||||
|
||||
remove(id: number) {
|
||||
return `This action removes a #${id} dpage`;
|
||||
}
|
||||
|
||||
async loginToDpage(dto: LoginDto, userId: string) {
|
||||
const { userSubscriptionId } = dto
|
||||
// 1. validate userSubscription by id
|
||||
const userSubscription = await this.subscriptionService.findOneOrFail(userSubscriptionId)
|
||||
|
||||
|
||||
if (userSubscription.user.id !== userId) {
|
||||
throw new BadRequestException("this subscription doesnt belongs to you")
|
||||
}
|
||||
|
||||
try {
|
||||
// 2. Login with userSubscriptionId
|
||||
const { data } = await firstValueFrom(
|
||||
this.httpService
|
||||
.post(`${this.config.baseUrl}/super-admin/auth/direct-login`, {
|
||||
subscriptionId: userSubscriptionId
|
||||
}, {
|
||||
headers: this.getHeaders(),
|
||||
})
|
||||
.pipe(
|
||||
catchError((err: AxiosError) => {
|
||||
this.logger.error(`Failed to perform direct login: ${err.message}`, err.stack);
|
||||
return throwError(() => err);
|
||||
}),
|
||||
),
|
||||
);
|
||||
return data;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof AxiosError && error.response) {
|
||||
this.logger.error(`External API error response:`, error.response.data);
|
||||
const errorResponse = {
|
||||
...error.response.data,
|
||||
message: error.response.data.error?.message || error.message,
|
||||
};
|
||||
throw new HttpException(errorResponse, error.response.status);
|
||||
}
|
||||
this.logger.error(`Error performing direct login: ${error instanceof Error ? error.message : "Unknown error"}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user