windows auth (#143)

* windows auth

* update icon

* fix: lint
This commit is contained in:
Innei
2024-07-19 20:21:53 +08:00
committed by GitHub
parent 99e993a644
commit 3f46acf8e0
9 changed files with 169 additions and 131 deletions
+7 -1
View File
@@ -62,7 +62,12 @@ const config: ForgeConfig = {
appBundleId: "is.follow",
icon: "resources/icon",
extraResource: ["./resources/app-update.yml"],
protocols: [
{
name: "Follow",
schemes: ["follow"],
},
],
afterCopy: [cleanSources],
asar: true,
ignore: [/^\/node_modules\/(?!font-list)/],
@@ -117,6 +122,7 @@ const config: ForgeConfig = {
new MakerAppImage({
options: {
icon: "resources/icon.png",
mimeType: ["x-scheme-handler/follow"],
},
}),
],
+3 -2
View File
@@ -141,7 +141,7 @@
"@vercel/ncc": "0.38.1",
"@vitejs/plugin-react": "^4.3.1",
"drizzle-orm": "0.32.0",
"electron": "^30.1.2",
"electron": "^31.0.2",
"electron-devtools-installer": "3.2.0",
"electron-vite": "^2.3.0",
"eslint": "^9.7.0",
@@ -171,5 +171,6 @@
},
"lint-staged": {
"*": "eslint --fix"
}
},
"productName": "Follow"
}
+14 -14
View File
@@ -21,13 +21,13 @@ importers:
dependencies:
'@egoist/tipc':
specifier: 0.3.1
version: 0.3.1(patch_hash=xlivt3m5hothgltzlhgcjx3uca)(electron@30.1.2)(react@18.3.1)
version: 0.3.1(patch_hash=xlivt3m5hothgltzlhgcjx3uca)(electron@31.2.1)(react@18.3.1)
'@electron-toolkit/preload':
specifier: ^3.0.1
version: 3.0.1(electron@30.1.2)
version: 3.0.1(electron@31.2.1)
'@electron-toolkit/utils':
specifier: ^3.0.0
version: 3.0.0(electron@30.1.2)
version: 3.0.0(electron@31.2.1)
'@fontsource/sn-pro':
specifier: 5.0.1
version: 5.0.1
@@ -348,8 +348,8 @@ importers:
specifier: 0.32.0
version: 0.32.0(@opentelemetry/api@1.9.0)(@types/pg@8.6.1)(@types/react@18.3.3)(react@18.3.1)
electron:
specifier: ^30.1.2
version: 30.1.2
specifier: ^31.0.2
version: 31.2.1
electron-devtools-installer:
specifier: 3.2.0
version: 3.2.0
@@ -3664,8 +3664,8 @@ packages:
resolution: {integrity: sha512-oM8BW3a8NEqG0XW+Vx3xywhk0DyDV4T0jT0zZfWt0IczNT3jHAAvQWBorF8osQDplSsCyXXyxrsrQ8cY0Slb/A==}
engines: {node: '>=8.0.0'}
electron@30.1.2:
resolution: {integrity: sha512-A5CFGwbA+HSXnzwjc8fP2GIezBcAb0uN/VbNGLOW8DHOYn07rvJ/1bAJECHUUzt5zbfohveG3hpMQiYpbktuDw==}
electron@31.2.1:
resolution: {integrity: sha512-g3CLKjl4yuXt6VWm/KpgEjYYhFiCl19RgUn8lOC8zV/56ZXAS3+mqV4wWzicE/7vSYXs6GRO7vkYRwrwhX3Gaw==}
engines: {node: '>= 12.20.55'}
hasBin: true
@@ -7212,9 +7212,9 @@ snapshots:
transitivePeerDependencies:
- supports-color
'@egoist/tipc@0.3.1(patch_hash=xlivt3m5hothgltzlhgcjx3uca)(electron@30.1.2)(react@18.3.1)':
'@egoist/tipc@0.3.1(patch_hash=xlivt3m5hothgltzlhgcjx3uca)(electron@31.2.1)(react@18.3.1)':
dependencies:
electron: 30.1.2
electron: 31.2.1
optionalDependencies:
'@tanstack/react-query': 5.51.4(react@18.3.1)
transitivePeerDependencies:
@@ -7439,17 +7439,17 @@ snapshots:
dependencies:
chrome-trace-event: 1.0.4
'@electron-toolkit/preload@3.0.1(electron@30.1.2)':
'@electron-toolkit/preload@3.0.1(electron@31.2.1)':
dependencies:
electron: 30.1.2
electron: 31.2.1
'@electron-toolkit/tsconfig@1.0.1(@types/node@20.14.11)':
dependencies:
'@types/node': 20.14.11
'@electron-toolkit/utils@3.0.0(electron@30.1.2)':
'@electron-toolkit/utils@3.0.0(electron@31.2.1)':
dependencies:
electron: 30.1.2
electron: 31.2.1
'@electron/asar@3.2.10':
dependencies:
@@ -10703,7 +10703,7 @@ snapshots:
- supports-color
optional: true
electron@30.1.2:
electron@31.2.1:
dependencies:
'@electron/get': 2.0.3
'@types/node': 20.14.11
Binary file not shown.

After

Width:  |  Height:  |  Size: 171 KiB

+91 -83
View File
@@ -1,7 +1,5 @@
import "dotenv/config"
import path from "node:path"
import { electronApp, optimizer } from "@electron-toolkit/utils"
import { APP_PROTOCOL, DEEPLINK_SCHEME } from "@shared/constants"
import { extractElectronWindowOptions } from "@shared/electron"
@@ -14,47 +12,99 @@ import { registerUpdater } from "./updater"
import { createMainWindow, createWindow } from "./window"
if (isDev) console.info("[main] env loaded:", env)
initializationApp()
let mainWindow: BrowserWindow
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient(APP_PROTOCOL, process.execPath, [
path.resolve(process.argv[1]),
])
function bootsharp() {
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
return
}
} else {
app.setAsDefaultProtocolClient(APP_PROTOCOL)
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
// Set app user model id for windows
electronApp.setAppUserModelId(`re.${APP_PROTOCOL}`)
let mainWindow: BrowserWindow
// Default open or close DevTools by F12 in development
// and ignore CommandOrControl + R in production.
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
app.on("browser-window-created", (_, window) => {
optimizer.watchWindowShortcuts(window)
initializationApp()
app.on("second-instance", (_, commandLine) => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
const url = commandLine.pop()
if (url) {
handleOpen(url)
}
})
mainWindow = createMainWindow()
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
// Set app user model id for windows
electronApp.setAppUserModelId(`re.${APP_PROTOCOL}`)
app.on("activate", () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
mainWindow = createMainWindow()
mainWindow = createMainWindow()
registerUpdater()
app.on("activate", () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
mainWindow = createMainWindow()
}
})
app.on("open-url", (_, url) => {
if (mainWindow && !mainWindow.isDestroyed()) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
} else {
mainWindow = createMainWindow()
}
handleOpen(url)
})
// Default open or close DevTools by F12 in development
// and ignore CommandOrControl + R in production.
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
app.on("browser-window-created", (_, window) => {
optimizer.watchWindowShortcuts(window)
})
// for dev debug
if (process.env.NODE_ENV === "development") {
import("electron-devtools-installer").then(
({
default: installExtension,
REDUX_DEVTOOLS,
REACT_DEVELOPER_TOOLS,
}) => {
[REDUX_DEVTOOLS, REACT_DEVELOPER_TOOLS].forEach((extension) => {
installExtension(extension, {
loadExtensionOptions: { allowFileAccess: true },
})
.then((name) => console.info(`Added Extension: ${name}`))
.catch((err) => console.info("An error occurred:", err))
})
session.defaultSession.getAllExtensions().forEach((e) => {
session.defaultSession.loadExtension(e.path)
})
},
)
}
})
const handleOpen = (url: string) => {
const urlObj = new URL(url)
if (urlObj.hostname === "auth") {
if (urlObj.hostname === "auth" || urlObj.pathname === "//auth") {
const token = urlObj.searchParams.get("token")
const apiURL =
process.env["VITE_API_URL"] || import.meta.env.VITE_API_URL
if (token && apiURL) {
@@ -80,58 +130,16 @@ app.whenReady().then(() => {
resizable,
})
}
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit()
}
})
}
}
app.on("second-instance", (_, commandLine) => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
const url = commandLine.pop()
if (url) {
handleOpen(url)
}
})
app.on("open-url", (_, url) => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
handleOpen(url)
})
registerUpdater()
// for dev debug
if (process.env.NODE_ENV === "development") {
import("electron-devtools-installer").then(
({
default: installExtension,
REDUX_DEVTOOLS,
REACT_DEVELOPER_TOOLS,
}) => {
[REDUX_DEVTOOLS, REACT_DEVELOPER_TOOLS].forEach((extension) => {
installExtension(extension, {
loadExtensionOptions: { allowFileAccess: true },
})
.then((name) => console.info(`Added Extension: ${name}`))
.catch((err) => console.info("An error occurred:", err))
})
session.defaultSession.getAllExtensions().forEach((e) => {
session.defaultSession.loadExtension(e.path)
})
},
)
}
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit()
}
})
bootsharp()
+10
View File
@@ -2,6 +2,7 @@ import path from "node:path"
import { registerIpcMain } from "@egoist/tipc/main"
import * as Sentry from "@sentry/electron/main"
import { APP_PROTOCOL } from "@shared/constants"
import { app } from "electron"
import { getIconPath } from "./helper"
@@ -15,6 +16,15 @@ const appFolder = {
const isDev = process.env.NODE_ENV === "development"
export const initializationApp = () => {
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient(APP_PROTOCOL, process.execPath, [
path.resolve(process.argv[1]),
])
}
} else {
app.setAsDefaultProtocolClient(APP_PROTOCOL)
}
Sentry.init({
dsn: process.env.VITE_SENTRY_DSN,
})
+2 -2
View File
@@ -31,7 +31,7 @@ export function createWindow(
show: false,
resizable: configs?.resizable ?? true,
autoHideMenuBar: true,
...(platform === "linux" ? { icon: getIconPath() } : {}),
...(platform !== "darwin" ? { icon: getIconPath() } : {}),
webPreferences: {
preload: path.join(__dirname, "../preload/index.mjs"),
sandbox: false,
@@ -66,7 +66,7 @@ export function createWindow(
process.env["ELECTRON_RENDERER_URL"] + (options?.extraPath || ""),
)
} else {
window.loadFile(path.join(__dirname, "../renderer/index.html"))
window.loadFile(path.resolve(__dirname, "../renderer/index.html"))
}
const refererMatchs = [
+2 -29
View File
@@ -5,10 +5,9 @@ import { Outlet } from "react-router-dom"
import { env } from "../../env.js"
import { useAppIsReady } from "./atoms/app"
import { useUISettingKey } from "./atoms/settings/ui"
import { Logo } from "./components/icons/logo"
import { tipcClient } from "./lib/client"
import { appLog } from "./lib/log"
import { getOS } from "./lib/utils"
import { Titlebar } from "./modules/app/Titlebar.js"
import { RootProviders } from "./providers/root-providers"
import { handlers } from "./tipc"
@@ -51,33 +50,7 @@ function App() {
aria-hidden
/>
)}
{windowsElectron && (
<div className="drag-region fixed top-0 z-[99999] flex h-[24px] w-full items-center justify-end rounded-t-[12px] bg-background">
<div className="absolute left-5 top-0 flex h-[24px] items-center gap-2">
<Logo className="size-4" />
<span className="text-sm font-bold">{APP_NAME}</span>
</div>
<button
className="no-drag-region flex h-[24px] w-[32px] items-center justify-center rounded duration-200 hover:bg-theme-item-active"
type="button"
onClick={() => {
tipcClient?.windowAction({ action: "minimize" })
}}
>
<i className="i-mingcute-minimize-line" />
</button>
<button
type="button"
className="no-drag-region flex h-[24px] w-[32px] items-center justify-center rounded duration-200 hover:bg-red-500 hover:!text-white"
onClick={() => {
tipcClient?.windowAction({ action: "close" })
}}
>
<i className="i-mingcute-close-line" />
</button>
</div>
)}
{windowsElectron && <Titlebar />}
<RootProviders>
<AppLayer />
</RootProviders>
+40
View File
@@ -0,0 +1,40 @@
import { tipcClient } from "@renderer/lib/client"
import { useForceUpdate } from "framer-motion"
import { useEffect } from "react"
import { useLocation } from "react-router-dom"
export const Titlebar = () => {
const [update] = useForceUpdate()
const location = useLocation()
useEffect(() => {
update()
}, [location])
return (
<div className="drag-region fixed top-0 z-[99999] flex h-[24px] w-full items-center justify-end rounded-t-[12px] bg-background">
<div className="absolute inset-x-[90px] top-0 flex h-[24px] items-center gap-2">
<div className="flex w-full justify-center">
<span className="text-xs font-bold">{document.title}</span>
</div>
</div>
<button
className="no-drag-region flex h-[24px] w-[32px] items-center justify-center rounded duration-200 hover:bg-theme-item-active"
type="button"
onClick={() => {
tipcClient?.windowAction({ action: "minimize" })
}}
>
<i className="i-mingcute-minimize-line" />
</button>
<button
type="button"
className="no-drag-region flex h-[24px] w-[32px] items-center justify-center rounded duration-200 hover:bg-red-500 hover:!text-white"
onClick={() => {
tipcClient?.windowAction({ action: "close" })
}}
>
<i className="i-mingcute-close-line" />
</button>
</div>
)
}