Refactor authentication options to improve environment variable handling and add error logging for API sync failures
Build and Push Reader Image / docker (push) Successful in 1m25s
Build and Push Reader Image / docker (push) Successful in 1m25s
This commit is contained in:
+27
-12
@@ -1,7 +1,9 @@
|
|||||||
import { NextAuthOptions } from "next-auth"
|
import { NextAuthOptions } from "next-auth"
|
||||||
import GoogleProvider from "next-auth/providers/google"
|
import GoogleProvider from "next-auth/providers/google"
|
||||||
|
|
||||||
const readerApiOrigin = (process.env.READER_API_ORIGIN || "http://localhost:8000").replace(/\/+$/, "")
|
const readerApiOrigin = process.env.READER_API_ORIGIN?.replace(/\/+$/, "")
|
||||||
|
const googleClientId = process.env.GOOGLE_CLIENT_ID
|
||||||
|
const googleClientSecret = process.env.GOOGLE_CLIENT_SECRET
|
||||||
|
|
||||||
type MobileLoginResponse = {
|
type MobileLoginResponse = {
|
||||||
accessToken: string
|
accessToken: string
|
||||||
@@ -17,8 +19,8 @@ type MobileLoginResponse = {
|
|||||||
export const authOptions: NextAuthOptions = {
|
export const authOptions: NextAuthOptions = {
|
||||||
providers: [
|
providers: [
|
||||||
GoogleProvider({
|
GoogleProvider({
|
||||||
clientId: process.env.GOOGLE_CLIENT_ID || "demo-id",
|
clientId: googleClientId || "",
|
||||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "demo-secret",
|
clientSecret: googleClientSecret || "",
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
session: {
|
session: {
|
||||||
@@ -27,23 +29,35 @@ export const authOptions: NextAuthOptions = {
|
|||||||
callbacks: {
|
callbacks: {
|
||||||
async jwt({ token, account }) {
|
async jwt({ token, account }) {
|
||||||
if (account?.provider === "google" && account.id_token) {
|
if (account?.provider === "google" && account.id_token) {
|
||||||
|
if (!readerApiOrigin) {
|
||||||
|
console.warn("READER_API_ORIGIN is not configured, skipping reader-api sync after Google login")
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${readerApiOrigin}/api/auth/mobile-login`, {
|
const response = await fetch(`${readerApiOrigin}/api/auth/mobile-login`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ googleIdToken: account.id_token }),
|
body: JSON.stringify({ googleIdToken: account.id_token }),
|
||||||
|
signal: AbortSignal.timeout(5000),
|
||||||
})
|
})
|
||||||
|
|
||||||
if (response.ok) {
|
if (!response.ok) {
|
||||||
const data = (await response.json()) as MobileLoginResponse
|
console.error("reader-api sync failed", {
|
||||||
token.sub = data.user.id
|
status: response.status,
|
||||||
;(token as any).id = data.user.id
|
statusText: response.statusText,
|
||||||
;(token as any).role = data.user.role || "USER"
|
})
|
||||||
;(token as any).name = data.user.name || token.name || null
|
return token
|
||||||
;(token as any).email = data.user.email || token.email || null
|
|
||||||
;(token as any).picture = data.user.image || (token as any).picture || null
|
|
||||||
;(token as any).accessToken = data.accessToken
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const data = (await response.json()) as MobileLoginResponse
|
||||||
|
token.sub = data.user.id
|
||||||
|
;(token as any).id = data.user.id
|
||||||
|
;(token as any).role = data.user.role || "USER"
|
||||||
|
;(token as any).name = data.user.name || token.name || null
|
||||||
|
;(token as any).email = data.user.email || token.email || null
|
||||||
|
;(token as any).picture = data.user.image || (token as any).picture || null
|
||||||
|
;(token as any).accessToken = data.accessToken
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to sync Google login with reader-api", error)
|
console.error("Failed to sync Google login with reader-api", error)
|
||||||
}
|
}
|
||||||
@@ -64,4 +78,5 @@ export const authOptions: NextAuthOptions = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
secret: process.env.NEXTAUTH_SECRET,
|
secret: process.env.NEXTAUTH_SECRET,
|
||||||
|
debug: process.env.NEXTAUTH_DEBUG === "true",
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user