Skip to content

Instantly share code, notes, and snippets.

@BlackHole1
Created February 28, 2023 08:06
Show Gist options
  • Save BlackHole1/dac924fd8a3716dd8d1e22b80a1ee635 to your computer and use it in GitHub Desktop.
Save BlackHole1/dac924fd8a3716dd8d1e22b80a1ee635 to your computer and use it in GitHub Desktop.
GitHub 登录/注册
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<link href="./styles.css" rel="stylesheet">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using Node.js <span id="node-version"></span>,
Chromium <span id="chrome-version"></span>,
and Electron <span id="electron-version"></span>.
<button onClick="openWindow()">Login Github</button>
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>
const {app, BrowserWindow, session, ipcMain} = require('electron')
const path = require('path')
function createWindow () {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile('index.html')
mainWindow.webContents.openDevTools()
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
session.defaultSession.webRequest.onBeforeSendHeaders({
urls: ["https://*.oomol.dev/*"],
}, async (details, callback) => {
const requestHeaders = Object.assign({}, details.requestHeaders);
const cookies = await session.defaultSession.cookies.get({
domain: ".oomol.dev",
});
cookieStr = cookies.map(x => `${x.name}=${x.value}`).join(";")
requestHeaders.Cookie = cookieStr;
callback({
cancel: false,
requestHeaders,
})
})
ipcMain.handle("get-cookie", async (_event, key) => {
return await session.defaultSession.cookies.get({
domain: ".oomol.dev",
name: key,
})
.then(results => {
if (results.length === 0) {
return null;
}
if (results[0].httpOnly) {
return null;
}
return results[0].value;
})
.catch(err => {
console.error(err);
return null;
})
})
ipcMain.handle("remove-cookie", async (_event, key) => {
return await session.defaultSession.cookies.remove("https://api.oomol.dev", key);
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
{
"name": "1",
"productName": "1",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "Black-Hole<158blackhole@gmail.com>",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "23.0.0"
}
}
// preload with contextIsolation enabled
const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld('cookie', {
get: async (key) => {
return await ipcRenderer.invoke("get-cookie", key)
},
remove: async(key) => {
return await ipcRenderer.invoke("remove-cookie", key)
}
})
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
})
async function openWindow() {
const url = "https://api.oomol.dev/v1/auth/github/redirect";
// 如果想让窗口居中,见: https://stackoverflow.com/a/16861050/6596777
const popup = window.open(url, "_blank", "width=800,height=750");
window.a = popup
const loginGithub = new Promise((r, j) => {
const id = setInterval(async () => {
const authStatus = await window.cookie.get("oomol-auth-status");
if (authStatus !== null) {
clearInterval(id);
popup.close();
await window.cookie.remove("oomol-auth-status");
return authStatus !== "Failed" ? r(authStatus) : j(new Error("login failed"));
}
}, 50);
});
const authStatus = await loginGithub;
if (authStatus === "Intermediate") {
const resp = await fetch("https://api.oomol.dev/v1/auth/intermediate-info", {
method: "GET",
})
data = await resp.json()
console.log(data)
const resp2 = await fetch("https://api.oomol.dev/v1/auth/third-party-signup", {
method: "POST",
body: JSON.stringify(data)
})
const data2 = await resp2.json()
console.log(data2)
}
}
/* styles.css */
/* Add styles here to customize the appearance of your app */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment