Fix auth protocol registration on Windows when running from source

This commit is contained in:
Samuel Elliott 2022-09-15 22:12:14 +01:00
parent 99f263cef8
commit 6310485db7
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
2 changed files with 22 additions and 10 deletions

View File

@ -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;

View File

@ -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);
}
});