-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make Clerk auth in insights app
- Loading branch information
1 parent
692b983
commit 6c067a3
Showing
11 changed files
with
263 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
import { utilFetch } from '@lifeis/common-ui'; | ||
import { getAuth } from '@clerk/nextjs/server'; | ||
import { Client } from '@notionhq/client'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { PageObjectResponse } from '@notionhq/client/build/src/api-endpoints'; | ||
|
||
export const GET = async (): Promise<Response> => { | ||
const response = await utilFetch(`/insights`, { | ||
method: 'GET', | ||
}); | ||
const notion = new Client({ auth: process.env.NEXT_NOTION_API_KEY }); | ||
const insightsDatabaseId = 'caae7cadc59c43fe920dd5ce4048dade'; | ||
|
||
export const GET = async (req: NextRequest, res: NextResponse) => { | ||
const { userId } = getAuth(req); | ||
|
||
if (!response.ok) { | ||
throw new Error('Failed to get logs'); | ||
if (!userId) { | ||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); | ||
} | ||
|
||
return response; | ||
const response = await notion.databases.query({ | ||
database_id: insightsDatabaseId, | ||
}); | ||
// objects; | ||
const insights = response.results.map((obj) => | ||
((obj as PageObjectResponse).properties.insight as any).title.map((title: any) => title.plain_text).join(', '), | ||
); | ||
|
||
return NextResponse.json(insights, { status: 200 }); | ||
}; |
6 changes: 4 additions & 2 deletions
6
apps/insights-app/src/app/components/all-insights/all-insights.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,13 @@ | ||
'use client'; | ||
|
||
import { init, isUserLoggedIn, UserSession } from '@lifeis/common-ui'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { CONFIG } from '../config'; | ||
import { GoogleOAuthProvider } from '@react-oauth/google'; | ||
import { AllInsights } from './components/all-insights/all-insights'; | ||
import { auth, currentUser } from '@clerk/nextjs/server'; | ||
|
||
export default function Index() { | ||
const [isLoggedIn, setIsLoggedIn] = useState(isUserLoggedIn()); | ||
const [isInitialized, setIsInitialized] = useState(false); | ||
|
||
useEffect(() => { | ||
init({ | ||
beUrl: CONFIG.BE_URL, | ||
clientId: CONFIG.CLIENT_ID, | ||
app: 'insights', | ||
}); | ||
setIsInitialized(true); | ||
}, []); | ||
export default async function Index() { | ||
// const { userId } = await auth(); | ||
|
||
return ( | ||
<GoogleOAuthProvider clientId={CONFIG.CLIENT_ID}> | ||
<header> | ||
<UserSession | ||
isLoggedIn={isLoggedIn} | ||
onLoginSuccess={() => setIsLoggedIn(true)} | ||
onLogOut={() => setIsLoggedIn(false)} | ||
/> | ||
</header> | ||
{isLoggedIn && isInitialized && ( | ||
<div> | ||
<AllInsights /> | ||
</div> | ||
)} | ||
</GoogleOAuthProvider> | ||
<div> | ||
<AllInsights /> | ||
{/* {userId} */} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export const CONFIG = { | ||
BE_URL: `${process.env.NEXT_PUBLIC_BE || 'http://localhost:3000'}/api`, | ||
CLIENT_ID: process.env.NEXT_PUBLIC_CLIENT_ID as string, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { clerkMiddleware } from '@clerk/nextjs/server'; | ||
|
||
export default clerkMiddleware(); | ||
|
||
export const config = { | ||
matcher: [ | ||
// Skip Next.js internals and all static files, unless found in search params | ||
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)', | ||
// Always run for API routes | ||
'/(api|trpc)(.*)', | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.