electron-google-oauth2 icon indicating copy to clipboard operation
electron-google-oauth2 copied to clipboard

Change successRedirectURL

Open justmedev opened this issue 4 years ago • 5 comments

Is there any non-hacky way to change the successRedirectURL to a custom one or just let it close the window?

Something like: myApiOauth.openAuthWindowAndGetToken(successRedirectURL="https://localhost/?loginresult=done")

justmedev avatar Jan 30 '21 19:01 justmedev

Hey, did you find a solution for this? Thanks

sgeier avatar Aug 14 '21 21:08 sgeier

Nope

justmedev avatar Aug 15 '21 20:08 justmedev

I am also dealing with the same issue. After having a look at the src/index.ts file, I have tried it with

defaultElectronGoogleOAuth2Options.successRedirectURL = 'https://localhost:3000';

but it seems it does not work. Has anyone found a solution for this?

Auclown avatar Sep 02 '21 05:09 Auclown

You look at the constructor signature: constructor(clientId: string, clientSecret: string, scopes: string[], options?: Partial<ElectronGoogleOAuth2Options>);

I do it so:

import ElectronGoogleOAuth2 from '@getstation/electron-google-oauth2';
const envVariables = require('../env-variables.json');


const myApiOauth = new ElectronGoogleOAuth2(
	envVariables.GOOGLE_OAUTH_CLIENT_ID,
	envVariables.GOOGLE_OAUTH_CLIENT_SECRET,
	[
		'openid',
		'profile',
		'email',
		'https://www.googleapis.com/auth/documents',
		'https://www.googleapis.com/auth/drive.file',
		'https://www.googleapis.com/auth/spreadsheets.readonly',
	],
	{
		successRedirectURL: envVariables.OAUTH_REDIRECT_URI + '?app=google',
		refocusAfterSuccess: true,
	},
);

I am spinning up my own local webserver to accept the response.

import http from 'http';
import * as Dropbox from './dropbox';

const port = 5000;

export async function runHttpServer(): Promise<void> {
  await http
    .createServer(function (req, res) {
      res.writeHead(200, { 'Content-Type': 'application/json' });

      const url = req.url;

      // Dropbox specific
      const route = '/oauth';
      if (url && url.indexOf(route) !== -1) {
        const urlSearchParams = new URLSearchParams(url.replace(route, ''));
        const params = Object.fromEntries(urlSearchParams.entries());

        if (params.app === 'dropbox') {
          if (params.code) {
            Dropbox.getAccessTokenFromCode(params.code)
              .then(() => {
                res.write('Thanks for authenticating Dropbox. This page can be closed now.');
                res.end();
              })
              .catch((error) => console.log(error));
          }
        }
        if (params.app === 'google') {
          res.write('Thanks for authenticating Google. This page can be closed now.');
          res.end();
        }
      }

      // Google Auth here.
    })
    .listen(port, function () {
      console.log('[Http] Server started at port', port);
    });
}

sgeier avatar Sep 02 '21 08:09 sgeier

Hi everyone, I just want to share that I found a way to change the successRedirectURL.

Just need to pass it like this.

const myApiOauth = new ElectronGoogleOAuth2(
    process.env.GOOGLE_CLIENT_ID,
    process.env.GOOGLE_CLIENT_SECRET,
    [],
    { successRedirectURL: 'https://google.com' },
);

afrieirham avatar Oct 06 '21 04:10 afrieirham