Julian Bilcke
commited on
Commit
·
3420ebd
1
Parent(s):
58379d0
emergency fix
Browse files- src/app/server/aitube/types.ts +1 -0
- src/app/server/config/getDynamicConfig.ts +17 -0
- src/lib/config/config.ts +6 -0
- src/lib/config/getDefaultDynamicConfig.ts +8 -0
- src/lib/config/useDynamicConfig.ts +31 -0
- src/lib/oauth/useOAuth.ts +11 -23
- src/lib/oauth/useShouldDisplayLoginWall.ts +14 -9
- src/lib/utils/getValidBoolean.ts +9 -0
src/app/server/aitube/types.ts
CHANGED
|
@@ -2,3 +2,4 @@
|
|
| 2 |
// well, lol
|
| 3 |
// https://www.youtube.com/watch?v=CDZg3maL9q0
|
| 4 |
export type Workaround<T> = Promise<{ promise: Promise<T> }>
|
|
|
|
|
|
| 2 |
// well, lol
|
| 3 |
// https://www.youtube.com/watch?v=CDZg3maL9q0
|
| 4 |
export type Workaround<T> = Promise<{ promise: Promise<T> }>
|
| 5 |
+
|
src/app/server/config/getDynamicConfig.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use server"
|
| 2 |
+
|
| 3 |
+
import { DynamicConfig } from "@/lib/config/config"
|
| 4 |
+
import { getValidBoolean } from "@/lib/utils/getValidBoolean"
|
| 5 |
+
|
| 6 |
+
export async function getDynamicConfig(): Promise<DynamicConfig> {
|
| 7 |
+
const config = {
|
| 8 |
+
//oauthClientId: getValidString(process.env.HUGGING_FACE_OAUTH_CLIENT_ID, ""),
|
| 9 |
+
oauthClientId: `${process.env.NEXT_PUBLIC_HUGGING_FACE_OAUTH_CLIENT_ID || ""}`,
|
| 10 |
+
|
| 11 |
+
oauthScopes: "openid profile inference-api",
|
| 12 |
+
enableHuggingFaceOAuth: getValidBoolean(process.env.NEXT_PUBLIC_ENABLE_HUGGING_FACE_OAUTH, false),
|
| 13 |
+
enableHuggingFaceOAuthWall: getValidBoolean(process.env.NEXT_PUBLIC_ENABLE_HUGGING_FACE_OAUTH_WALL, false),
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
return config
|
| 17 |
+
}
|
src/lib/config/config.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export type DynamicConfig = {
|
| 2 |
+
oauthClientId: string
|
| 3 |
+
oauthScopes: string
|
| 4 |
+
enableHuggingFaceOAuth: boolean
|
| 5 |
+
enableHuggingFaceOAuthWall: boolean
|
| 6 |
+
}
|
src/lib/config/getDefaultDynamicConfig.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { DynamicConfig } from "./config"
|
| 2 |
+
|
| 3 |
+
export const getDefaultDynamicConfig = (): DynamicConfig => ({
|
| 4 |
+
oauthClientId: "",
|
| 5 |
+
oauthScopes: "openid profile inference-api",
|
| 6 |
+
enableHuggingFaceOAuth: false,
|
| 7 |
+
enableHuggingFaceOAuthWall: false,
|
| 8 |
+
})
|
src/lib/config/useDynamicConfig.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client"
|
| 2 |
+
|
| 3 |
+
import { useEffect, useState, useTransition } from "react"
|
| 4 |
+
|
| 5 |
+
import { getDynamicConfig } from "@/app/server/config/getDynamicConfig"
|
| 6 |
+
|
| 7 |
+
import { getDefaultDynamicConfig } from "./getDefaultDynamicConfig"
|
| 8 |
+
import { DynamicConfig } from "./config"
|
| 9 |
+
|
| 10 |
+
export function useDynamicConfig(): {
|
| 11 |
+
config: DynamicConfig;
|
| 12 |
+
isConfigReady: boolean;
|
| 13 |
+
} {
|
| 14 |
+
const [_isPending, startTransition] = useTransition()
|
| 15 |
+
const [config, setConfig] = useState<DynamicConfig>(getDefaultDynamicConfig())
|
| 16 |
+
const [isConfigReady, setConfigReady] = useState(false)
|
| 17 |
+
|
| 18 |
+
useEffect(() => {
|
| 19 |
+
startTransition(async () => {
|
| 20 |
+
if (isConfigReady) { return }
|
| 21 |
+
const newConfig = await getDynamicConfig()
|
| 22 |
+
setConfig(newConfig)
|
| 23 |
+
setConfigReady(true)
|
| 24 |
+
})
|
| 25 |
+
}, [isConfigReady])
|
| 26 |
+
|
| 27 |
+
return {
|
| 28 |
+
config,
|
| 29 |
+
isConfigReady
|
| 30 |
+
}
|
| 31 |
+
}
|
src/lib/oauth/useOAuth.ts
CHANGED
|
@@ -4,12 +4,11 @@ import { useEffect } from "react"
|
|
| 4 |
import { useSearchParams } from "next/navigation"
|
| 5 |
import { OAuthResult, oauthHandleRedirectIfPresent, oauthLoginUrl } from "@huggingface/hub"
|
| 6 |
|
| 7 |
-
import { enableHuggingFaceOAuth, oauthClientId, oauthScopes } from "@/app/config"
|
| 8 |
-
|
| 9 |
import { usePersistedOAuth } from "./usePersistedOAuth"
|
| 10 |
import { getValidOAuth } from "./getValidOAuth"
|
| 11 |
import { useShouldDisplayLoginWall } from "./useShouldDisplayLoginWall"
|
| 12 |
import { getOAuthRedirectUrl } from "./getOAuthRedirectUrl"
|
|
|
|
| 13 |
|
| 14 |
export function useOAuth({
|
| 15 |
debug = false
|
|
@@ -28,15 +27,17 @@ export function useOAuth({
|
|
| 28 |
enableOAuthWall: boolean
|
| 29 |
oauthResult?: OAuthResult
|
| 30 |
} {
|
|
|
|
|
|
|
| 31 |
const [oauthResult, setOAuthResult] = usePersistedOAuth()
|
| 32 |
|
| 33 |
-
const clientId = oauthClientId
|
| 34 |
|
| 35 |
// const redirectUrl = config.oauthRedirectUrl
|
| 36 |
const redirectUrl = getOAuthRedirectUrl()
|
| 37 |
|
| 38 |
-
const scopes = oauthScopes
|
| 39 |
-
const enableOAuth = enableHuggingFaceOAuth
|
| 40 |
|
| 41 |
const searchParams = useSearchParams()
|
| 42 |
const code = searchParams?.get("code") || ""
|
|
@@ -46,11 +47,12 @@ export function useOAuth({
|
|
| 46 |
|
| 47 |
// note: being able to log into hugging face using the popup
|
| 48 |
// is different from seeing the "login wall"
|
| 49 |
-
const canLogin: boolean = Boolean(
|
| 50 |
const isLoggedIn = Boolean(oauthResult)
|
| 51 |
|
| 52 |
const enableOAuthWall = useShouldDisplayLoginWall()
|
| 53 |
|
|
|
|
| 54 |
if (debug) {
|
| 55 |
console.log("useOAuth debug:", {
|
| 56 |
oauthResult,
|
|
@@ -65,22 +67,8 @@ export function useOAuth({
|
|
| 65 |
canLogin,
|
| 66 |
isLoggedIn,
|
| 67 |
})
|
| 68 |
-
|
| 69 |
-
/*
|
| 70 |
-
useOAuth debug: {
|
| 71 |
-
oauthResult: '',
|
| 72 |
-
clientId: '........',
|
| 73 |
-
redirectUrl: 'http://localhost:3000',
|
| 74 |
-
scopes: 'openid profile inference-api',
|
| 75 |
-
isOAuthEnabled: true,
|
| 76 |
-
code: '...........',
|
| 77 |
-
state: '{"nonce":".........","redirectUri":"http://localhost:3000"}',
|
| 78 |
-
hasReceivedFreshOAuth: true,
|
| 79 |
-
canLogin: false,
|
| 80 |
-
isLoggedIn: false
|
| 81 |
-
}
|
| 82 |
-
*/
|
| 83 |
}
|
|
|
|
| 84 |
|
| 85 |
useEffect(() => {
|
| 86 |
// no need to perfor the rest if the operation is there is nothing in the url
|
|
@@ -93,11 +81,11 @@ export function useOAuth({
|
|
| 93 |
|
| 94 |
if (!newOAuth) {
|
| 95 |
if (debug) {
|
| 96 |
-
console.log("useOAuth::useEffect 1: got something in the url but no valid oauth data to show.. something went terribly wrong")
|
| 97 |
}
|
| 98 |
} else {
|
| 99 |
if (debug) {
|
| 100 |
-
console.log("useOAuth::useEffect 1: correctly received the new oauth result, saving it to local storage:", newOAuth)
|
| 101 |
}
|
| 102 |
setOAuthResult(newOAuth)
|
| 103 |
|
|
|
|
| 4 |
import { useSearchParams } from "next/navigation"
|
| 5 |
import { OAuthResult, oauthHandleRedirectIfPresent, oauthLoginUrl } from "@huggingface/hub"
|
| 6 |
|
|
|
|
|
|
|
| 7 |
import { usePersistedOAuth } from "./usePersistedOAuth"
|
| 8 |
import { getValidOAuth } from "./getValidOAuth"
|
| 9 |
import { useShouldDisplayLoginWall } from "./useShouldDisplayLoginWall"
|
| 10 |
import { getOAuthRedirectUrl } from "./getOAuthRedirectUrl"
|
| 11 |
+
import { useDynamicConfig } from "../config/useDynamicConfig"
|
| 12 |
|
| 13 |
export function useOAuth({
|
| 14 |
debug = false
|
|
|
|
| 27 |
enableOAuthWall: boolean
|
| 28 |
oauthResult?: OAuthResult
|
| 29 |
} {
|
| 30 |
+
const { config, isConfigReady } = useDynamicConfig()
|
| 31 |
+
|
| 32 |
const [oauthResult, setOAuthResult] = usePersistedOAuth()
|
| 33 |
|
| 34 |
+
const clientId = config.oauthClientId
|
| 35 |
|
| 36 |
// const redirectUrl = config.oauthRedirectUrl
|
| 37 |
const redirectUrl = getOAuthRedirectUrl()
|
| 38 |
|
| 39 |
+
const scopes = config.oauthScopes
|
| 40 |
+
const enableOAuth = config.enableHuggingFaceOAuth
|
| 41 |
|
| 42 |
const searchParams = useSearchParams()
|
| 43 |
const code = searchParams?.get("code") || ""
|
|
|
|
| 47 |
|
| 48 |
// note: being able to log into hugging face using the popup
|
| 49 |
// is different from seeing the "login wall"
|
| 50 |
+
const canLogin: boolean = Boolean(isConfigReady && clientId && enableOAuth)
|
| 51 |
const isLoggedIn = Boolean(oauthResult)
|
| 52 |
|
| 53 |
const enableOAuthWall = useShouldDisplayLoginWall()
|
| 54 |
|
| 55 |
+
/*
|
| 56 |
if (debug) {
|
| 57 |
console.log("useOAuth debug:", {
|
| 58 |
oauthResult,
|
|
|
|
| 67 |
canLogin,
|
| 68 |
isLoggedIn,
|
| 69 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
}
|
| 71 |
+
*/
|
| 72 |
|
| 73 |
useEffect(() => {
|
| 74 |
// no need to perfor the rest if the operation is there is nothing in the url
|
|
|
|
| 81 |
|
| 82 |
if (!newOAuth) {
|
| 83 |
if (debug) {
|
| 84 |
+
// console.log("useOAuth::useEffect 1: got something in the url but no valid oauth data to show.. something went terribly wrong")
|
| 85 |
}
|
| 86 |
} else {
|
| 87 |
if (debug) {
|
| 88 |
+
// console.log("useOAuth::useEffect 1: correctly received the new oauth result, saving it to local storage:", newOAuth)
|
| 89 |
}
|
| 90 |
setOAuthResult(newOAuth)
|
| 91 |
|
src/lib/oauth/useShouldDisplayLoginWall.ts
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
|
|
| 1 |
|
| 2 |
-
// we don't want to display the login wall to people forking the project,
|
| 3 |
-
|
| 4 |
-
import { enableHuggingFaceOAuth, enableHuggingFaceOAuthWall, oauthClientId } from "@/app/config"
|
| 5 |
-
|
| 6 |
-
// or to people who selected no hugging face server at all
|
| 7 |
export function useShouldDisplayLoginWall() {
|
|
|
|
| 8 |
|
| 9 |
-
const
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
return shouldDisplayLoginWall
|
| 16 |
}
|
|
|
|
| 1 |
+
import { useDynamicConfig } from "../config/useDynamicConfig"
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
export function useShouldDisplayLoginWall() {
|
| 4 |
+
const { config, isConfigReady } = useDynamicConfig()
|
| 5 |
|
| 6 |
+
const clientId = config.oauthClientId
|
| 7 |
+
const enableOAuth = config.enableHuggingFaceOAuth
|
| 8 |
+
const enableOAuthWall = config.enableHuggingFaceOAuthWall
|
| 9 |
+
|
| 10 |
+
const isConfigEnablingOAuthWall = Boolean(
|
| 11 |
+
clientId &&
|
| 12 |
+
enableOAuth &&
|
| 13 |
+
enableOAuthWall
|
| 14 |
)
|
| 15 |
|
| 16 |
+
const shouldDisplayLoginWall =
|
| 17 |
+
isConfigReady &&
|
| 18 |
+
isConfigEnablingOAuthWall
|
| 19 |
+
|
| 20 |
return shouldDisplayLoginWall
|
| 21 |
}
|
src/lib/utils/getValidBoolean.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export const getValidBoolean = (something: any, defaultValue: boolean) => {
|
| 2 |
+
if (typeof something === "boolean") {
|
| 3 |
+
return something
|
| 4 |
+
}
|
| 5 |
+
|
| 6 |
+
const strValue = `${something || defaultValue}`.toLowerCase()
|
| 7 |
+
|
| 8 |
+
return strValue === "true" || strValue === "1" || strValue === "on"
|
| 9 |
+
}
|