refactor(core): auth (#7999)

closes AF-753 AF-1227
This commit is contained in:
forehalo
2024-09-03 09:03:43 +00:00
parent 8b0afd6eeb
commit e33aa35f7e
18 changed files with 286 additions and 166 deletions

View File

@@ -39,6 +39,8 @@ const desktopWhiteList = [
'/upgrade-success',
'/ai-upgrade-success',
'/share',
'/oauth',
'/magic-link',
];
if (
!environment.isDesktop &&

View File

@@ -2,13 +2,13 @@ import path from 'node:path';
import type { App } from 'electron';
import { buildType, CLOUD_BASE_URL, isDev } from './config';
import { buildType, isDev } from './config';
import { mainWindowOrigin } from './constants';
import { logger } from './logger';
import {
getMainWindow,
handleOpenUrlInHiddenWindow,
setCookie,
openUrlInHiddenWindow,
openUrlInMainWindow,
} from './windows-manager';
let protocol = buildType === 'stable' ? 'affine' : `affine-${buildType}`;
@@ -61,51 +61,28 @@ async function handleAffineUrl(url: string) {
logger.info('open affine url', url);
const urlObj = new URL(url);
logger.info('handle affine schema action', urlObj.hostname);
// handle more actions here
// hostname is the action name
if (urlObj.hostname === 'signin-redirect') {
await handleOauthJwt(url);
}
if (urlObj.hostname === 'bring-to-front') {
const mainWindow = await getMainWindow();
if (mainWindow) {
mainWindow.show();
}
} else {
await openUrl(urlObj);
}
}
async function handleOauthJwt(url: string) {
const mainWindow = await getMainWindow();
if (url && mainWindow) {
try {
mainWindow.show();
const urlObj = new URL(url);
const token = urlObj.searchParams.get('token');
async function openUrl(urlObj: URL) {
const params = urlObj.searchParams;
if (!token) {
logger.error('no token in url', url);
return;
}
const openInHiddenWindow = params.get('hidden');
params.delete('hidden');
// set token to cookie
await setCookie({
url: CLOUD_BASE_URL,
httpOnly: true,
value: token,
secure: true,
name: 'affine_session',
expirationDate: Math.floor(
Date.now() / 1000 +
3600 *
24 *
399 /* as long as possible, cookie max expires is 400 days */
),
});
// hacks to refresh auth state in the main window
await handleOpenUrlInHiddenWindow(mainWindowOrigin + '/auth/signIn');
} catch (e) {
logger.error('failed to open url in popup', e);
}
const url = mainWindowOrigin + urlObj.pathname + '?' + params.toString();
if (!openInHiddenWindow) {
await openUrlInHiddenWindow(url);
} else {
// TODO(@pengx17): somehow the page won't load the url passed, help needed
await openUrlInMainWindow(url);
}
}

View File

@@ -229,16 +229,15 @@ export async function showMainWindow() {
/**
* Open a URL in a hidden window.
* This is useful for opening a URL in the background without user interaction for *authentication*.
*/
export async function handleOpenUrlInHiddenWindow(url: string) {
export async function openUrlInHiddenWindow(url: string) {
const win = new BrowserWindow({
width: 1200,
height: 600,
webPreferences: {
preload: join(__dirname, './preload.js'),
},
show: false,
show: environment.isDebug,
});
win.on('close', e => {
e.preventDefault();
@@ -250,3 +249,11 @@ export async function handleOpenUrlInHiddenWindow(url: string) {
await win.loadURL(url);
return win;
}
export async function openUrlInMainWindow(url: string) {
const mainWindow = await getMainWindow();
if (mainWindow) {
mainWindow.show();
await mainWindow.loadURL(url);
}
}

View File

@@ -29,12 +29,20 @@ export function getElectronAPIs() {
};
}
type Schema =
| 'affine'
| 'affine-canary'
| 'affine-beta'
| 'affine-internal'
| 'affine-dev';
// todo: remove duplicated codes
const ReleaseTypeSchema = z.enum(['stable', 'beta', 'canary', 'internal']);
const envBuildType = (process.env.BUILD_TYPE || 'canary').trim().toLowerCase();
const buildType = ReleaseTypeSchema.parse(envBuildType);
const isDev = process.env.NODE_ENV === 'development';
let schema = buildType === 'stable' ? 'affine' : `affine-${envBuildType}`;
let schema =
buildType === 'stable' ? 'affine' : (`affine-${envBuildType}` as Schema);
schema = isDev ? 'affine-dev' : schema;
export const appInfo = {
@@ -45,7 +53,7 @@ export const appInfo = {
viewId:
process.argv.find(arg => arg.startsWith('--view-id='))?.split('=')[1] ??
'unknown',
schema: `${schema}`,
schema,
};
function getMainAPIs() {