A complete example application demonstrating URL protocol handling with Node.js and Electron. This application showcases the authentication flow where users can start the process in a desktop app, complete authentication in their browser, and seamlessly return to the desktop application with authentication parameters.
- Desktop Application: Built with Electron for cross-platform compatibility
- Web Server: Express.js server for handling authentication flow
- Custom URL Protocol:
myapp://
protocol handler registration - Authentication Flow: Complete OAuth-like flow with browser redirect
- Windows Registry Integration: Automatic protocol registration during installation
- Session Management: Secure session handling with timeout
- Real-time Status Updates: Polling-based status checking
- Node.js 16+
- npm or yarn
- Windows, macOS, or Linux
- Clone the repository:
git clone <repository-url>
cd Exmaple-url-protocol-and-handler-application-
- Install dependencies:
npm install
- Start the application in development mode:
npm run dev
This will:
- Start the Express.js server on
http://localhost:3000
- Launch the Electron desktop application
- Enable hot-reload for development
- Build the application:
npm run build
- Create distribution package:
npm run dist
The built application will be in the dist/
directory.
- Start Flow: User clicks "Start Authentication Flow" in the desktop app
- Browser Opens: Default browser opens to the authentication page
- User Login: User enters credentials on the web page
- Protocol Redirect: Browser redirects to
myapp://auth/callback?token=...&user=...
- App Handles: Desktop app receives the protocol URL and processes the authentication
- Success: User is authenticated and returned to the desktop app
The application registers the myapp://
protocol and handles these URL patterns:
myapp://test/basic
- Basic protocol testmyapp://auth/callback?token=<token>&user=<user_json>
- Authentication callback
myapp://test/basic
myapp://auth/callback?token=abc123&user={"id":"123","username":"john","email":"john@example.com"}
βββ src/
β βββ electron/ # Electron main process
β β βββ main.js
β βββ server/ # Express.js server
β β βββ server.js
β βββ web/ # Web interface
β βββ index.html
βββ assets/ # Application assets
βββ dist/ # Build output
βββ package.json # Project configuration
βββ register-protocol.reg # Windows registry file (dev)
βββ README.md
The protocol is configured in package.json
under the build.protocols
section:
{
"protocols": {
"name": "myapp-protocol",
"schemes": ["myapp"]
}
}
Default server runs on http://localhost:3000
. You can modify this in src/server/server.js
.
The application now supports GitHub OAuth authentication. To set this up:
-
Create a GitHub OAuth App:
- Go to GitHub Settings β Developer settings β OAuth Apps
- Click "New OAuth App"
- Set the following values:
- Application name: "Your App Name"
- Homepage URL:
http://localhost:3000
- Authorization callback URL:
http://localhost:3000/auth/github/callback
- Save the Client ID and Client Secret
-
Environment Configuration:
- Copy
.env.example
to.env
- Replace the demo values with your actual GitHub OAuth credentials:
GITHUB_CLIENT_ID=your_actual_client_id GITHUB_CLIENT_SECRET=your_actual_client_secret SESSION_SECRET=your_random_secure_secret
- Copy
Note: The app will work with demo credentials for testing, but real GitHub authentication requires proper OAuth app setup.
GET /
- Main application interfaceGET /auth/start
- Start authentication flowGET /auth/login
- Mock login pageGET /auth/callback
- Web callback (fallback)GET /auth/status/:sessionId
- Check authentication statusGET /health
- Server health check
For development, you can manually register the protocol using register-protocol.reg
:
- Edit the file to point to your built application
- Double-click to import into Windows Registry
For production, electron-builder handles this automatically during installation.
- Start the application:
npm run dev
- Click "Login with GitHub"
- Complete the GitHub OAuth flow in the browser
- Verify the desktop app receives the callback with GitHub user data
For testing GitHub OAuth:
- Set up proper GitHub OAuth credentials in
.env
- Start the app:
npm run dev
- Click "Login with GitHub"
- Authorize the app on GitHub
- Verify user profile and GitHub API access work
Test the protocol directly by:
- Opening a browser
- Entering
myapp://test/basic
in the address bar - Verifying the desktop app responds
- Ensure the desktop app is running
- Check if protocol is registered (Windows Registry)
- Try reinstalling/rebuilding the application
- Check if port 3000 is available
- Look for error messages in the console
- Verify Node.js installation
- Check browser console for errors
- Verify server is running on localhost:3000
- Check session timeout (30 minutes default)
- GitHub OAuth Integration: The application now uses real GitHub OAuth instead of mock authentication
- Demo Mode: Works with demo credentials for testing (see
.env.example
) - Production Ready: Real GitHub OAuth support with proper session management
- Session Storage: In-memory storage for development; use Redis or database for production
- Icons: Add proper app icons to
assets/
directory for branding - Environment Variables: Never commit
.env
file with real credentials
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
MIT License - see LICENSE file for details.