chr15m / sitefox

Node + cljs backend web framework
https://chr15m.github.io/sitefox/
MIT License
283 stars 7 forks source link

Add set-user-password nbb script to Sitefox #25

Open chr15m opened 1 year ago

chr15m commented 1 year ago

Need to package this up so it's available when Sitefox + nbb are installed.

(ns set-user-password
  (:require
    [applied-science.js-interop :as j]
    [promesa.core :as p]
    [sitefox.auth :refer [hash-password save-user]]
    [sitefox.db :refer [f]]))

(defn change-password [password email]
  (p/let [users (f "users" (fn [u] (= (j/get-in u [:auth :email]) email)))
          user (first users)
          [hashed-password salt] (hash-password password)
          user (when user (j/assoc! user :auth (clj->js {:email email
                                                         :password hashed-password
                                                         :salt salt})))]
    (if user
      (save-user user)
      (print
        "User not found.\n"
        "Usage: set-user-password EMAIL NEW-PASSWORD"))))

(change-password (.pop js/process.argv) (.pop js/process.argv))