You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
1.8 KiB

3 years ago
  1. import 'https://deno.land/x/dotenv@v2.0.0/load.ts'
  2. import Ask from 'https://deno.land/x/ask@1.0.6/mod.ts'
  3. import { Database } from 'https://deno.land/x/aloedb@0.9.0/mod.ts'
  4. import { UserSchema } from './src/stores/user.ts'
  5. import * as bcrypt from 'https://deno.land/x/bcrypt@v0.2.4/mod.ts'
  6. // create ask for promt
  7. const ask = new Ask()
  8. const user = <UserSchema>{ is_admin: false };
  9. // getting email for user
  10. const { email } = await ask.input({
  11. name: 'email',
  12. message: 'Email:',
  13. validate: (value) => {
  14. return (value) ? true : false
  15. }
  16. })
  17. user.email = email
  18. // getting password for user
  19. const { password } = await ask.input({
  20. name: 'password',
  21. message: 'Password:',
  22. validate: (value) => {
  23. return (value) ? true : false
  24. }
  25. })
  26. const { passwordRepeat } = await ask.input({
  27. name: 'passwordRepeat',
  28. message: 'Repeat Password:',
  29. validate: (value) => {
  30. let result = false
  31. if (value === password) {
  32. result = true
  33. } else {
  34. console.log('Password not match')
  35. }
  36. return result
  37. }
  38. })
  39. user.password = password
  40. // is admin
  41. const { isAdmin } = await ask.input({
  42. name: 'admin',
  43. message: 'Admin (n):',
  44. validate: (value) => {
  45. let result = false
  46. if (value === 'n' || value === 'y') {
  47. result = true
  48. } else {
  49. console.log('Ony (y)es and (n)o allowed')
  50. }
  51. return result
  52. }
  53. })
  54. user.is_admin = isAdmin
  55. // getting schema for users
  56. const db = new Database<UserSchema>('./storage/database/users.json')
  57. // search for user by email
  58. const userExists = await db.findOne({ email: email })
  59. if (userExists) {
  60. console.log('User already exists')
  61. Deno.exit(1);
  62. }
  63. // hash password
  64. user.password = await bcrypt.hash(user.password);
  65. await db.insertOne(user)