Electron app cloud with Virtualization/server

22 Views Asked by At

I have a basic electron app that I created, it is used to open windows and do google oauth2(see code below) and returns a refresh token. Is there a way I can have it run in the browser using some sort of virtualization, I have a server that can host it.( I looked into other [options][1] but they havennt worked. here is my code if that helps

const { app, BrowserWindow,clipboard } = require('electron')

let authWindow;

function createAuthWindow() {

  authWindow = new BrowserWindow({
    width: 800,
    height: 600,
    show: false,
    'node-integration': false
  });
  
  let googleAuthURL = 'https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&redirect_uri=URI%3A%2F%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile%20openid&response_type=code&client_id=CLIENTID';
  
  authWindow.loadURL(googleAuthURL);
  authWindow.show();

  authWindow.webContents.on('will-navigate', function (event, url) {
    handleCallback(url);
  });

  authWindow.webContents.on('did-get-redirect-request', function (event, oldUrl, newUrl) {
    handleCallback(newUrl);
  });
}

function handleCallback(url) {
 let raw_code = /code=([^&]*)/.exec(url) || null;
 let code = (raw_code && raw_code.length > 1) ? raw_code[1] : null;
 let error = /\?error=(.+)$/.exec(url);

  if (code || error) {
    // Close the browser if code found or error
    authWindow.destroy();
  }

 if (code) {
   // This is where you should send the code to your server to exchange for a access token
     console.log('Code: ', code);
     const options = {
  method: 'POST',
  headers: {
    accept: '*/*',
    'content-type': 'application/x-www-form-urlencoded',
    'user-agent': ,
    'accept-language': 'en-US,en;q=0.9'
  },
  body: new URLSearchParams({
    grant_type: 'authorization_code',
    code: code,
    client_id: ,
    redirect_uri: 
  })
};
     fetch('https://oauth2.googleapis.com/token', options)
         .then(response => response.json())
         .then(response => {
          clipboard.writeText(JSON.stringify(response.refresh_token))
  
     
             // Create a new window to display the refresh token
             tokenWindow = new BrowserWindow({
                 width: 800,
                 height: 600,
                 show: false,
                 'node-integration': false
             });

             // Load the refresh token into the new window
 

             tokenWindow.loadURL(`data:text/html,
   <h1>Login token has been copied to clipboard</h1><p>here it is : <br> ${JSON.stringify(response.refresh_token)}</p>
   `);
             tokenWindow.show();
         }).catch(error => {
                console.log(error);
            });
 } else if (error) {
   alert('Oops! Something went wrong and we couldn\'t log you in using Google. Please try again.');
 }
}

app.whenReady().then(createAuthWindow)

app.on('window-all-closed', () => {
 if (process.platform !== 'darwin') {
   app.quit()
 }
})

app.on('activate', () => {
 if (BrowserWindow.getAllWindows().length === 0) {
   createAuthWindow()
 }
})
0

There are 0 best solutions below