Skip to content

Commit

Permalink
fix(auth): restore oauth2 flow functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Setch <adam.setch@outlook.com>
  • Loading branch information
setchy committed Aug 11, 2024
1 parent 572e157 commit e7cf991
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 23 deletions.
11 changes: 7 additions & 4 deletions src/electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ const { autoUpdater } = require('electron-updater');
const { updateElectronApp } = require('update-electron-app');

log.initialize();

// TODO: Remove @electron/remote use - see #650
require('@electron/remote/main').initialize();

// Tray Icons
const idleIcon = path.resolve(
`${__dirname}/../../assets/images/tray-idleTemplate.png`,
Expand Down Expand Up @@ -124,6 +120,13 @@ app.whenReady().then(async () => {
mb.on('ready', () => {
mb.app.setAppUserModelId('com.electron.gitify');

/**
* TODO: Remove @electron/remote use - see #650
* GitHub OAuth 2 Login Flows - Enable Remote Browser Window Launch
*/
require('@electron/remote/main').initialize();
require('@electron/remote/main').enable(mb.window.webContents);

// Tray configuration
mb.tray.setToolTip('Gitify');
mb.tray.setIgnoreDoubleClickEvents(true);
Expand Down
34 changes: 16 additions & 18 deletions src/routes/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { KeyIcon, PersonIcon } from '@primer/octicons-react';
import { type FC, useContext, useEffect } from 'react';
import { KeyIcon, MarkGithubIcon, PersonIcon } from '@primer/octicons-react';
import log from 'electron-log';
import { type FC, useCallback, useContext, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { Button } from '../components/buttons/Button';
import { LogoIcon } from '../components/icons/LogoIcon';
Expand All @@ -9,7 +10,7 @@ import { showWindow } from '../utils/comms';

export const LoginRoute: FC = () => {
const navigate = useNavigate();
const { isLoggedIn } = useContext(AppContext);
const { loginWithGitHubApp, isLoggedIn } = useContext(AppContext);

useEffect(() => {
if (isLoggedIn) {
Expand All @@ -18,14 +19,13 @@ export const LoginRoute: FC = () => {
}
}, [isLoggedIn]);

// FIXME: Temporarily disable Login with GitHub (OAuth) as it's currently broken and requires a rewrite - see #485 #561 #747
/* const loginUser = useCallback(async () => {
const loginUser = useCallback(async () => {
try {
await login();
await loginWithGitHubApp();
} catch (err) {
// Skip
log.error('Auth: failed to login with GitHub', err);
}
}, []); */
}, [loginWithGitHubApp]);

return (
<div className="flex flex-1 flex-col items-center justify-center p-4">
Expand All @@ -36,18 +36,16 @@ export const LoginRoute: FC = () => {
</div>

<div className="text-center text-sm font-semibold italic">Login with</div>
{
// FIXME: Temporarily disable Login with GitHub (OAuth) as it's currently broken and requires a rewrite - see #485 #561 #747
/*
<Button

<Button
name="GitHub"
icon={MarkGitHubIcon}
icon={{ icon: MarkGithubIcon }}
label="Login with GitHub"
class="w-50 px-2 py-2 my-2 text-xs"
onClick={loginUser}
/>
*/
}
className="mt-2 py-2"
onClick={() => loginUser()}
>
GitHub
</Button>

<Button
icon={{ icon: KeyIcon }}
Expand Down
7 changes: 6 additions & 1 deletion src/routes/LoginWithOAuthApp.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { BookIcon, PersonIcon, SignInIcon } from '@primer/octicons-react';
import log from 'electron-log';
import { type FC, useCallback, useContext } from 'react';
import { Form, type FormRenderProps } from 'react-final-form';
import { useNavigate } from 'react-router-dom';
import { Header } from '../components/Header';
import { Button } from '../components/buttons/Button';
import { FieldInput } from '../components/fields/FieldInput';
Expand Down Expand Up @@ -58,6 +60,8 @@ export const validate = (values: IValues): IFormErrors => {
};

export const LoginWithOAuthApp: FC = () => {
const navigate = useNavigate();

const { loginWithOAuthApp } = useContext(AppContext);

const renderForm = (formProps: FormRenderProps) => {
Expand Down Expand Up @@ -119,8 +123,9 @@ export const LoginWithOAuthApp: FC = () => {
async (data: IValues) => {
try {
await loginWithOAuthApp(data as LoginOAuthAppOptions);
navigate('/', { replace: true });
} catch (err) {
// Skip
log.error('Auth: Failed to login with oauth app', err);
}
},
[loginWithOAuthApp],
Expand Down
2 changes: 2 additions & 0 deletions src/routes/LoginWithPersonalAccessToken.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BookIcon, KeyIcon, SignInIcon } from '@primer/octicons-react';
import log from 'electron-log';
import { type FC, useCallback, useContext, useState } from 'react';
import { Form, type FormRenderProps } from 'react-final-form';
import { useNavigate } from 'react-router-dom';
Expand Down Expand Up @@ -125,6 +126,7 @@ export const LoginWithPersonalAccessToken: FC = () => {
);
navigate(-1);
} catch (err) {
log.error('Auth: failed to login with personal access token', err);
setIsValidToken(false);
}
},
Expand Down
44 changes: 44 additions & 0 deletions src/routes/__snapshots__/Login.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/utils/auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Constants } from '../constants';
import { getPlatformFromHostname } from '../helpers';
import type { AuthMethod, AuthResponse, AuthTokenResponse } from './types';

// TODO - Refactor our OAuth2 flow to use system browser and local app gitify://callback - see #485 #561
export function authGitHub(
authOptions = Constants.DEFAULT_AUTH_OPTIONS,
): Promise<AuthResponse> {
Expand Down

0 comments on commit e7cf991

Please sign in to comment.