As seen above, the information returned by the server (if the user has been authenticated) will be saved in a .json file (in the electronjs context). We will save the « name of this file » in the configuration file specific to the environment used. Remember that we have chosen the 3 following environments:
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);
let data = {};
data[key] = content;
this.electron.fs.writeFileSync(this.configFile, JSON.stringify(data));
return;
localStorage.setItem(key, JSON.stringify(content));
Line 2-7 checks if we are in the electronjs context. If so, the information returned by the server (including the token) will be recorded in the json C:\Users\~AppData\Roaming\angular-electron\token.json .This is possible thanks to the API of electronjs which makes it possible to save a file on the machine. This API simply uses the fs API of nodejs.