From 6a8268eacbc07587ac7cb1d627f274cc89f28c30 Mon Sep 17 00:00:00 2001 From: talksik Date: Sun, 13 Mar 2022 22:02:42 -0400 Subject: [PATCH] receiving refresh token --- packages/desktop/electron/electron.js | 100 +++++++++++---------- packages/desktop/electron/preload.js | 19 +++- packages/desktop/package.json | 1 + packages/desktop/src/pages/Login/index.tsx | 15 +++- yarn.lock | 67 +++++++++++++- 5 files changed, 148 insertions(+), 54 deletions(-) diff --git a/packages/desktop/electron/electron.js b/packages/desktop/electron/electron.js index f4ff913..52e4caa 100644 --- a/packages/desktop/electron/electron.js +++ b/packages/desktop/electron/electron.js @@ -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); + } +} diff --git a/packages/desktop/electron/preload.js b/packages/desktop/electron/preload.js index 2dd8444..bcea0bf 100644 --- a/packages/desktop/electron/preload.js +++ b/packages/desktop/electron/preload.js @@ -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); diff --git a/packages/desktop/package.json b/packages/desktop/package.json index f7b7019..792512e 100644 --- a/packages/desktop/package.json +++ b/packages/desktop/package.json @@ -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", diff --git a/packages/desktop/src/pages/Login/index.tsx b/packages/desktop/src/pages/Login/index.tsx index 1ab37a1..6bc075f 100644 --- a/packages/desktop/src/pages/Login/index.tsx +++ b/packages/desktop/src/pages/Login/index.tsx @@ -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 (
diff --git a/yarn.lock b/yarn.lock index d672d5d..f8b3852 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2951,7 +2951,7 @@ ajv@^6.10.0, ajv@^6.12.2, ajv@^6.12.3, ajv@^6.12.4, ajv@^6.12.5: json-schema-traverse "^0.4.1" uri-js "^4.2.2" -ajv@^8.0.0, ajv@^8.6.0, ajv@^8.8.0: +ajv@^8.0.0, ajv@^8.6.0, ajv@^8.6.3, ajv@^8.8.0: version "8.10.0" resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.10.0.tgz#e573f719bd3af069017e3b66538ab968d040e54d" integrity sha512-bzqAEZOjkrUMl2afH8dknrq5KEk2SrwdBROR+vH1EKVQTqaUbJVPdc/gEdggTMM0Se+s+Ja4ju4TlNcStKl2Hw== @@ -3183,6 +3183,11 @@ atob@^2.1.2: resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9" integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg== +atomically@^1.7.0: + version "1.7.0" + resolved "https://registry.yarnpkg.com/atomically/-/atomically-1.7.0.tgz#c07a0458432ea6dbc9a3506fffa424b48bccaafe" + integrity sha512-Xcz9l0z7y9yQ9rdDaxlmaI4uJHf/T8g9hOEzJcsEqX2SjCj4J20uK7+ldkDHMbpJDK76wF7xEIgxc/vSlsfw5w== + author-regex@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/author-regex/-/author-regex-1.0.0.tgz#d08885be6b9bbf9439fe087c76287245f0a81450" @@ -4039,6 +4044,22 @@ concurrently@^7.0.0: tree-kill "^1.2.2" yargs "^16.2.0" +conf@^10.0.3: + version "10.1.1" + resolved "https://registry.yarnpkg.com/conf/-/conf-10.1.1.tgz#ff08046d5aeeee0eaff55d57f5b4319193c3dfda" + integrity sha512-z2civwq/k8TMYtcn3SVP0Peso4otIWnHtcTuHhQ0zDZDdP4NTxqEc8owfkz4zBsdMYdn/LFcE+ZhbCeqkhtq3Q== + dependencies: + ajv "^8.6.3" + ajv-formats "^2.1.1" + atomically "^1.7.0" + debounce-fn "^4.0.0" + dot-prop "^6.0.1" + env-paths "^2.2.1" + json-schema-typed "^7.0.3" + onetime "^5.1.2" + pkg-up "^3.1.0" + semver "^7.3.5" + config-chain@^1.1.11: version "1.1.13" resolved "https://registry.yarnpkg.com/config-chain/-/config-chain-1.1.13.tgz#fad0795aa6a6cdaff9ed1b68e9dff94372c232f4" @@ -4417,6 +4438,13 @@ date-fns@^2.16.1: resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.28.0.tgz#9570d656f5fc13143e50c975a3b6bbeb46cd08b2" integrity sha512-8d35hViGYx/QH0icHYCeLmsLmMUheMmTyV9Fcm6gvNwdw31yXXH+O85sOBJ+OLnLQMKZowvpKb6FgMIQjcpvQw== +debounce-fn@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/debounce-fn/-/debounce-fn-4.0.0.tgz#ed76d206d8a50e60de0dd66d494d82835ffe61c7" + integrity sha512-8pYCQiL9Xdcg0UPSD3d+0KMlOjp+KGU5EPwYddgzQ7DATsg4fuUDjQtsYLmWjnk2obnNHgV3vE2Y4jejSOJVBQ== + dependencies: + mimic-fn "^3.0.0" + debug@2.6.9, debug@^2.1.3, debug@^2.2.0, debug@^2.6.0, debug@^2.6.8, debug@^2.6.9: version "2.6.9" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" @@ -4748,6 +4776,13 @@ dot-case@^3.0.4: no-case "^3.0.4" tslib "^2.0.3" +dot-prop@^6.0.1: + version "6.0.1" + resolved "https://registry.yarnpkg.com/dot-prop/-/dot-prop-6.0.1.tgz#fc26b3cf142b9e59b74dbd39ed66ce620c681083" + integrity sha512-tE7ztYzXHIeyvc7N+hR3oi7FIbf/NIjVP9hmAt3yMXzrQ072/fpjGLx2GxNxGxUl5V73MEqYzioOMoVhGMJ5cA== + dependencies: + is-obj "^2.0.0" + dotenv-expand@^5.1.0: version "5.1.0" resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-5.1.0.tgz#3fbaf020bfd794884072ea26b1e9791d45a629f0" @@ -4915,6 +4950,14 @@ electron-squirrel-startup@^1.0.0: dependencies: debug "^2.2.0" +electron-store@^8.0.1: + version "8.0.1" + resolved "https://registry.yarnpkg.com/electron-store/-/electron-store-8.0.1.tgz#9b598c1d2edeffebee9d8c1cd957ad368c528925" + integrity sha512-ZyLvNywiqSpbwC/pp89O/AycVWY/UJIkmtyzF2Bd0Nm/rLmcFc0NTGuLdg6+LE8mS8qsiK5JMoe4PnrecLHH5w== + dependencies: + conf "^10.0.3" + type-fest "^1.0.2" + electron-to-chromium@^1.4.76: version "1.4.82" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.82.tgz#51e123ca434b1eba8c434ece2b54f095b304a651" @@ -4992,7 +5035,7 @@ entities@^2.0.0: resolved "https://registry.yarnpkg.com/entities/-/entities-2.2.0.tgz#098dc90ebb83d8dffa089d55256b351d34c4da55" integrity sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A== -env-paths@^2.2.0: +env-paths@^2.2.0, env-paths@^2.2.1: version "2.2.1" resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.1.tgz#420399d416ce1fbe9bc0a07c62fa68d67fd0f8f2" integrity sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A== @@ -6705,6 +6748,11 @@ is-obj@^1.0.1: resolved "https://registry.yarnpkg.com/is-obj/-/is-obj-1.0.1.tgz#3e4729ac1f5fde025cd7d83a896dab9f4f67db0f" integrity sha1-PkcprB9f3gJc19g6iW2rn09n2w8= +is-obj@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/is-obj/-/is-obj-2.0.0.tgz#473fb05d973705e3fd9620545018ca8e22ef4982" + integrity sha512-drqDG3cbczxxEJRoOXcOjtdp1J/lyp1mNn0xaznRs8+muBhgQcrnbspox5X5fOw0HnMnbfDzvnEMEtqDEJEo8w== + is-path-cwd@^2.2.0: version "2.2.0" resolved "https://registry.yarnpkg.com/is-path-cwd/-/is-path-cwd-2.2.0.tgz#67d43b82664a7b5191fd9119127eb300048a9fdb" @@ -7440,6 +7488,11 @@ json-schema-traverse@^1.0.0: resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz#ae7bcb3656ab77a73ba5c49bf654f38e6b6860e2" integrity sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug== +json-schema-typed@^7.0.3: + version "7.0.3" + resolved "https://registry.yarnpkg.com/json-schema-typed/-/json-schema-typed-7.0.3.tgz#23ff481b8b4eebcd2ca123b4fa0409e66469a2d9" + integrity sha512-7DE8mpG+/fVw+dTpjbxnx47TaMnDfOI1jwft9g1VybltZCduyRQPJPvc+zzKY9WPHxhPWczyFuYa6I8Mw4iU5A== + json-schema@0.4.0, json-schema@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.4.0.tgz#f7de4cf6efab838ebaeb3236474cbba5a1930ab5" @@ -7941,6 +7994,11 @@ mimic-fn@^2.0.0, mimic-fn@^2.1.0: resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b" integrity sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg== +mimic-fn@^3.0.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-3.1.0.tgz#65755145bbf3e36954b949c16450427451d5ca74" + integrity sha512-Ysbi9uYW9hFyfrThdDEQuykN4Ey6BuwPD2kpI5ES/nFTDn/98yxYNLZJcgUAKPT/mcrLLKaGzJR9YVxJrIdASQ== + mimic-response@^1.0.0, mimic-response@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-1.0.1.tgz#4923538878eef42063cb8a3e3b0798781487ab1b" @@ -11152,6 +11210,11 @@ type-fest@^0.21.3: resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.21.3.tgz#d260a24b0198436e133fa26a524a6d65fa3b2e37" integrity sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w== +type-fest@^1.0.2: + version "1.4.0" + resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-1.4.0.tgz#e9fb813fe3bf1744ec359d55d1affefa76f14be1" + integrity sha512-yGSza74xk0UG8k+pLh5oeoYirvIiWo5t0/o3zHHAO2tRDiZcxWP7fywNlXhqb6/r6sWvwi+RsyQMWhVLe4BVuA== + type-is@~1.6.18: version "1.6.18" resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131"