Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempting to Create a User Login for Clojure / Korma / PostgreSQL site

I am completely stuck on where to start with getting a log-in area for a Clojure site I am building (for fun).

I've looked at several resources, which I'll post below, mercilessly copy/pasted code, and the closest I can get is one of two situations:

The login page takes the login but says that the login failed, though, as far as I can tell, the login matches.

Or I get this error: No method in multimethod '->sql' for dispatch value: null

I'm not sure how to interpret the above error: is this specifying that I need a multi-method or is it specifying that I need to check for null? The null requirement makes no sense at all. I'm not really asking but if anyone wants to give an explanation, that is great.

I tested the output by comparing the results-to-select queries from raw non-hashed data, I've went through 5 variations on this theme, using everything from page-to-page calls to creating new defpartials, multi-methods, defn, etc.

Sources I have used (unfortunately, I can't list all of them being a first-time poster):

This one uses Clojure -> Korma -> PostgreSQL, but the code doesn't seem to work for multiple users?
http://www.vijaykiran.com/2012/01/17/web-application-development-with-clojure-part-2/

This one shows how to use Noir and PostgreSQL (Yes, I am using Noir): https://yogthos.net:11794/blog/23-Noir+tutorial+-+part+2

The 4Clojure site, but that one uses CongoMongo:

The Heroku Twitter clone, but no mention of how to create logins for one person, much less several.

I also bought Programming Clojure from O'Reilly Press, but once again, nothing about how to create a log-in area.

FIRST EDIT: I was asked to create a github repository of a stand-alone site. This includes a working "Account Creation" area that is found in the welcome.clj file and only a form of the Login area in login.clj.

I was attempting to get some of the same errors working as I had last night and also attempting to get this working before I uploaded the files. I don't have any reasonable starting points yet, thus there is no beginning implementation as of yet. I'm seriously embarrassed at the solutions I've been coming up with, thus I don't want to post them. I get conceptually what I should do, but for some reason, I can't seem to translate this. This is my first github account: my background is Python, Scheme a'la SICP, and some Python + PostgreSQL marketing program I built.

SECOND EDIT: Ack! I can't seem to get the thing to work at all... Yeah, I spent well over 20 minutes (hours) on this one, so I have just have to admit that I don't yet have the requisite knowledge to accomplish this, no matter how many sources I look to. I committed the updated files and all the odd things I tried, including all the variations on login box to running raw SQL. The closest I can come is getting it so that I don't get any errors, but no evidence at all that someone is logged in. Thanks so much for the help and suggestions. I'll most certainly return to this later.

https://github.com/dt1/noirKormaLogin

like image 986
dizzystar Avatar asked Aug 26 '12 11:08

dizzystar


1 Answers

There are a couple of issues that I see. First, in datapass.clj, you're creating an entity with no content. I'm not sure how Korma handles that. It's trying to thread results as inputs to other functions, so I could see how nil gets introduced there.

Secondly, you'll need something to handle the login post. (defpage ...) only handles GET requests by default. You'll need a separate defpage to handle the post. Something along these lines:

(defpage [:post "/login"] {:keys [user-name pwd]}
  (if-let [user (db/find-user user)]
    (if (noir.util.crypt/compare pwd (:password user))
       (do
         (noir.session/put! :some-key some-value)
         (noir.response/redirect "/success"))
       noir.response/redirect "/failed-to-login"))
  (noir.response/redirect "/failed-to-login"))

session/put! is how you put data into the session. The default is to use an in-memory store. You'll need to add Ring middleware to use persistent sessions (look at Session Stores).

Also, as luck would have, someone just posted an authentication app for Noir... you may want to take a look: https://github.com/xavi/noir-auth-app

like image 185
John Szakmeister Avatar answered Nov 09 '22 00:11

John Szakmeister