diff --git a/src/app/main/index.ts b/src/app/main/index.ts index e1579cf..d6127cf 100644 --- a/src/app/main/index.ts +++ b/src/app/main/index.ts @@ -23,8 +23,17 @@ import { handleAuthUri } from './na-auth.js'; const debug = createDebug('app:main'); +export const protocol_registration_options = dev && process.platform === 'win32' ? { + path: process.execPath, + argv: [ + path.join(dir, 'dist', 'app', 'main', 'app-entry.cjs'), + ], +} : null; export const login_item_options: LoginItemSettingsOptions = {}; +debug('Protocol registration options', protocol_registration_options); +debug('Login item registration options', login_item_options); + export class App { readonly store: Store; readonly monitors: PresenceMonitorManager; @@ -141,13 +150,8 @@ export async function init() { } }); - if (dev && process.platform === 'win32') { - app.setAsDefaultProtocolClient('com.nintendo.znca', process.execPath, [ - path.join(dir, 'dist', 'app', 'main', 'app-entry.cjs'), - ]); - } else { - app.setAsDefaultProtocolClient('com.nintendo.znca'); - } + app.setAsDefaultProtocolClient('com.nintendo.znca', + protocol_registration_options?.path, protocol_registration_options?.argv); app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) appinstance.showMainWindow(); @@ -175,6 +179,8 @@ export async function init() { } function tryHandleUrl(app: App, url: string) { + debug('Attempting to handle URL', url); + if (url.match(/^npf[0-9a-f]{16}:\/\/auth($|\?|\#)/i)) { handleAuthUri(url); return true; diff --git a/src/app/main/na-auth.ts b/src/app/main/na-auth.ts index df769d6..ec2f1cb 100644 --- a/src/app/main/na-auth.ts +++ b/src/app/main/na-auth.ts @@ -13,6 +13,7 @@ import { tryGetNativeImageFromUrl } from './util.js'; import { ZNCA_API_USE_URL } from '../../common/constants.js'; import { createWindow } from './windows.js'; import { WindowType } from '../common/types.js'; +import { protocol_registration_options } from './index.js'; const debug = createDebug('app:main:na-auth'); @@ -256,14 +257,18 @@ export function getSessionTokenCodeByDefaultBrowser( if (force_manual) { debug('Manual entry forced, prompting for redirect URI'); window = askUserForRedirectUri(authoriseurl, client_id, handleAuthUrl, rj); - } else if (app.isDefaultProtocolClient(protocol)) { + } else if (app.isDefaultProtocolClient(protocol, + protocol_registration_options?.path, protocol_registration_options?.argv + )) { debug('App is already default protocol handler, opening browser'); auth_state.set(state, [handleAuthUrl, rj, protocol]); shell.openExternal(authoriseurl); } else { const registered_app = app.getApplicationNameForProtocol(protocol); - if (registered_app || !app.setAsDefaultProtocolClient(protocol)) { + if (registered_app || !app.setAsDefaultProtocolClient(protocol, + protocol_registration_options?.path, protocol_registration_options?.argv + )) { debug('Another app is using the auth protocol or registration failed, prompting for redirect URI'); window = askUserForRedirectUri(authoriseurl, client_id, handleAuthUrl, rj); } else { @@ -298,7 +303,8 @@ export function handleAuthUri(url_string: string) { app.on('quit', () => { for (const [,, protocol] of auth_state.values()) { - app.removeAsDefaultProtocolClient(protocol); + app.removeAsDefaultProtocolClient(protocol, + protocol_registration_options?.path, protocol_registration_options?.argv); } });