@yan06/openai-oauth-react
v2.0.2
Published
React hook and button for OpenAI OAuth sessions.
Maintainers
Readme
@yan06/openai-oauth-react
Let your users sign in with their ChatGPT accounts.
npm i @yan06/openai-oauth-reactQuickstart:
"use client";
import { SignInWithChatGPT } from "@yan06/openai-oauth-react";
export default function Page() {
return <SignInWithChatGPT />;
}Package Notes
SignInWithChatGPT renders the OpenAI-style sign-in button. After sign-in, it becomes a disconnect button.
The prebuilt button includes a small "Powered by OpenAI OAuth" link by default. Pass hideAttribution to render only the button with no attribution link or reserved space.
Hosted web apps need the open-source Sign in with ChatGPT extension for Chrome or Firefox to complete the OAuth handoff securely. SignInWithChatGPT detects the current browser, shows the correct install screen when needed, and automatically continues once installation is detected. Developers do not need to configure the extension separately.
Browser model calls must go through your own app route because ChatGPT does not allow direct browser CORS requests. Send the signed-in session to that route with openaiAuthHeaders():
"use client";
import { openaiAuthHeaders, SignInWithChatGPT } from "@yan06/openai-oauth-react";
async function ask() {
const response = await fetch("/api/chat", {
method: "POST",
headers: await openaiAuthHeaders({
headers: { "content-type": "application/json" },
}),
body: JSON.stringify({ prompt: "Hello!" }),
});
return response.text();
}openaiAuthHeaders() returns a plain object, so it can be passed directly to fetch, AI SDK hooks, and other code that spreads header objects.
Read the request-bound credentials on your server:
import { createOpenAIOAuth } from "@yan06/openai-oauth-ai-sdk";
import { openaiCredentials } from "@yan06/openai-oauth-react/server";
import { generateText } from "ai";
export async function POST(request: Request) {
const { prompt } = await request.json();
const openai = createOpenAIOAuth(openaiCredentials(request));
const result = await generateText({
model: openai("gpt-5.4-mini"),
prompt,
});
return Response.json({ text: result.text });
}Useful props:
<SignInWithChatGPT
onSuccess={(session) => console.log(session.accountId)}
onError={(error) => console.error(error.message)}
onStateChange={(state) => console.log(state.status)}
hideAttribution
/>For custom UI, use the hook. Custom interfaces are responsible for presenting the extension installation link when needed.
import { useSignInWithChatGPT } from "@yan06/openai-oauth-react";
function CustomLogin() {
const login = useSignInWithChatGPT();
if (login.status === "signed-in") {
return <button onClick={login.logout}>Disconnect</button>;
}
if (login.status === "needs-extension") {
return (
<div>
<a href={login.installUrl} rel="noreferrer" target="_blank">
Install Sign in with ChatGPT
</a>
<button onClick={login.login}>Try again</button>
<button onClick={login.reset}>Cancel</button>
</div>
);
}
return (
<button onClick={login.login}>Sign in with ChatGPT</button>
);
}The hook returns:
type UseSignInWithChatGPTReturn = SignInWithChatGPTState & {
isSignedIn: boolean;
login(): Promise<void>;
logout(): Promise<void>;
refresh(): Promise<OpenAIOAuthSession | null>;
reset(): Promise<void>;
};State statuses:
"checking" | "signed-out" | "starting" | "needs-extension" | "redirecting" | "signed-in" | "error"The default browser session store persists encrypted sessions in IndexedDB. Apps can provide their own store:
import { createSessionStore } from "@yan06/openai-oauth-react";
const sessionStore = createSessionStore();
type SessionStore = {
get(): Promise<OpenAIOAuthSession | null>;
set(session: OpenAIOAuthSession): Promise<void>;
clear(): Promise<void>;
};Exports:
SignInWithChatGPTuseSignInWithChatGPTopenaiAuthHeadersgetSessioncreateSessionStoreopenaiCredentialsfrom@yan06/openai-oauth-react/server
