nice flow not buggy with listeners doubling up

This commit is contained in:
talksik
2022-05-28 10:15:53 -05:00
parent 84b67c2c62
commit d843e0f130
7 changed files with 175 additions and 35 deletions
+4 -4
View File
@@ -1,5 +1,5 @@
import { Dimensions, DimensionChangeRequest } from "./constants";
import { electronAPI } from "./preload";
import { Dimensions, DimensionChangeRequest } from './constants';
import { electronAPI } from './preload';
export {};
declare global {
@@ -15,8 +15,8 @@ declare global {
window: {
resizeWindow(dimensionChangeRequest: DimensionChangeRequest): void;
};
on(channel: Channels, func: any): void;
once(channel: Channels, func: any): void;
on(channel: Channels, func: any): () => void;
once(channel: Channels, func: any): () => void;
};
}
}
+7 -1
View File
@@ -24,13 +24,14 @@ const electronAPI = {
},
on(channel: Channels, func: any) {
const validChannels = ['ipc-example'];
// const validChannels = ['ipc-example'];
// 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));
return () => ipcRenderer.removeAllListeners(channel);
},
once(channel: Channels, func: any) {
// const validChannels = ["ipc-example"];
@@ -40,6 +41,11 @@ const electronAPI = {
// }
ipcRenderer.once(channel, (event, ...args) => func(...args));
return () => ipcRenderer.removeAllListeners(channel);
},
removeListener(channel: Channels) {
ipcRenderer.removeAllListeners(channel);
},
};
+5
View File
@@ -0,0 +1,5 @@
import Store from 'electron-store';
const store = new Store();
export default store;
+11
View File
@@ -2,6 +2,8 @@ import { app, BrowserWindow, ipcMain, Display, screen } from 'electron';
import Channels, { DEFAULT_APP_PRESET, DimensionChangeRequest } from './electron/constants';
import { handleGoogleLogin } 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).
@@ -48,6 +50,15 @@ app
.whenReady()
.then(createWindow)
.then(() => {
// access storage/cookies
ipcMain.on('electron-store-set', async (event, key, val) => {
store.set(key, val);
});
ipcMain.handle('electron-store-get', async (event, val) => {
const result = await store.get(val);
return result;
});
// activate login
ipcMain.on(Channels.ACTIVATE_LOG_IN, async (event, arg) => {
console.log('initiating log in');
+52 -27
View File
@@ -6,7 +6,12 @@ import { FcGoogle } from 'react-icons/fc';
import { blueGrey } from '@mui/material/colors';
import Channels from '../electron/constants';
import { firebaseAuth } from '../firebase/connect';
import { GoogleAuthProvider, signInWithCredential } from 'firebase/auth';
import {
GoogleAuthProvider,
signInWithCredential,
setPersistence,
browserLocalPersistence,
} from 'firebase/auth';
import { useSnackbar } from 'notistack';
const provider = new GoogleAuthProvider();
@@ -23,37 +28,57 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
const { enqueueSnackbar } = useSnackbar();
useEffect(() => {
window.electronAPI.once(Channels.GOOGLE_AUTH_TOKENS, async (tokens: Credentials) => {
console.log('got tokens', tokens);
const tokenListener = window.electronAPI.once(
Channels.GOOGLE_AUTH_TOKENS,
async (tokens: Credentials) => {
console.log('got tokens', tokens);
const credential = GoogleAuthProvider.credential(tokens.id_token);
const credential = GoogleAuthProvider.credential(tokens.id_token);
// Sign in with credential from the Google user.
signInWithCredential(firebaseAuth, credential)
.then((result) => {
// This gives you a Google Access Token. You can use it to access Google APIs.
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
setPersistence(firebaseAuth, browserLocalPersistence)
.then(() => {
// Sign in with credential from the Google user.
return signInWithCredential(firebaseAuth, credential)
.then((result) => {
// This gives you a Google Access Token. You can use it to access Google APIs.
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
// The signed-in user info.
const user = result.user;
console.log(user);
// The signed-in user info.
const user = result.user;
console.log(user);
enqueueSnackbar('Signed in! All set!', { variant: 'success' });
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.email;
// The credential that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
// ...
enqueueSnackbar('Signed in! All set!', { variant: 'success' });
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.email;
// The credential that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
// ...
enqueueSnackbar('Something went wrong', { variant: 'error' });
});
});
enqueueSnackbar('Something went wrong', { variant: 'error' });
});
})
.catch((error: any) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.email;
// The credential that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
// ...
enqueueSnackbar('Something went wrong', { variant: 'error' });
});
},
);
return () => tokenListener();
}, []);
return <Login />;