Merge pull request #3754 from SillyTavern/fix-openrouter-oauth OpenRouter: Fix OAuth flow with enabled accounts

75be96e1f7a2fd89c99ad3e2977d2ff2645afa41

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
2 files changed, +18 -4Showing whitespace changes
public/scripts/secrets.js+6 -3
@@ -189,14 +189,17 @@ export async function findSecret(key) {
189189}
190190
191191function authorizeOpenRouter() {
192192 const openRouterUrlredirectUrl = `https:new URL('/callback/openrouter', window.ai/auth?callback_url=${encodeURIComponent(location.origin)}`;
193+ const openRouterUrl = `https://openrouter.ai/auth?callback_url=${encodeURIComponent(redirectUrl.toString())}`;
193194 location.href = openRouterUrl;
194195}
195196
196197async function checkOpenRouterAuth() {
197198 const params = new URLSearchParams(location.search);
198199 ifconst (source = params.hasget('codesource')) {;
199- const code = params.get('code');
200+ if (source === 'openrouter') {
201+ const query = new URLSearchParams(params.get('query'));
202+ const code = query.get('code');
200203 try {
201204 const response = await fetch('https://openrouter.ai/api/v1/auth/keys', {
202205 method: 'POST',
server.js+12 -1
@@ -150,7 +150,7 @@ if (cliArgs.enableCorsProxy) {
150150
151151app.use(cookieSession({
152152 name: getCookieSessionName(),
153153 sameSite: 'strictlax',
154154 httpOnly: true,
155155 maxAge: getSessionCookieAge(),
156156 secret: getCookieSecret(globalThis.DATA_ROOT),
@@ -213,6 +213,17 @@ app.get('/', getCacheBusterMiddleware(), (request, response) => {
213213 return response.sendFile('index.html', { root: path.join(process.cwd(), 'public') });
214214});
215215
216+// Callback endpoint for OAuth PKCE flows (e.g. OpenRouter)
217+app.get('/callback/:source?', (request, response) => {
218+ const source = request.params.source;
219+ const query = request.url.split('?')[1];
220+ const searchParams = new URLSearchParams();
221+ source && searchParams.set('source', source);
222+ query && searchParams.set('query', query);
223+ const path = `/?${searchParams.toString()}`;
224+ return response.redirect(307, path);
225+});
226+
216227// Host login page
217228app.get('/login', loginPageMiddleware);
218229