Skip to content

Commit

Permalink
auth, list-research-context: use kp credentials from session #7
Browse files Browse the repository at this point in the history
  • Loading branch information
maany committed Aug 4, 2024
1 parent 30dceac commit 84fabe6
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 44 deletions.
18 changes: 4 additions & 14 deletions src/app/_components/list-research-contexts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,20 @@ export type ListResearchContextsPageProps = {
researchContexts: ResearchContext[];
kernelPlancksterHost: string;
};
const IdiotPage = ({children}: {children: React.ReactNode}) => {
return (
<div>
{children}
</div>
)
}

export function ListResearchContextsPage(props: ListResearchContextsPageProps) {
const api = clientContainer.get<TClientComponentAPI>(TRPC.REACT_CLIENT_COMPONENTS_API);
const api = clientContainer.get<TClientComponentAPI>(
TRPC.REACT_CLIENT_COMPONENTS_API,
);
const addNewContextMutation = api.kernel.researchContext.create.useMutation({
onSuccess: () => {
// TODO: handle success
console.log("Context created");
},
});

const MySecyButton = (<button

>Sexy</button>)
return (
<div>
<IdiotPage>
{MySecyButton}
</IdiotPage>
<button disabled={false} />
{addNewContextMutation.isError && (
<div>Error: {addNewContextMutation.error.message}</div>
Expand Down
41 changes: 23 additions & 18 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,41 @@ import { ListResearchContextsPage } from "./_components/list-research-contexts";
import type { ResearchContext } from "@maany_shr/kernel-planckster-sdk-ts";
import serverContainer from "~/lib/infrastructure/server/config/ioc/server-container";
import type AuthGatewayOutputPort from "~/lib/core/ports/secondary/auth-gateway-output-port";
import { GATEWAYS, TRPC } from "~/lib/infrastructure/server/config/ioc/server-ioc-symbols";
import {
GATEWAYS,
TRPC,
} from "~/lib/infrastructure/server/config/ioc/server-ioc-symbols";
import type { TServerComponentAPI } from "~/lib/infrastructure/server/trpc/server-api";
export default async function Home() {
const authGateway = serverContainer.get<AuthGatewayOutputPort>(GATEWAYS.AUTH_GATEWAY);

export default async function ListResearchContexts() {
const authGateway = serverContainer.get<AuthGatewayOutputPort>(
GATEWAYS.AUTH_GATEWAY,
);
const sessionDTO = await authGateway.getSession();
if (!sessionDTO.success) {
redirect("/auth/login");
};
return (
<ListResearchContexts />
}
const kpCredentialsDTO = await authGateway.extractKPCredentials();
const api: TServerComponentAPI = serverContainer.get(
TRPC.REACT_SERVER_COMPONENTS_API,
);
}

async function ListResearchContexts() {
const isConnected = false
const api: TServerComponentAPI = serverContainer.get(TRPC.REACT_SERVER_COMPONENTS_API);

const isConnected = await api.kernel.healthCheck.ping({});
const isAuthorizedKPUser = kpCredentialsDTO.success;
let researchContexts: ResearchContext[] = [];

if (isConnected) {
researchContexts = await api.kernel.researchContext.list(
{ id: env.KP_CLIENT_ID, xAuthToken: env.KP_AUTH_TOKEN },
);
if (isConnected && isAuthorizedKPUser) {
researchContexts = await api.kernel.researchContext.list({
id: kpCredentialsDTO.data.clientID,
xAuthToken: kpCredentialsDTO.data.xAuthToken,
});
}

return (
<div>
<ListResearchContextsPage researchContexts={researchContexts}
<ListResearchContextsPage
researchContexts={researchContexts}
kernelPlancksterHost={env.KP_HOST}
/>
</div>
);
}
}
14 changes: 13 additions & 1 deletion src/lib/core/dto/auth-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ export const GetSessionDTOSchema = DTOSchemaFactory(
}),
)

export type GetSessionDTO = z.infer<typeof GetSessionDTOSchema>;
export type GetSessionDTO = z.infer<typeof GetSessionDTOSchema>;

export const ExtractKPCredentialsDTOSchema = DTOSchemaFactory(
z.object({
xAuthToken: z.string(),
clientID: z.number(),
}),
z.object({
message: z.string(),
})
)

export type ExtractKPCredentialsDTO = z.infer<typeof ExtractKPCredentialsDTOSchema>;
3 changes: 2 additions & 1 deletion src/lib/core/ports/secondary/auth-gateway-output-port.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { GetSessionDTO } from "../../dto/auth-dto";
import type { ExtractKPCredentialsDTO, GetSessionDTO } from "../../dto/auth-dto";

/**
* Represents the output port for the authentication gateway.
*/
export default interface AuthGatewayOutputPort{
getSession(): Promise<GetSessionDTO>;
extractKPCredentials(): Promise<ExtractKPCredentialsDTO>;
}
7 changes: 5 additions & 2 deletions src/lib/infrastructure/server/config/auth/next-auth-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import type { NextAuthOptions } from "next-auth";
import NextAuthCredentialsProvider from "./next-auth-credentials-provider";
import { SessionSchema } from "~/lib/core/entity/auth/session";


/**
Expand All @@ -11,15 +12,17 @@ import NextAuthCredentialsProvider from "./next-auth-credentials-provider";
export const authOptions: NextAuthOptions = {
callbacks: {
jwt: async ({token, user}) => {
console.log("jwt", token);
console.log("user", user);
user && (token.user = user)
return token;
},
session: ({ session, token }) => {
console.log("session", session);
console.log("token", token);
token.user && (session.user = token.user);
const validationResponse = SessionSchema.safeParse(session);
if (!validationResponse.success) {
throw new Error("Session schema validation failed");
}
return session;
},
},
Expand Down
46 changes: 38 additions & 8 deletions src/lib/infrastructure/server/gateway/next-auth-gateway.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
import { inject, injectable } from "inversify";
import { getServerSession, type NextAuthOptions } from "next-auth";
import { TSession } from "~/lib/core/entity/auth/session";
import { SessionSchema, TSession } from "~/lib/core/entity/auth/session";
import AuthGatewayOutputPort from "~/lib/core/ports/secondary/auth-gateway-output-port";
import { CONSTANTS } from "../config/ioc/server-ioc-symbols";
import { GetSessionDTO } from "~/lib/core/dto/auth-dto";
import { ExtractKPCredentialsDTO, GetSessionDTO } from "~/lib/core/dto/auth-dto";

@injectable()
export default class NextAuthGateway implements AuthGatewayOutputPort{
export default class NextAuthGateway implements AuthGatewayOutputPort {
constructor(
@inject(CONSTANTS.NEXT_AUTH_OPTIONS) private authOptions: NextAuthOptions,
) {}
) { }

async getSession(): Promise<GetSessionDTO> {
const session = await getServerSession(this.authOptions);
if (!session) {
return {
success: false,
data: {
notFound: true,
message: "Session not found",
notFound: true,
message: "Session not found",
},
};
}
return {
success: true,
data: session as TSession

};
}


async extractKPCredentials(): Promise<ExtractKPCredentialsDTO> {
const sessionDTO = await this.getSession();
if (!sessionDTO.success) {
return {
success: false,
data: {
message: "User not authenticated",
},
};
}
const session = sessionDTO.data;
const schemaValidationResult = SessionSchema.safeParse(session);
if (!schemaValidationResult.success) {
return {
success: false,
data: {
message: "Session schema validation failed",
},
};
}

const user = sessionDTO.data.user;

return {
success: true,
data: {
clientID: user.kp.client_id,
xAuthToken: user.kp.auth_token,
},
};
}
}

0 comments on commit 84fabe6

Please sign in to comment.