diff --git a/gateway/src/api/oauth/github.rs b/gateway/src/api/oauth/github.rs index bac27faab..42b0ac6e8 100644 --- a/gateway/src/api/oauth/github.rs +++ b/gateway/src/api/oauth/github.rs @@ -65,8 +65,12 @@ impl OauthHandler for GithubOauthService { .send() .await .unwrap(); - // tracing::debug!("user_resp: {:?}", resp.text().await.unwrap()); - let user_info = resp.json::().await.unwrap(); + let mut user_info = GitHubUserJson::default(); + if resp.status().is_success() { + user_info = resp.json::().await.unwrap(); + } else { + tracing::error!("github:user_info:err {:?}", resp.text().await.unwrap()); + } Ok(user_info) } } diff --git a/gateway/src/api/oauth/model.rs b/gateway/src/api/oauth/model.rs index 8454b1a8c..14e550596 100644 --- a/gateway/src/api/oauth/model.rs +++ b/gateway/src/api/oauth/model.rs @@ -19,7 +19,7 @@ pub struct GitHubAccessTokenJson { pub token_type: String, } -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Debug, Default)] pub struct GitHubUserJson { pub login: String, pub id: u32, diff --git a/lunar/.env.local b/lunar/.env.local index 3f0bbef9a..b6366b0a6 100644 --- a/lunar/.env.local +++ b/lunar/.env.local @@ -1,3 +1,2 @@ RELAY_API_URL=http://34.84.172.121 NEXT_PUBLIC_API_URL=http://localhost:8000 -NEXT_PUBLIC_CALLBACK_URL=http://localhost:3000/auth/github/callback \ No newline at end of file diff --git a/lunar/src/app/application-layout.tsx b/lunar/src/app/application-layout.tsx index efabf9e3c..db092ab9f 100644 --- a/lunar/src/app/application-layout.tsx +++ b/lunar/src/app/application-layout.tsx @@ -41,9 +41,6 @@ import { TicketIcon, } from '@heroicons/react/20/solid' import { usePathname } from 'next/navigation' -import { useUser } from '@/app/api/fetcher'; -import { Skeleton } from "antd"; -import { Button } from '@/components/catalyst/button' import { useState, useEffect } from 'react' function AccountDropdownMenu({ anchor }: { anchor: 'top start' | 'bottom end' }) { @@ -86,19 +83,15 @@ export function ApplicationLayout({ } }, []) - const { user, isLoading, isError } = useUser(token); - if (isLoading) return ; + // const { user, isLoading, isError } = useUser(token); + // if (isLoading) return ; return ( - { - !token && - - } - { + {/* { token && @@ -108,9 +101,7 @@ export function ApplicationLayout({ - - } - + } */} } sidebar={ @@ -183,7 +174,7 @@ export function ApplicationLayout({ - + {/* {token && @@ -201,12 +192,7 @@ export function ApplicationLayout({ } - { - !token && - - } - - + */} } > diff --git a/lunar/src/app/auth/github/callback/page.tsx b/lunar/src/app/auth/github/callback/page.tsx deleted file mode 100644 index 4018ef5c8..000000000 --- a/lunar/src/app/auth/github/callback/page.tsx +++ /dev/null @@ -1,21 +0,0 @@ -'use client' - -import { useSearchParams } from 'next/navigation'; -import { useRouter } from 'next/navigation'; - -export default function AuthPage() { - const router = useRouter(); - const apiUrl = process.env.NEXT_PUBLIC_API_URL; - const searchParams = useSearchParams(); - const access_token = searchParams.get('access_token') || ""; - const code = searchParams.get('code') || ""; - const state = searchParams.get('state') || ""; - - if (code && state) { - const targetUrl = `${apiUrl}/auth/github/callback?code=${code}&state=${state}`; - window.location.href = targetUrl; - } else if (access_token) { - localStorage.setItem('access_token', access_token); - router.push('/'); - } -} diff --git a/moon/.env.local b/moon/.env.local index 6a35dc570..58a33cf5a 100644 --- a/moon/.env.local +++ b/moon/.env.local @@ -1 +1,2 @@ -NEXT_MEGA_API_URL=http://localhost:8000 +NEXT_PUBLIC_API_URL=http://localhost:8000 +NEXT_PUBLIC_CALLBACK_URL=http://localhost:3000/auth/github/callback \ No newline at end of file diff --git a/moon/.env.production b/moon/.env.production index 1fee243d8..3339dfef8 100644 --- a/moon/.env.production +++ b/moon/.env.production @@ -1 +1 @@ -NEXT_MEGA_API_URL=http://mega.api.com +NEXT_PUBLIC_API_URL=http://mega.api.com diff --git a/moon/package.json b/moon/package.json index 9eb20371f..73ce37466 100644 --- a/moon/package.json +++ b/moon/package.json @@ -29,11 +29,11 @@ "react-markdown": "^9.0.1" }, "devDependencies": { - "@types/node": "^20", + "@types/node": "^22", "@types/react": "^18", "@types/react-dom": "^18", "autoprefixer": "^10.4.16", - "eslint": "^8", + "eslint": "^9.8", "eslint-config-next": "14.0.3", "postcss": "^8.4.31", "typescript": "^5" diff --git a/moon/src/app/actions.ts b/moon/src/app/actions.ts new file mode 100644 index 000000000..3f0ab845a --- /dev/null +++ b/moon/src/app/actions.ts @@ -0,0 +1,27 @@ +'use server' + +import { cookies } from 'next/headers' +import { redirect } from 'next/navigation'; + +export async function handleLogin(sessionData) { + const encryptedSessionData = encrypt(sessionData) // Encrypt your session data + cookies().set('access_token', encryptedSessionData, { + httpOnly: true, + secure: process.env.NODE_ENV === 'production', + maxAge: 60 * 60 * 24 * 7, // One week + path: '/', + }) + redirect('/') + // Redirect or handle the response after setting the cookie +} + +export async function get_access_token() { + const cookieStore = cookies() + const token = cookieStore.get('access_token') + return token +} + +// TODO encrypt access_token +function encrypt(sessionData) { + return sessionData +} \ No newline at end of file diff --git a/moon/src/app/api/auth/github/user/route.ts b/moon/src/app/api/auth/github/user/route.ts new file mode 100644 index 000000000..c8cdbb676 --- /dev/null +++ b/moon/src/app/api/auth/github/user/route.ts @@ -0,0 +1,23 @@ +// import { type NextRequest } from 'next/server' + +import { cookies } from "next/headers"; + +export const revalidate = 0 + +const endpoint = process.env.NEXT_PUBLIC_API_URL; + +export async function GET(request: Request) { + const cookieStore = cookies(); + const access_token = cookieStore.get('access_token'); + + const res = await fetch(`${endpoint}/auth/github/user`, { + headers: { + 'Authorization': `Bearer ${access_token?.value}`, + 'Content-Type': 'application/json', + }, + }) + + const data = await res.json() + + return Response.json({ data }) +} \ No newline at end of file diff --git a/moon/src/app/api/blob/route.ts b/moon/src/app/api/blob/route.ts index bad4089e0..089863b96 100644 --- a/moon/src/app/api/blob/route.ts +++ b/moon/src/app/api/blob/route.ts @@ -3,7 +3,7 @@ import { type NextRequest } from 'next/server' export const dynamic = 'force-dynamic' // defaults to auto export const revalidate = 0 -const endpoint = process.env.NEXT_MEGA_API_URL; +const endpoint = process.env.NEXT_PUBLIC_API_URL; export async function GET(request: NextRequest) { const searchParams = request.nextUrl.searchParams diff --git a/moon/src/app/api/mr/[id]/detail/route.ts b/moon/src/app/api/mr/[id]/detail/route.ts index c8ebdbd82..0458babc6 100644 --- a/moon/src/app/api/mr/[id]/detail/route.ts +++ b/moon/src/app/api/mr/[id]/detail/route.ts @@ -1,7 +1,7 @@ export const dynamic = 'force-dynamic' // defaults to auto export const revalidate = 0 -const endpoint = process.env.NEXT_MEGA_API_URL; +const endpoint = process.env.NEXT_PUBLIC_API_URL; export async function GET(request: Request, { params }: { params: { id: string } }) { const res = await fetch(`${endpoint}/api/v1/mr/${params.id}/detail`, { diff --git a/moon/src/app/api/mr/[id]/files/route.ts b/moon/src/app/api/mr/[id]/files/route.ts index 119e65ad2..5e97abcc3 100644 --- a/moon/src/app/api/mr/[id]/files/route.ts +++ b/moon/src/app/api/mr/[id]/files/route.ts @@ -1,6 +1,6 @@ export const dynamic = 'force-dynamic' // defaults to auto -const endpoint = process.env.NEXT_MEGA_API_URL; +const endpoint = process.env.NEXT_PUBLIC_API_URL; export async function GET(request: Request, { params }: { params: { id: string } }) { const res = await fetch(`${endpoint}/api/v1/mr/${params.id}/files`, { diff --git a/moon/src/app/api/mr/[id]/merge/route.ts b/moon/src/app/api/mr/[id]/merge/route.ts index 867021594..2a73b20d7 100644 --- a/moon/src/app/api/mr/[id]/merge/route.ts +++ b/moon/src/app/api/mr/[id]/merge/route.ts @@ -1,6 +1,6 @@ export const dynamic = 'force-dynamic' // defaults to auto -const endpoint = process.env.NEXT_MEGA_API_URL; +const endpoint = process.env.NEXT_PUBLIC_API_URL; export async function POST(request: Request, { params }: { params: { id: string } }) { diff --git a/moon/src/app/api/mr/list/route.ts b/moon/src/app/api/mr/list/route.ts index 1501237d4..b1b30c6bc 100644 --- a/moon/src/app/api/mr/list/route.ts +++ b/moon/src/app/api/mr/list/route.ts @@ -3,7 +3,7 @@ export const revalidate = 0 import { type NextRequest } from 'next/server' -const endpoint = process.env.NEXT_MEGA_API_URL; +const endpoint = process.env.NEXT_PUBLIC_API_URL; export async function GET(request: NextRequest) { const searchParams = request.nextUrl.searchParams diff --git a/moon/src/app/api/tree/commit-info/route.ts b/moon/src/app/api/tree/commit-info/route.ts index 1c794dfcb..25d9693ad 100644 --- a/moon/src/app/api/tree/commit-info/route.ts +++ b/moon/src/app/api/tree/commit-info/route.ts @@ -3,7 +3,7 @@ import { type NextRequest } from 'next/server' export const dynamic = 'force-dynamic' // defaults to auto export const revalidate = 0 -const endpoint = process.env.NEXT_MEGA_API_URL; +const endpoint = process.env.NEXT_PUBLIC_API_URL; export async function GET(request: NextRequest) { const searchParams = request.nextUrl.searchParams diff --git a/moon/src/app/api/tree/route.ts b/moon/src/app/api/tree/route.ts index 1ffb190d1..d86b9bc05 100644 --- a/moon/src/app/api/tree/route.ts +++ b/moon/src/app/api/tree/route.ts @@ -3,7 +3,7 @@ export const revalidate = 0 export const dynamic = 'force-dynamic' // defaults to auto -const endpoint = process.env.NEXT_MEGA_API_URL; +const endpoint = process.env.NEXT_PUBLIC_API_URL; export async function GET(request: NextRequest) { const searchParams = request.nextUrl.searchParams diff --git a/moon/src/app/application-layout.tsx b/moon/src/app/application-layout.tsx index 12458c396..5d209d16c 100644 --- a/moon/src/app/application-layout.tsx +++ b/moon/src/app/application-layout.tsx @@ -40,6 +40,9 @@ import { Square2StackIcon, TicketIcon, } from '@heroicons/react/20/solid' +import { Button } from '@/components/catalyst/button' +import { useState, useEffect } from 'react' +import { get_access_token } from '@/app/actions' import { usePathname } from 'next/navigation' function AccountDropdownMenu({ anchor }: { anchor: 'top start' | 'bottom end' }) { @@ -67,28 +70,64 @@ function AccountDropdownMenu({ anchor }: { anchor: 'top start' | 'bottom end' }) ) } +interface User { + avatar_url: string; + login: string; + id: number; + email: string; +} + export function ApplicationLayout({ - // events, children, }: { - // events: Awaited> children: React.ReactNode }) { let pathname = usePathname() + const [token, setToken] = useState(""); + const [user, setUser] = useState(null); + useEffect(() => { + fetch_token() + async function fetch_token() { + let res = await get_access_token(); + if (res?.value) { + let token_value = res?.value; + setToken(token_value) + } + } + }, []) + + useEffect(() => { + if (token) { + fetchMessage(); + } + async function fetchMessage() { + const response = await fetch('http://localhost:3000/api/auth/github/user'); + const user = await response.json(); + setUser(user.data); + } + }, [token]) return ( - - - - - - - - + { + !user && + + } + { + user && + + + + + + + + + } + } sidebar={ @@ -157,21 +196,27 @@ export function ApplicationLayout({ - - - - - - Admin - - Admin@mega.com + {user && + + + + + + {user.login} + + {user.email} + - - - - - + + + + + } + { + !user && + + } } @@ -180,3 +225,4 @@ export function ApplicationLayout({ ) } + diff --git a/moon/src/app/auth/github/callback/page.tsx b/moon/src/app/auth/github/callback/page.tsx new file mode 100644 index 000000000..049b9357d --- /dev/null +++ b/moon/src/app/auth/github/callback/page.tsx @@ -0,0 +1,17 @@ +'use client' + +import { handleLogin } from '@/app/actions'; + +export default function AuthPage({ searchParams }) { + const apiUrl = process.env.NEXT_PUBLIC_API_URL; + const access_token = searchParams.access_token || ""; + const code = searchParams.code || ""; + const state = searchParams.state || ""; + + if (code && state) { + const targetUrl = `${apiUrl}/auth/github/callback?code=${code}&state=${state}`; + window.location.href = targetUrl; + } else if (access_token) { + handleLogin(access_token) + } +} diff --git a/lunar/src/app/login/layout.tsx b/moon/src/app/login/layout.tsx similarity index 95% rename from lunar/src/app/login/layout.tsx rename to moon/src/app/login/layout.tsx index b096bfacf..426ab0e1d 100644 --- a/lunar/src/app/login/layout.tsx +++ b/moon/src/app/login/layout.tsx @@ -1,4 +1,4 @@ -'use client' +// 'use client' export default function LoginLayout({ children }) { return ( diff --git a/lunar/src/app/login/page.tsx b/moon/src/app/login/page.tsx similarity index 99% rename from lunar/src/app/login/page.tsx rename to moon/src/app/login/page.tsx index 9be2b0e76..a370e093b 100644 --- a/lunar/src/app/login/page.tsx +++ b/moon/src/app/login/page.tsx @@ -1,4 +1,4 @@ -'use client' +// 'use client' export default function Login() {