receiving refresh token

This commit is contained in:
talksik
2022-03-13 22:02:42 -04:00
parent 6e916b2faf
commit 6a8268eacb
5 changed files with 148 additions and 54 deletions
+51 -49
View File
@@ -5,16 +5,33 @@ const path = require("path");
const { app, BrowserWindow, session, ipcMain } = require("electron");
const isDev = require("electron-is-dev");
const Store = require("electron-store");
const store = new Store();
const { channels } = require("../src/shared/constants");
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;
const myApiOauth = new ElectronGoogleOAuth2(
"423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com",
"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",
[""],
{ successRedirectURL: "https://usenirvana.com" }
);
function createWindow() {
// Create the browser window.
const win = new BrowserWindow({
win = new BrowserWindow({
width: 800,
height: 800,
webPreferences: {
nodeIntegration: true,
preload: path.resolve(path.join(__dirname, "./preload.js")),
nodeIntegration: false, // is default value after Electron v5
contextIsolation: true, // protect against prototype pollution
enableRemoteModule: false, // turn off remote
preload: path.join(__dirname, "preload.js"), // use a preload script
},
});
@@ -29,12 +46,23 @@ function createWindow() {
if (isDev) {
win.webContents.openDevTools({ mode: "attach" });
}
// End of the file
ipcMain.on(channels.ACTIVATE_LOG_IN, async (event, arg) => {
await handleLogin();
});
}
// 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(createWindow);
app
.whenReady()
.then(createWindow)
.then(async () => {
console.log(process.env);
await handleLogin();
});
// 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
@@ -51,52 +79,26 @@ app.on("activate", () => {
}
});
app.on("ready", () => {
console.log(process.env);
// const myApiOauth = new ElectronGoogleOAuth2(
// "423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com",
// "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",
// [""],
// { successRedirectURL: "https://usenirvana.com" }
// );
async function handleLogin() {
// read saved refresh token if any
// todo: fix this...should be working
const refreshToken = await store.get("refresh_token");
console.log(refreshToken);
// myApiOauth.openAuthWindowAndGetTokens().then((token) => {
// // use your token.access_token
// const cookie = {
// name: "access_token",
// value: token,
// };
if (refreshToken) {
console.log("have a refresh token from user auth previously", refreshToken);
myApiOauth.setTokens({ refresh_token: refreshToken });
// // session.defaultSession.cookies.set(cookie).then(
// // () => {
// // // success
// // console.log("stored auth token in cookies");
// // },
// // (error) => {
// // console.error(error);
// // }
// // );
// });
});
// send token to client
win.webContents.send(channels.AUTH_TOKEN, refreshToken);
} else {
const token = await myApiOauth.openAuthWindowAndGetTokens();
// End of the file
ipcMain.on(channels.ACTIVATE_LOG_IN, (event, arg) => {
console.log("activated log in channel");
// store the refresh token in cookies for app reopen
store.set("refresh_token", token.refresh_token);
const myApiOauth = new ElectronGoogleOAuth2(
"423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com",
"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",
[""],
{ successRedirectURL: "https://usenirvana.com" }
);
myApiOauth.openAuthWindowAndGetTokens().then((token) => {
// use your token.access_token
const cookie = {
name: "access_token",
value: token,
};
console.log(cookie);
});
});
// todo: send the access token to the renderer
// send token to client
win.webContents.send(channels.AUTH_TOKEN, token.access_token);
}
}
+18 -1
View File
@@ -2,7 +2,24 @@ const { contextBridge, ipcRenderer } = require("electron");
const { channels } = require("../src/shared/constants");
const API = {
activateLogin: () => ipcRenderer.send(channels.ACTIVATE_LOG_IN),
send: (channel, data) => {
// whitelist channels
// let validChannels = ["toMain"];
// if (validChannels.includes(channel)) {
// ipcRenderer.send(channel, data);
// }
ipcRenderer.send(channel, data);
},
receive: (channel, func) => {
// let validChannels = ["fromMain"];
// if (validChannels.includes(channel)) {
// // Deliberately strip event as it includes `sender`
// ipcRenderer.on(channel, (event, ...args) => func(...args));
// }
ipcRenderer.on(channel, (event, ...args) => func(...args));
},
};
contextBridge.exposeInMainWorld("API", API);
+1
View File
@@ -15,6 +15,7 @@
"@types/node": "^16.11.26",
"@types/react": "^17.0.40",
"@types/react-dom": "^17.0.13",
"electron-store": "^8.0.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-query": "^3.34.16",
+13 -2
View File
@@ -1,13 +1,24 @@
import { Button } from "@mui/material";
import Logo from "../../components/Logo";
import { channels } from "../../shared/constants";
import { useEffect } from "react";
export default function () {
export default function Login() {
const logIn = () => {
// send to main process
window.API.activateLogin();
window.API.send(channels.ACTIVATE_LOG_IN);
};
useEffect(() => {
window.API.receive(channels.AUTH_TOKEN, (data: any) => {
console.log(data);
});
// return () => {
// ipcRenderer.removeAllListeners(channels.AUTH_TOKEN);
// };
}, []);
return (
<div className="container flex flex-col space-y-5 justify-center items-center h-screen bg-slate-900">
<Logo className="scale-50" />