diff --git a/.gitignore b/.gitignore index 7ea34e7..d1ee9ab 100644 --- a/.gitignore +++ b/.gitignore @@ -107,4 +107,5 @@ dist .env*.local # config file for BorgWarehouse -config/repo.json \ No newline at end of file +config/repo.json +config/users.json \ No newline at end of file diff --git a/pages/api/auth/[...nextauth].js b/pages/api/auth/[...nextauth].js index 31daed8..45c08bb 100644 --- a/pages/api/auth/[...nextauth].js +++ b/pages/api/auth/[...nextauth].js @@ -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' );