Angular-Electronjs — Token registration using the fs API from Nodejs

  • Creation of a shared storageService that records the information returned by the server:
  • either localStorage (in the webapp context)
  • or in a json file (in the electronjs context)
  • Replacing calls from localStorage .
ng g service providers/storage
  • angular-electron/src/app/providers/storage.service.ts
  • angular-electron/src/app/providers/storage.service.spec.ts
import { StorageService } from './storage.service'; import { ElectronService } from './electron.service';
public storage: StorageService;
constructor(private http: HttpClient, private electron: ElectronService)
export const AppConfig = { production: false, environment: 'LOCAL', apiUrl: 'http://localhost:3008/api', configFile: 'token.json' };
this.electron.remote.app.getPath('userData') + '/' + AppConfig.configFile
import { Injectable } from '@angular/core'; import {ElectronService} from "./electron.service"; import { AppConfig } from '../../environments/environment'; @Injectable({ providedIn: 'root' }) export class StorageService { isElectron: boolean; confExists: boolean; electron: ElectronService; configFile: string; constructor(electron: ElectronService) { this.electron = electron; if (this.electron.isElectron()) { this.initElectron(); } } save(key: string, content: string) { if (this.isElectron) { let data = {}; data[key] = content; this.electron.fs.writeFileSync(this.configFile, JSON.stringify(data)); return; } localStorage.setItem(key, JSON.stringify(content)); } remove(key: string) { if (this.isElectron) { // Remove file this.electron.fs.unlinkSync(this.configFile); return; } localStorage.removeItem(key); } get(key: string) { if (this.isElectron && this.electron.fs.existsSync(this.configFile)) { let jsonContents = this.electron.fs.readFileSync(this.configFile, "utf8"); jsonContents = JSON.parse(jsonContents); return JSON.stringify(jsonContents[key]); } return localStorage.getItem(key); } private initElectron() { this.isElectron = true; this.configFile = this.electron.remote.app.getPath('userData') + '/' + AppConfig.configFile; this.confExists = this.electron.fs.existsSync(this.configFile); } }

save

If we look closely at the save method that saves the information returned by the server.

save(key: string, content: string) { if (this.isElectron) { let data = {}; data[key] = content; this.electron.fs.writeFileSync(this.configFile, JSON.stringify(data)); return; } localStorage.setItem(key, JSON.stringify(content)); }
this.electron.fs.writeFileSync(this.configFile, JSON.stringify(data));

remove

The remove method is slightly different depending on the context:

  • electronjs: the file C:\Users\~AppData\Roaming\angular-electron\token.json will be deleted.
  • webapplication: the localstorage containing the user key will be deleted.

get

The get method is similar in both contexts:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store