feat: support google login on desktop (#4053)

Co-authored-by: Alex Yang <himself65@outlook.com>
This commit is contained in:
Peng Xiao
2023-08-31 12:51:49 +08:00
committed by GitHub
parent ba735d8b57
commit 4e45554585
14 changed files with 256 additions and 96 deletions

View File

@@ -7,6 +7,7 @@ import { logger } from './logger';
import {
handleOpenUrlInHiddenWindow,
restoreOrCreateWindow,
setCookie,
} from './main-window';
import { uiSubjects } from './ui';
@@ -62,6 +63,8 @@ async function handleAffineUrl(url: string) {
if (urlToOpen) {
await handleSignIn(urlToOpen);
}
} else if (urlObj.hostname === 'oauth-jwt') {
await handleOauthJwt(url);
}
}
@@ -103,3 +106,32 @@ async function handleSignIn(url: string) {
}
}
}
async function handleOauthJwt(url: string) {
if (url) {
try {
const mainWindow = await restoreOrCreateWindow();
mainWindow.show();
const urlObj = new URL(url);
const token = urlObj.searchParams.get('token');
if (!token) {
logger.error('no token in url', url);
return;
}
// set token to cookie
await setCookie({
url: new URL(mainWindow.webContents.getURL()).origin,
httpOnly: true,
value: token,
name: 'next-auth.session-token',
});
// force reload app
mainWindow.webContents.reload();
} catch (e) {
logger.error('failed to open url in popup', e);
}
}
}

View File

@@ -1,6 +1,6 @@
import assert from 'node:assert';
import { BrowserWindow, nativeTheme } from 'electron';
import { BrowserWindow, type CookiesSetDetails, nativeTheme } from 'electron';
import electronWindowState from 'electron-window-state';
import { join } from 'path';
@@ -174,7 +174,22 @@ export function reloadApp() {
browserWindow?.reload();
}
export async function setCookie(origin: string, cookie: string) {
export async function setCookie(cookie: CookiesSetDetails): Promise<void>;
export async function setCookie(origin: string, cookie: string): Promise<void>;
export async function setCookie(
arg0: CookiesSetDetails | string,
arg1?: string
) {
const window = await restoreOrCreateWindow();
await window.webContents.session.cookies.set(parseCookie(cookie, origin));
const details =
typeof arg1 === 'string' && typeof arg0 === 'string'
? parseCookie(arg0, arg1)
: arg0;
if (typeof details !== 'object') {
throw new Error('invalid cookie details');
}
await window.webContents.session.cookies.set(details);
}