feat: add users.json to .gitignore #12

This commit is contained in:
bsourisse 2022-12-22 10:15:16 +01:00
parent cda9818225
commit d43389f395
2 changed files with 20 additions and 3 deletions

3
.gitignore vendored
View File

@ -107,4 +107,5 @@ dist
.env*.local
# config file for BorgWarehouse
config/repo.json
config/repo.json
config/users.json

View File

@ -2,7 +2,7 @@
import NextAuth from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import { verifyPassword } from '../../../helpers/functions/auth';
import { promises as fs } from 'fs';
import fs from 'fs';
import path from 'path';
////Use if need getServerSideProps and therefore unstable_getServerSession
@ -14,7 +14,23 @@ export const authOptions = {
//Read the users file
//Find the absolute path of the json directory
const jsonDirectory = path.join(process.cwd(), '/config');
let usersList = await fs.readFile(
//Check if the users.json file exists and initialize it if not with admin/admin.
if (!fs.existsSync(jsonDirectory + '/users.json')) {
fs.writeFileSync(
jsonDirectory + '/users.json',
JSON.stringify([
{
id: 0,
email: 'admin@demo',
username: 'admin',
password:
'$2a$12$20yqRnuaDBH6AE0EvIUcEOzqkuBtn1wDzJdw2Beg8w9S.vEqdso0a',
roles: ['admin'],
},
])
);
}
let usersList = await fs.promises.readFile(
jsonDirectory + '/users.json',
'utf8'
);