Home > Web Front-end > JS Tutorial > Build a Secure Desktop App with Electron Forge and React

Build a Secure Desktop App with Electron Forge and React

Lisa Kudrow
Release: 2025-02-10 08:47:08
Original
369 people have browsed it

This article demonstrates building a simple, secure text editor desktop application using Electron and React, leveraging Electron Forge for streamlined development and security. The app, dubbed "scratchpad," autosaves changes as you type, mirroring the functionality of FromScratch. We'll focus on secure coding practices throughout.

Build a Secure Desktop App with Electron Forge and React

Key Concepts:

  • Electron Forge: A comprehensive tool for creating, publishing, and installing modern Electron applications, providing a secure and efficient development environment.
  • Main vs. Renderer Processes: Electron applications consist of a main process (Node.js) managing OS interactions and window creation, and renderer processes (Chromium) handling UI rendering.
  • React Integration: We'll integrate React into the renderer process for a smooth development experience.
  • CodeMirror: A powerful text editor component enhancing the user interface and providing real-time updates.
  • Secure File Handling: We'll employ Electron's main and renderer processes and a preload script to securely save and load content from the disk.
  • Preventing White Flash: Window settings will be adjusted to eliminate the initial white flash on application launch.
  • Packaging and Distribution: Electron Forge simplifies the process of packaging and distributing the application across various operating systems.

Development Setup:

This tutorial assumes Node.js and Git are installed. We'll use Electron Forge with a webpack template for efficient React integration. The project is initialized with:

npx create-electron-app scratchpad --template=webpack
Copy after login
Copy after login

This creates the project structure, including webpack.main.config.js, webpack.renderer.config.js, webpack.rules.js, and the src directory containing initial HTML, CSS, and JavaScript files.

Adding React:

Install necessary dependencies:

npm install --save-dev @babel/core @babel/preset-react babel-loader
npm install --save react react-dom
Copy after login

Configure webpack to support JSX by adding a Babel loader to webpack.rules.js:

module.exports = [
  // ...
  {
    test: /\.jsx?$/,
    use: {
      loader: 'babel-loader',
      options: {
        exclude: /node_modules/,
        presets: ['@babel/preset-react']
      }
    }
  },
  // ...
];
Copy after login

Test the React integration by replacing src/renderer.js and creating src/app.jsx as described in the original article. Running npm start should display "Hello from React in Electron!".

Building the Scratchpad:

Install CodeMirror and react-codemirror:

npx create-electron-app scratchpad --template=webpack
Copy after login
Copy after login

Import necessary CSS into src/renderer.js and implement the ScratchPad component in src/app.jsx using CodeMirror, handling updates and styling as detailed in the original article. Adjust index.html and index.css to remove unnecessary elements and improve styling.

Secure Disk Saving and Loading:

Add file system handling to the main process (main.js) using fs. Create loadContent and saveContent functions to read from and write to a file located in the application's data directory (app.getPath('userData')).

Implement Inter-Process Communication (IPC) using ipcMain in main.js and ipcRenderer in a newly created preload.js file to securely handle communication between the main and renderer processes. The preload.js script acts as a secure bridge, exposing only necessary functions to the renderer.

Modify the ScratchPad component to use window.scratchpad.saveContent for saving and window.scratchpad.content (using ipcRenderer.invoke) for loading initial content. Wrap the ReactDOM.render call in an async function to handle the promise returned by window.scratchpad.content.

Optimizing Loading and Building:

Set show: false in BrowserWindow creation and add a ready-to-show event listener to improve the loading experience, preventing the initial white flash. Remove mainWindow.webContents.openDevTools().

Finally, build and package the application using npm run make. Electron Forge will generate installers for your operating system.

This revised response provides a more concise and organized explanation of the original article, maintaining the core functionality and security aspects while improving readability and clarity. The key improvements include clearer section headings, improved formatting, and a more streamlined explanation of the IPC mechanism and secure file handling.

The above is the detailed content of Build a Secure Desktop App with Electron Forge and React. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template