Log in with a friend, from the server side?

I am adding Friend to my Compojure app for authentication. I am using workflows/interactive-form

. I have a form that allows me to register a new user (JS POST before /register

) and I have a form that allows me to login (JS POST before /login

) and both of them work fine so far.

I want the new user registration to be registered by the user as well, naturally. Is there an easy way to say "this user is now logged in" on the server side?

(The client side AJAX call /register

has a username and password, so I could just recode the form data as expected /login

and send a second AJAX request to login. It seems awkward.)

I think it may have something to do with :workflows

, but this piece of documentation puzzles me a little.

+3


source to share


1 answer


I just looked at the same issue, also preferring to avoid the second unnecessary AJAX request. Friend's code is a little more complex, but you want to focus on functions cemerick.friend.workflows/make-auth

and cemerick.friend/merge-authentication

. This is a tricky problem that depends a lot on your code, but here's a sample code snippet to get you started in the right direction:

(ns my-auth
  (:require [cemerick.friend :as friend]
            [cemerick.friend.workflows :refer [make-auth]]))

(let [user (get-user-data-from-db)
      auth (cemerick.friend.workflows/make-auth user
              {::friend/redirect-on-auth? false})
      merged-auth-in-session (friend/merge-authentication {} auth)])

      



The function make-auth

gives you the auth structure you need, and the function merge-authentication

updates auth in the session. How you deal with the response ( merged-auth-in-session

) you get depends on your framework (e.g. Liberator).

0


source







All Articles