diff --git a/README.md b/README.md index 444a6a4..23db855 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,13 @@ and share with other, and also Work with a small Community of your Own. Important! This App is in a really early stage and nothing works! +### Add User + +``` +deno run --allow-read --allow-env --allow-write --allow-net --unstable add-user.ts +``` + +### Start Server ``` deno run --unstable --allow-write --allow-read --allow-net --allow-env server.ts diff --git a/add-user.ts b/add-user.ts new file mode 100644 index 0000000..c211411 --- /dev/null +++ b/add-user.ts @@ -0,0 +1,87 @@ +import 'https://deno.land/x/dotenv@v2.0.0/load.ts' +import Ask from 'https://deno.land/x/ask@1.0.6/mod.ts' + +import { Database } from 'https://deno.land/x/aloedb@0.9.0/mod.ts' +import { UserSchema } from './src/stores/user.ts' + +import * as bcrypt from 'https://deno.land/x/bcrypt@v0.2.4/mod.ts' + +// create ask for promt +const ask = new Ask() + +const user = { is_admin: false }; + +// getting email for user +const { email } = await ask.input({ + name: 'email', + message: 'Email:', + validate: (value) => { + return (value) ? true : false + } +}) + +user.email = email + + +// getting password for user +const { password } = await ask.input({ + name: 'password', + message: 'Password:', + validate: (value) => { + return (value) ? true : false + } +}) + +const { passwordRepeat } = await ask.input({ + name: 'passwordRepeat', + message: 'Repeat Password:', + validate: (value) => { + let result = false + + if (value === password) { + result = true + } else { + console.log('Password not match') + } + + return result + } +}) + +user.password = password + + +// is admin +const { isAdmin } = await ask.input({ + name: 'admin', + message: 'Admin (n):', + validate: (value) => { + let result = false + + if (value === 'n' || value === 'y') { + result = true + } else { + console.log('Ony (y)es and (n)o allowed') + } + + return result + } +}) + +user.is_admin = isAdmin + +// getting schema for users +const db = new Database('./storage/database/users.json') + +// search for user by email +const userExists = await db.findOne({ email: email }) + +if (userExists) { + console.log('User already exists') + Deno.exit(1); +} + +// hash password +user.password = await bcrypt.hash(user.password); + +await db.insertOne(user) \ No newline at end of file diff --git a/src/respositories/user.ts b/src/respositories/user.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/stores/user.ts b/src/stores/user.ts index 05bec65..10ee638 100644 --- a/src/stores/user.ts +++ b/src/stores/user.ts @@ -1,6 +1,7 @@ -interface Bucket { +interface UserSchema { _id: string; - username: string; + email: string; password: string; displayname: string; + is_admin: boolean }