1- import { app , BrowserWindow , dialog , ipcMain } from "electron" ;
1+ import { app , BrowserWindow , dialog , ipcMain , session } from "electron" ;
22import serve from "electron-serve" ;
33import path from "path" ;
44import { fileURLToPath } from "url" ;
55
66import fs from "fs" ;
7- import { createTerminalServer } from "./lib/node-pty-server.js" ;
87import ignore from "ignore" ;
8+ import { createTerminalServer } from "./lib/node-pty-server.js" ;
99
1010// Change path to "Pulse Editor"
1111app . setName ( "Pulse Editor" ) ;
1212app . setPath (
1313 "userData" ,
14- app . getPath ( "userData" ) . replace ( "pulse-editor-desktop" , "Pulse Editor" )
14+ app . getPath ( "userData" ) . replace ( "pulse-editor-desktop" , "Pulse Editor" ) ,
1515) ;
1616
1717// Get the file path of the current module
@@ -34,7 +34,9 @@ serve({
3434 scheme : "extension" ,
3535} ) ;
3636
37- function createWindow ( ) {
37+ let mainWindow = null ;
38+
39+ function createMainWindow ( ) {
3840 const win = new BrowserWindow ( {
3941 width : 960 ,
4042 height : 600 ,
@@ -47,6 +49,7 @@ function createWindow() {
4749 } ,
4850 icon : path . join ( __dirname , "../shared-assets/icons/electron/pulse_editor" ) ,
4951 } ) ;
52+ mainWindow = win ;
5053
5154 win . menuBarVisible = false ;
5255
@@ -134,7 +137,7 @@ async function listPathContent(uri, options, baseUri = undefined) {
134137 ( file ) =>
135138 ( options ?. include === "folders" && file . isDirectory ( ) ) ||
136139 ( options ?. include === "files" && file . isFile ( ) ) ||
137- options ?. include === "all"
140+ options ?. include === "all" ,
138141 )
139142 // Filter by gitignore
140143 . filter ( ( file ) => {
@@ -263,6 +266,77 @@ function handleGetInstallationPath(event) {
263266 return uri ;
264267}
265268
269+ async function handleLogin ( event ) {
270+ const cookieName = "pulse-editor.session-token" ;
271+
272+ // Use the default session so cookies are shared automatically
273+ const loginWindow = new BrowserWindow ( {
274+ width : 600 ,
275+ height : 700 ,
276+ show : true ,
277+ webPreferences : {
278+ nodeIntegration : false ,
279+ contextIsolation : true ,
280+ session : session . defaultSession , // ← important
281+ } ,
282+ } ) ;
283+
284+ const signinUrl = "https://pulse-editor.com/api/auth/signin" ;
285+ await loginWindow . loadURL ( signinUrl ) ;
286+
287+ const loginSession = loginWindow . webContents . session ;
288+
289+ const interval = setInterval ( async ( ) => {
290+ try {
291+ const cookies = await loginSession . cookies . get ( { name : cookieName } ) ;
292+ if ( cookies . length > 0 ) {
293+ clearInterval ( interval ) ;
294+
295+ // Login successful
296+ loginWindow . close ( ) ;
297+ console . log ( `Login successful, cookie "${ cookieName } " present.` ) ;
298+
299+ // Reload main window
300+ if ( mainWindow ) {
301+ mainWindow . reload ( ) ;
302+ }
303+ }
304+ } catch ( err ) {
305+ console . error ( "Error checking cookie:" , err ) ;
306+ }
307+ } , 1000 ) ;
308+ }
309+
310+ async function handleLogout ( ) {
311+ const mainSession = session . defaultSession ;
312+ const cookieName = "pulse-editor.session-token" ;
313+
314+ try {
315+ const cookies = await mainSession . cookies . get ( { name : cookieName } ) ;
316+
317+ for ( const cookie of cookies ) {
318+ const url = cookie . domain . startsWith ( "." )
319+ ? `https://${ cookie . domain . slice ( 1 ) } ${ cookie . path } `
320+ : `https://${ cookie . domain } ${ cookie . path } ` ;
321+
322+ await mainSession . cookies . remove ( url , cookie . name ) ;
323+ }
324+
325+ console . log ( `Cookie "${ cookieName } " removed. Logout successful.` ) ;
326+
327+ // Reload main window
328+ if ( mainWindow ) {
329+ mainWindow . reload ( ) ;
330+ }
331+
332+ // Return success to renderer
333+ return { success : true } ;
334+ } catch ( err ) {
335+ console . error ( "Error during logout:" , err ) ;
336+ return { success : false , error : err . message } ;
337+ }
338+ }
339+
266340let isCreatedTerminal = false ;
267341function handleCreateTerminal ( event ) {
268342 if ( ! isCreatedTerminal ) {
@@ -303,7 +377,10 @@ app.whenReady().then(() => {
303377
304378 ipcMain . handle ( "create-terminal" , handleCreateTerminal ) ;
305379
306- createWindow ( ) ;
380+ ipcMain . handle ( "login" , handleLogin ) ;
381+ ipcMain . handle ( "logout" , handleLogout ) ;
382+
383+ createMainWindow ( ) ;
307384} ) ;
308385
309386app . on ( "window-all-closed" , ( ) => {
0 commit comments