trying with the store and such
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
import { Route, useNavigate } from "react-router-dom";
|
||||
|
||||
import { STORE_ITEMS } from "../../electron/store";
|
||||
import { useEffect } from "react";
|
||||
import { useGetUserDetails } from "../../controller/index";
|
||||
|
||||
export default function ProtectedRoute({ ...children }) {
|
||||
const { isLoading, isError } = useGetUserDetails();
|
||||
|
||||
useEffect(() => {
|
||||
console.log(window.electronAPI.store.get(STORE_ITEMS.AUTH_TOKENS));
|
||||
}, []);
|
||||
|
||||
const navigate = useNavigate();
|
||||
|
||||
if (isLoading) return <span>please wait while we authenticate you</span>;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import axios, { AxiosRequestConfig } from "axios";
|
||||
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
|
||||
|
||||
import { User } from "@nirvana/core/models";
|
||||
|
||||
// export const localHost = process.env.REACT_APP_API_DOMAIN;
|
||||
|
||||
@@ -12,22 +14,22 @@ class NirvanaApi {
|
||||
this._authToken = _googleIdToken;
|
||||
}
|
||||
|
||||
async fetch(options: AxiosRequestConfig, privateRoute = false) {
|
||||
async fetch<T>(options: AxiosRequestConfig, privateRoute = false) {
|
||||
// use the auth token if it's a private route
|
||||
// error if no auth token and it's a private route
|
||||
// throw error and show message on anything that is an error from the backend
|
||||
if (privateRoute && this._authToken) {
|
||||
return await axios({
|
||||
return await axios.request<T>({
|
||||
...options,
|
||||
headers: { Authorization: this._authToken },
|
||||
});
|
||||
}
|
||||
|
||||
return axios(options);
|
||||
return axios.request<T>(options);
|
||||
}
|
||||
|
||||
async getUserDetails() {
|
||||
return await this.fetch(
|
||||
async getUserDetails(): Promise<AxiosResponse<User>> {
|
||||
return await this.fetch<User>(
|
||||
{
|
||||
method: "GET",
|
||||
url: localHost + `/users`,
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import { electronAPI } from "./preload";
|
||||
|
||||
export {};
|
||||
declare global {
|
||||
interface Window {
|
||||
electronAPI: typeof electronAPI;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
import store, { STORE_ITEMS } from "./store";
|
||||
|
||||
import Channels from "./constants";
|
||||
import ElectronGoogleOAuth2 from "@getstation/electron-google-oauth2";
|
||||
import { browserWindow } from "../index";
|
||||
import { ipcMain } from "electron";
|
||||
import store from "./store";
|
||||
|
||||
const myApiOauth = new ElectronGoogleOAuth2(
|
||||
"423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com",
|
||||
@@ -14,7 +15,7 @@ const myApiOauth = new ElectronGoogleOAuth2(
|
||||
export async function handleLogin() {
|
||||
// read saved refresh token if any
|
||||
// todo: fix this...should be working
|
||||
const refreshToken = await store.get("tokens");
|
||||
const authTokens = await store.get(STORE_ITEMS.AUTH_TOKENS);
|
||||
|
||||
// todo: remove this when I have way of getting access token from a refresh token
|
||||
// if (refreshToken) {
|
||||
@@ -35,7 +36,8 @@ export async function handleLogin() {
|
||||
// }
|
||||
|
||||
const tokens = await myApiOauth.openAuthWindowAndGetTokens();
|
||||
store.set("tokens", tokens);
|
||||
|
||||
store.set(STORE_ITEMS.AUTH_TOKENS, tokens);
|
||||
|
||||
browserWindow.webContents.send(Channels.AUTH_TOKENS, tokens);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { contextBridge, ipcRenderer } from "electron";
|
||||
|
||||
import Channels from "./constants";
|
||||
|
||||
const electronAPI = {
|
||||
auth: {
|
||||
initiateLogin() {
|
||||
ipcRenderer.send(Channels.ACTIVATE_LOG_IN);
|
||||
},
|
||||
receiveTokens(callback: any) {
|
||||
ipcRenderer.on(Channels.AUTH_TOKENS, callback);
|
||||
},
|
||||
},
|
||||
batteryApi: {},
|
||||
fileApi: {},
|
||||
store: {
|
||||
get(val: string) {
|
||||
return ipcRenderer.sendSync("electron-store-get", val);
|
||||
},
|
||||
set(property: string, val: any) {
|
||||
ipcRenderer.send("electron-store-set", property, val);
|
||||
},
|
||||
// Other method you want to add like has(), reset(), etc.
|
||||
},
|
||||
};
|
||||
|
||||
contextBridge.exposeInMainWorld("electronAPI", electronAPI);
|
||||
|
||||
export default electronAPI;
|
||||
@@ -1,4 +1,9 @@
|
||||
import Store from "electron-store";
|
||||
import { ipcMain } from "electron";
|
||||
const store = new Store();
|
||||
|
||||
export enum STORE_ITEMS {
|
||||
AUTH_TOKENS = "AUTH_TOKENS",
|
||||
}
|
||||
|
||||
export default store;
|
||||
|
||||
@@ -2,11 +2,13 @@ import { BrowserWindow, app, ipcMain } from "electron";
|
||||
|
||||
import Channels from "./electron/constants";
|
||||
import { handleLogin } from "./electron/handleLogin";
|
||||
import store from "./electron/store";
|
||||
|
||||
// This allows TypeScript to pick up the magic constant that's auto-generated by Forge's Webpack
|
||||
// plugin that tells the Electron app where to look for the Webpack-bundled app code (depending on
|
||||
// whether you're running in development or production).
|
||||
declare const MAIN_WINDOW_WEBPACK_ENTRY: string;
|
||||
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: any;
|
||||
|
||||
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
|
||||
if (require("electron-squirrel-startup")) {
|
||||
@@ -24,7 +26,7 @@ const createWindow = (): void => {
|
||||
height: 600,
|
||||
width: 800,
|
||||
webPreferences: {
|
||||
nodeIntegration: true,
|
||||
preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -42,12 +44,18 @@ app
|
||||
.whenReady()
|
||||
.then(createWindow)
|
||||
.then(() => {
|
||||
console.log("hahha");
|
||||
|
||||
// End of the file
|
||||
ipcMain.on(Channels.ACTIVATE_LOG_IN, async (event, arg) => {
|
||||
console.log("initiating log in");
|
||||
await handleLogin();
|
||||
});
|
||||
|
||||
// IPC listener
|
||||
ipcMain.on("electron-store-get", async (event, val) => {
|
||||
event.returnValue = store.get(val);
|
||||
});
|
||||
ipcMain.on("electron-store-set", async (event, key, val) => {
|
||||
store.set(key, val);
|
||||
});
|
||||
});
|
||||
|
||||
// Quit when all windows are closed, except on macOS. There, it's common
|
||||
|
||||
@@ -7,6 +7,8 @@ import {
|
||||
} from "@mui/icons-material";
|
||||
import Logo, { LogoType } from "../../components/Logo";
|
||||
|
||||
import { useEffect } from "react";
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<div className="h-screen w-screen bg-slate-700">
|
||||
|
||||
@@ -1,65 +1,55 @@
|
||||
import { Button, CircularProgress } from "@mui/material";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import Channels from "../../electron/constants";
|
||||
import { CircularProgress } from "@mui/material";
|
||||
import Logo from "../../components/Logo";
|
||||
import { UserInfo } from "@nirvana/core/models";
|
||||
import axios from "axios";
|
||||
import { useGetUserDetails } from "../../controller";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
export default function Login() {
|
||||
const navigate = useNavigate();
|
||||
|
||||
const logIn = () => {
|
||||
setIsLoading(true);
|
||||
// send to main process
|
||||
// ipcRenderer.send(Channels.ACTIVATE_LOG_IN);
|
||||
window.electronAPI.auth.initiateLogin();
|
||||
};
|
||||
|
||||
// useEffect(() => {
|
||||
// window.API.receive(channels.AUTH_TOKENS, async (tokens: any) => {
|
||||
// console.log(tokens);
|
||||
useEffect(() => {
|
||||
window.electronAPI.auth.receiveTokens(async (tokens: any) => {
|
||||
console.log(tokens);
|
||||
|
||||
// const { access_token, id_token, refresh_token } = tokens;
|
||||
const { access_token, id_token, refresh_token } = tokens;
|
||||
|
||||
// // get user info from access token
|
||||
// const userInfo: UserInfo = await (
|
||||
// await fetch(
|
||||
// `https://www.googleapis.com/oauth2/v1/userinfo?access_token=${access_token}`
|
||||
// )
|
||||
// ).json();
|
||||
// home should handle this user now that they have signed in with google
|
||||
navigate("/home");
|
||||
});
|
||||
|
||||
// console.log("user data: ");
|
||||
// console.log(userInfo);
|
||||
// todo: figure out how to clean up with the preload api
|
||||
// return () => {
|
||||
// window.electronAPI.removeAllListeners(Channels.AUTH_TOKENS);
|
||||
// };
|
||||
}, []);
|
||||
|
||||
// // if user is an existing user, then continue to next route
|
||||
// axios({
|
||||
// method: "GET",
|
||||
// url: localHost + `/user`,
|
||||
// headers: {
|
||||
// Authorization:
|
||||
// "eyJhbGciOiJSUzI1NiIsImtpZCI6ImQ2M2RiZTczYWFkODhjODU0ZGUwZDhkNmMwMTRjMzZkYzI1YzQyOTIiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiI0MjM1MzMyNDQ5NTMtYmFubGlnb2JnYm9mOGhnODlpNmNyMWw3dTBwN2MycGsuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJhdWQiOiI0MjM1MzMyNDQ5NTMtYmFubGlnb2JnYm9mOGhnODlpNmNyMWw3dTBwN2MycGsuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMTM0NzA3ODY2OTAzNTMxMDkwODYiLCJlbWFpbCI6InBhdGVsLmFyanVuNTBAZ21haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF0X2hhc2giOiJKRlBBOC15TGgxeGlGOW5acUZQU0FBIiwibmFtZSI6IkFyanVuIFBhdGVsIiwicGljdHVyZSI6Imh0dHBzOi8vbGgzLmdvb2dsZXVzZXJjb250ZW50LmNvbS9hLS9BT2gxNEdqbTI5U2QxV25tOE52WmJYa29zdmY2U29JRDZrQlA1T0hSTFZJT0JRPXM5Ni1jIiwiZ2l2ZW5fbmFtZSI6IkFyanVuIiwiZmFtaWx5X25hbWUiOiJQYXRlbCIsImxvY2FsZSI6ImVuIiwiaWF0IjoxNjQ3Mzc4MzgzLCJleHAiOjE2NDczODE5ODN9.llIYMEjq3Ye1UFxiKIsgtjnRho2Ad7dQjCSFVDDlsiH3AVq_MssoKTXMS0tuHpQAZAfyqbi1kRpp4Ax3mFJb44z2OIyzOTDzhcNB_L_3C8U3gRuSmUaVfBa7EdkobkxByee3ddvjekD7Y9dobtXs3IllldjewD9Eg9FZmEkuSN6yYVxxRLYNYTjk_BAA17QKKkPPpyanAHHXLwxpMFhQc34tuAYCYmmDQQijV8YVvU4r9ruPpysumVXlUmX3CuNg9P415Dq1lUHQxaf5hWf_6RxIsw9PNg1wZ8CuJGvmLFnGBRQACKgnAkI9E7-tu2tWe3glRhORZlQmgRCKqG5xoA",
|
||||
// },
|
||||
// }).then((res) => console.log(res));
|
||||
|
||||
// navigate("/home");
|
||||
|
||||
// // if user is not an existing user, then show create account page
|
||||
// });
|
||||
|
||||
// // return () => {
|
||||
// // ipcRenderer.removeAllListeners(channels.AUTH_TOKEN);
|
||||
// // };
|
||||
// }, []);
|
||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||
|
||||
return (
|
||||
<div className="container flex flex-col space-y-5 justify-center items-center h-screen bg-slate-900">
|
||||
<Logo className="scale-50" />
|
||||
<CircularProgress />
|
||||
|
||||
<Button onClick={logIn} variant="contained">
|
||||
Sign In
|
||||
</Button>
|
||||
{isLoading ? (
|
||||
<>
|
||||
<CircularProgress />
|
||||
<span className="text-white">Attempting to log you in</span>
|
||||
</>
|
||||
) : (
|
||||
<button
|
||||
onClick={logIn}
|
||||
className="text-white p-3 rounded shadow border-white"
|
||||
>
|
||||
Sign In
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user