Browse Source

adding

master
Björn 3 years ago
parent
commit
7ada20573f
4 changed files with 97 additions and 2 deletions
  1. +7
    -0
      README.md
  2. +87
    -0
      add-user.ts
  3. +0
    -0
      src/respositories/user.ts
  4. +3
    -2
      src/stores/user.ts

+ 7
- 0
README.md View File

@ -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! 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 deno run --unstable --allow-write --allow-read --allow-net --allow-env server.ts

+ 87
- 0
add-user.ts View File

@ -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 = <UserSchema>{ 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<UserSchema>('./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)

+ 0
- 0
src/respositories/user.ts View File


+ 3
- 2
src/stores/user.ts View File

@ -1,6 +1,7 @@
interface Bucket {
interface UserSchema {
_id: string; _id: string;
username: string;
email: string;
password: string; password: string;
displayname: string; displayname: string;
is_admin: boolean
} }

Loading…
Cancel
Save