Shile
Shile

Reputation: 1063

Datomic with friend authentication not working properly

I'm working on a clojure web application project for my college,and i am trying to connect datomic database with friend authentication but is kinda buggy...i will explain further...

First i am doing user registration(user insertion in datomic database) like this and it is working.

 (defn insert-user [firstname lastname email password sex date] (.get (.transact conn
                            [{
                           :db/id #db/id[:db.part/user -1000001]
                           :user/name firstname
                           :user/lastName lastname
                           :user/username email
                           :user/password (creds/hash-bcrypt password)
                           :user/gender sex
                           :user/birthDate date}
                         ]))

(resp/redirect "/")
)

The routes handler and friend authenticator looks like this...function main is to start the app.

(def page (handler/site
        (friend/authenticate
          routes

          {:allow-anon? true
           :login-uri "/login"
           :default-landing-uri "/login"
           :unauthorized-handler #(-> (html5 [:h2 "You do not have sufficient privileges to access " (:uri %)])
                                    resp/response
                                    (resp/status 401))
           :credential-fn (partial creds/bcrypt-credential-fn users)
           :workflows [(workflows/interactive-form)]})

           (wrap-keyword-params routes)
           (wrap-nested-params routes)
           (wrap-params routes)

))

(defn -main []
(run-jetty page {:port 8080 :join? false}))

And for the end the datomic query for users to match with creds/bcrypt-credential-fn function of friend.

(defn upit-korisnici [] 
(def temp (d/q '[:find ?u ?p
         :where [?user :user/username ?u]
                [?user :user/password ?p]
         ]
        (d/db conn)))
(def users (into {} (map (fn [[k v]] [k {:username k :password v}]) temp)))
users
)

The thing that is bugging me and leaving me helpless is that when i register(insert user),the user is inserted in datomic database but when i try to log in i can't.It says wrong email and password but the new user is there.When i restart the whole app and try to login with the new users credentials it goes through and logs on.Does anyone know how to solve this problem?

Edit:

I solved this problem by changing :credential-fn (partial creds/bcrypt-credential-fn users) to :credential-fn #(creds/bcrypt-credential-fn users %).

Upvotes: 0

Views: 298

Answers (2)

sveri
sveri

Reputation: 1389

I started working on an UI for the friend lib with datomic persistence: https://github.com/sveri/friend-ui/ You can have a look at it, maybe it solves your problem already, of course you can take code from it, add pull requests / whatever. When I have time I will implement whatever is needed.

Currently it supports:

  • Sign up
  • Login
  • Logout
  • Twitter Bootstrap support for the templates

It would be nice if we could bundle the work done, as this will be something that many people will need in future.

Upvotes: 0

stonemetal
stonemetal

Reputation: 6208

You seem to think it will automatically update your user data, but it isn't going to because user is not a function it is plain data. What happens is (def user... is run then the results are bound to the name user, you are not binding the computation so the data is never updated. You make a similar mistake for temp. The query is run once then the results are bound to temp, and never reevaluated. You should bind them to a function so that it revaluates.

Upvotes: 1

Related Questions