我用 postgres 设置了一个基本的 luminus 模板。我希望能够添加新用户并对其进行身份验证,遵循本书第 6 章第 168 章的示例:https ://pragprog.com/titles/dswdcloj3/web-development-with-clojure-third-edition /
我遇到了问题,所以我重新开始了一个新项目,试图通过这部分。
我创建了一个新的 luminus 项目,创建了数据库表,并更新了 project.clj 文件。当我尝试创建测试用户时出现错误。
我已经完成了以下命令:
lein new luminus testproject --template-version 3.91 -- +postgres +http-kit
cd testproject
lein repl
在另一个终端,我做
psql
CREATE USER testproject WITH PASSWORD 'password';
CREATE DATABASE testproject WITH OWNER testproject;
我更新了 dev_config.edn 文件:
{:dev true
:port 3000
:nrepl-port 7000
:database-url "postgresql://localhost:5432/testproject?user=testproject&password=password"
}
然后我做了
lein repl
(start)
(create-migration "add-users-table")
(migrate)
(in-ns 'testproject)
(create-user! "testuser" "testpass")
我收到此错误:
Unable to resolve symbol: create-user! in this context
我编辑/创建的文件:project.clj(添加好友)
auth.clj:
(ns testproject.core
(:require
[buddy.hashers :as hashers]
[next.jdbc :as jdbc]
[testproject.db.core :as db]))
(defn create-user! [login password]
(jdbc/with-transaction [t-conn db/*db*]
(if-not (empty? (db/get-user-for-auth* t-conn {:login login}))
(throw (ex-info "User already exists!"
{:testproject/error-id ::duplicate-user
:error "User already exists!"}))
(db/create-user!* t-conn
{:login login
:password (hashers/derive password)}))))
(defn authenticate-user [login password]
(let [{hashed :password :as user} (db/get-user-for-auth* {:login login})]
(when (hashers/check password hashed)
(dissoc user :password))))
查询.sql:
-- :name create-user!* :! :n
-- :doc creates a new user with the provided login and hashed password
INSERT INTO users
(login, password)
VALUES (:login, :password)
-- :name get-user-for-auth* :? :1
-- :doc selects a user for authentication
SELECT * FROM users
WHERE login = :login
迁移:添加用户表:
CREATE TABLE users
(login text PRIMARY KEY,
password text not null,
created_at TIMESTAMP not null DEFAULT now());
向下添加用户表:
DROP TABLE users;
我能够添加消息/帖子就好了。对于这个测试项目,我只是想让新用户隔离问题。
我究竟做错了什么?我应该注意哪些部分?
谢谢。