Refactor authentication system: replace NextAuth with custom login/logout/session handling, improve cookie management, and enhance error handling
Build and Push Reader Image / docker (push) Successful in 39s
Build and Push Reader Image / docker (push) Successful in 39s
This commit is contained in:
+122
-9
@@ -1,21 +1,104 @@
|
||||
"use client"
|
||||
|
||||
import { SessionProvider, useSession, signIn, signOut } from "next-auth/react"
|
||||
import { useMemo, type ReactNode } from "react"
|
||||
import { useCallback, useEffect, useMemo, useState, type ReactNode } from "react"
|
||||
import type { User } from "./types"
|
||||
|
||||
let googleScriptPromise: Promise<void> | null = null
|
||||
|
||||
function ensureGoogleIdentityScript() {
|
||||
if (typeof window === "undefined") return Promise.resolve()
|
||||
if ((window as any).google?.accounts?.id) return Promise.resolve()
|
||||
if (googleScriptPromise) return googleScriptPromise
|
||||
|
||||
googleScriptPromise = new Promise((resolve, reject) => {
|
||||
const existing = document.querySelector('script[src="https://accounts.google.com/gsi/client"]')
|
||||
if (existing) {
|
||||
existing.addEventListener("load", () => resolve(), { once: true })
|
||||
existing.addEventListener("error", () => reject(new Error("Failed to load Google script")), { once: true })
|
||||
return
|
||||
}
|
||||
|
||||
const script = document.createElement("script")
|
||||
script.src = "https://accounts.google.com/gsi/client"
|
||||
script.async = true
|
||||
script.defer = true
|
||||
script.onload = () => resolve()
|
||||
script.onerror = () => reject(new Error("Failed to load Google script"))
|
||||
document.head.appendChild(script)
|
||||
})
|
||||
|
||||
return googleScriptPromise
|
||||
}
|
||||
|
||||
async function requestGoogleIdToken(clientId: string): Promise<string> {
|
||||
await ensureGoogleIdentityScript()
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const googleApi = (window as any).google?.accounts?.id
|
||||
if (!googleApi) {
|
||||
reject(new Error("Google Identity API is unavailable"))
|
||||
return
|
||||
}
|
||||
|
||||
let settled = false
|
||||
|
||||
googleApi.initialize({
|
||||
client_id: clientId,
|
||||
callback: (response: { credential?: string }) => {
|
||||
if (settled) return
|
||||
settled = true
|
||||
const credential = (response?.credential || "").trim()
|
||||
if (!credential) {
|
||||
reject(new Error("Google did not return ID token"))
|
||||
return
|
||||
}
|
||||
resolve(credential)
|
||||
},
|
||||
auto_select: false,
|
||||
cancel_on_tap_outside: true,
|
||||
})
|
||||
|
||||
googleApi.prompt((notification: any) => {
|
||||
if (settled) return
|
||||
|
||||
if (notification?.isNotDisplayed?.() || notification?.isSkippedMoment?.()) {
|
||||
settled = true
|
||||
reject(new Error("Google sign-in prompt was closed or not displayed"))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
return <SessionProvider>{children}</SessionProvider>
|
||||
return <>{children}</>
|
||||
}
|
||||
|
||||
// Giữ nguyên custom hook `useAuth` để tương thích ngược với UI components hiện tại
|
||||
export function useAuth() {
|
||||
const { data: session, status } = useSession()
|
||||
const [sessionUser, setSessionUser] = useState<any>(null)
|
||||
const [isLoading, setIsLoading] = useState(true)
|
||||
|
||||
const isLoading = status === "loading"
|
||||
const fetchSession = useCallback(async () => {
|
||||
try {
|
||||
setIsLoading(true)
|
||||
const res = await fetch("/api/auth/session", { cache: "no-store" })
|
||||
if (!res.ok) {
|
||||
setSessionUser(null)
|
||||
return
|
||||
}
|
||||
|
||||
// Chuyển đổi session user thành format User của project
|
||||
const sessionUser = session?.user
|
||||
const data = await res.json()
|
||||
setSessionUser(data?.user || null)
|
||||
} catch {
|
||||
setSessionUser(null)
|
||||
} finally {
|
||||
setIsLoading(false)
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
fetchSession()
|
||||
}, [fetchSession])
|
||||
|
||||
const user: User | null = useMemo(() => {
|
||||
if (!sessionUser) return null
|
||||
@@ -30,8 +113,38 @@ export function useAuth() {
|
||||
}
|
||||
}, [sessionUser])
|
||||
|
||||
const loginWithGoogle = () => signIn("google", { callbackUrl: "/" })
|
||||
const logout = () => signOut({ callbackUrl: "/" })
|
||||
const loginWithGoogle = async () => {
|
||||
const clientId = (process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID || "").trim()
|
||||
if (!clientId) {
|
||||
throw new Error("NEXT_PUBLIC_GOOGLE_CLIENT_ID is not configured")
|
||||
}
|
||||
|
||||
const googleIdToken = await requestGoogleIdToken(clientId)
|
||||
|
||||
const result = await fetch("/api/auth/login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ googleIdToken }),
|
||||
})
|
||||
|
||||
if (!result.ok) {
|
||||
const errorText = await result.text()
|
||||
throw new Error(errorText || "Đăng nhập thất bại")
|
||||
}
|
||||
|
||||
const payload = await result.json()
|
||||
setSessionUser(payload?.user || null)
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
const logout = async () => {
|
||||
try {
|
||||
await fetch("/api/auth/logout", { method: "POST" })
|
||||
} finally {
|
||||
setSessionUser(null)
|
||||
}
|
||||
}
|
||||
|
||||
return { user, isLoading, loginWithGoogle, logout }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user