Skip to content

Commit 6a65d44

Browse files
committed
Add next-auth with OIDC to Keycloak
1 parent 15d4d37 commit 6a65d44

File tree

15 files changed

+177
-24
lines changed

15 files changed

+177
-24
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE connections
2+
ADD COLUMN user_id UUID NOT NULL;

apps/dbagent/next.config.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@ const nextConfig: NextConfig = {
1414
},
1515
async rewrites() {
1616
return [];
17+
},
18+
// next-auth configuration
19+
auth: {
20+
providers: [
21+
{
22+
id: 'keycloak',
23+
name: 'Keycloak',
24+
type: 'oidc',
25+
wellKnown: process.env.KEYCLOAK_WELL_KNOWN,
26+
clientId: process.env.KEYCLOAK_CLIENT_ID,
27+
clientSecret: process.env.KEYCLOAK_CLIENT_SECRET,
28+
idToken: true,
29+
checks: ['pkce', 'state']
30+
}
31+
],
32+
secret: process.env.NEXTAUTH_SECRET,
33+
session: {
34+
jwt: true
35+
},
36+
pages: {
37+
signIn: '/auth/signin',
38+
signOut: '/auth/signout',
39+
error: '/auth/error'
40+
}
1741
}
1842
};
1943
module.exports = nextConfig;

apps/dbagent/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
"tailwind-merge": "^3.0.2",
4747
"tailwindcss-animate": "^1.0.7",
4848
"typescript": "^5.7.3",
49-
"zod": "^3.24.2"
49+
"zod": "^3.24.2",
50+
"next-auth": "^4.10.3",
51+
"oidc": "^2.0.0"
5052
},
5153
"devDependencies": {
5254
"@internal/eslint-config": "workspace:*",
@@ -60,7 +62,8 @@
6062
"dotenv": "^16.4.7",
6163
"postcss": "^8.5.3",
6264
"tailwindcss": "^4.0.8",
63-
"tsx": "^4.19.3"
65+
"tsx": "^4.19.3",
66+
"@types/next-auth": "^4.0.0"
6467
},
6568
"engines": {
6669
"node": "22.x",

apps/dbagent/src/app/(main)/chats/page.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
import { ChatsUI } from '~/components/chats/chats-ui';
44
import { listConnections } from '~/lib/db/connections';
5+
import { useSession } from 'next-auth/react';
56

6-
async function getConnections() {
7-
const connections = await listConnections();
7+
async function getConnections(userId: string) {
8+
const connections = await listConnections(userId);
89
return connections;
910
}
1011

1112
export default async function Page() {
12-
const connections = await getConnections();
13+
const { data: session } = useSession();
14+
15+
if (!session) {
16+
return <div>Please sign in to view your chats.</div>;
17+
}
18+
19+
const connections = await getConnections(session.user.id);
1320

1421
return (
1522
<div className="container">

apps/dbagent/src/app/(main)/layout.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
import { BelowHeaderBar, HeaderBar } from '~/app/components/ui/header-bar';
22
import { SideNav } from '../components/ui/side-nav';
3+
import { useSession } from 'next-auth/react';
4+
import { useRouter } from 'next/router';
5+
import { useEffect } from 'react';
36

47
export default async function Layout({ children }: { children: React.ReactNode }) {
8+
const { data: session, status } = useSession();
9+
const router = useRouter();
10+
11+
useEffect(() => {
12+
if (status === 'loading') return; // Do nothing while loading
13+
if (!session) router.push('/auth/signin'); // Redirect if not authenticated
14+
}, [session, status, router]);
15+
16+
if (status === 'loading' || !session) {
17+
return <div>Loading...</div>;
18+
}
19+
520
return (
621
<>
722
<div className="flex h-full overflow-hidden">

apps/dbagent/src/app/(main)/monitoring/page.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { MonitoringScheduleTable } from '~/components/monitoring/schedules-table';
22
import { listConnections } from '~/lib/db/connections';
3+
import { useSession } from 'next-auth/react';
34

4-
async function getConnections() {
5-
const connections = await listConnections();
5+
async function getConnections(userId: string) {
6+
const connections = await listConnections(userId);
67
return connections;
78
}
89

910
export default async function Page() {
10-
const connections = await getConnections();
11+
const { data: session } = useSession();
12+
13+
if (!session) {
14+
return <div>Please sign in to view your monitoring schedules.</div>;
15+
}
16+
17+
const connections = await getConnections(session.user.id);
1118

1219
return (
1320
<div className="container">

apps/dbagent/src/app/(main)/start/cloud/page.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
'use server';
22
import { AWSIntegration } from '~/components/aws-integration/aws-integration';
33
import { listConnections } from '~/lib/db/connections';
4+
import { useSession } from 'next-auth/react';
45

5-
async function getConnections() {
6-
const connections = await listConnections();
6+
async function getConnections(userId: string) {
7+
const connections = await listConnections(userId);
78
return connections;
89
}
910

1011
export default async function Page() {
11-
const connections = await getConnections();
12+
const { data: session } = useSession();
13+
14+
if (!session) {
15+
return <div>Please sign in to view your cloud management integrations.</div>;
16+
}
17+
18+
const connections = await getConnections(session.user.id);
19+
1220
return (
1321
<div className="container mx-auto p-4">
1422
<h1 className="mb-4 text-2xl font-bold">Cloud Management Integration</h1>

apps/dbagent/src/app/(main)/start/collect/page.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
'use server';
22
import { CollectInfo } from '~/components/collect/collect';
33
import { listConnections } from '~/lib/db/connections';
4+
import { useSession } from 'next-auth/react';
45

5-
async function getConnections() {
6-
const connections = await listConnections();
6+
async function getConnections(userId: string) {
7+
const connections = await listConnections(userId);
78
return connections;
89
}
910

1011
export default async function Page() {
11-
const connections = await getConnections();
12+
const { data: session } = useSession();
13+
14+
if (!session) {
15+
return <div>Please sign in to collect info about your database.</div>;
16+
}
17+
18+
const connections = await getConnections(session.user.id);
19+
1220
return (
1321
<div className="container mx-auto max-w-6xl p-4">
1422
<h1 className="mb-4 text-2xl font-bold">Collect info about your database</h1>

apps/dbagent/src/app/(main)/start/connect/page.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { ConnectionsList } from '~/components/connections/connections-list';
2+
import { useSession } from 'next-auth/react';
23

34
export default function Page() {
5+
const { data: session } = useSession();
6+
7+
if (!session) {
8+
return <div>Please sign in to view your connections.</div>;
9+
}
10+
411
return (
512
<div className="container mx-auto max-w-6xl p-4">
613
<ConnectionsList />
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
import { Onboarding } from '~/components/onboarding/onboarding';
2+
import { useSession } from 'next-auth/react';
3+
import { listConnections } from '~/lib/db/connections';
4+
5+
async function getConnections(userId: string) {
6+
const connections = await listConnections(userId);
7+
return connections;
8+
}
29

310
export default function Page() {
11+
const { data: session } = useSession();
12+
13+
if (!session) {
14+
return <div>Please sign in to start onboarding.</div>;
15+
}
16+
17+
const connections = await getConnections(session.user.id);
18+
419
return (
520
<div className="container mx-auto max-w-3xl p-4">
6-
<Onboarding />
21+
<Onboarding connections={connections} />
722
</div>
823
);
924
}

0 commit comments

Comments
 (0)