Created
November 16, 2020 00:48
-
-
Save alancasagrande/065636333ff494dbb4e858b05b4b9ca6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { isArray } from 'util'; | |
| const fs = require('fs'); | |
| const storagePath = './storage'; | |
| const usersPath = `${storagePath}/users.json`; | |
| const initialUsers = { | |
| alan: { | |
| username: 'alan', | |
| password: '1', | |
| }, | |
| jhon: { | |
| username: 'jhon', | |
| password: '2', | |
| }, | |
| }; | |
| export function initStorage() { | |
| if (!fs.existsSync(storagePath)) fs.mkdirSync(storagePath); | |
| if (!fs.existsSync(usersPath)) setUsers(initialUsers); | |
| } | |
| export function getUser(username) { | |
| return getUsers()[username]; | |
| } | |
| export function setUser(user) { | |
| const users = getUsers(); | |
| users[user.username] = user; | |
| setUsers(users); | |
| } | |
| function getUsers() { | |
| let users; | |
| if (fs.existsSync(usersPath)) { | |
| users = JSON.parse(fs.readFileSync(usersPath).toString()); | |
| } | |
| return users || {}; | |
| } | |
| function setUsers(users) { | |
| fs.writeFileSync(usersPath, JSON.stringify(users, null, 2)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment