Fork me on GitHub
#luminus
<
2016-09-27
>
credulous02:09:47

Throwing another one out there. 🙂 I’m going through Web Dev with Clojure 2nd (thanks, yogthos!) adapting the picture-gallery project for my own thing.

credulous02:09:07

Instead of having a login modal, I have the login screen as the home page.

credulous02:09:27

On successful login, I want to load an admin dashboard.

credulous02:09:05

The login form displays nicely on the home page, the creds are collected and submitted to the server, and the authentication is successful. In the handler I use (secretary/dispatch! “/dashboard”) to try to load the dashboard component.

credulous02:09:33

Instead, what happens is the system re-initializes, connects to Figwheel again, and displays the login form.

credulous02:09:15

This is the login handler. On the server I’ve confirmed that it’s successfully authenticating the user.

(defn login! [fields error]
   (let [{:keys [id password]} @fields]
     (reset! error nil)
     (ajax/POST "/api/login"
                {:headers        {"Authorization" (encode-auth (string/trim id) password)}
                 :handler        #(do
                                     (session/remove! :modal)
                                     (session/put! :identity id)
                                     (js/setTimeout session-timer timeout-ms)
                                     (reset! fields nil)
                                     (secretary/dispatch! "/dashboard")
                                    )
                 :error-handler  #(reset! error (get-in % [:response :message]))})))

credulous02:09:02

On the server, I confirm that that “/api/login” service is logging in and returning nice JSON, but the client chops it off before it can send.

"Logging in"
{:status 200, :headers {}, :body {:result :ok}, :session {:ring.middleware.anti-forgery/anti-forgery-token "OKMme0t8x3HoNk7GOzVNQEzu3vGs3pmI7p+rOPkarULwLr5CVbX9JftbOk9RtXDdI23IuqjGMtK1Jpjt", :identity "rodfrey"}}
2016-09-26 22:41:11,382 [XNIO-1 I/O-3] DEBUG  - Error reading request
java.io.IOException: Connection reset by peer
	at sun.nio.ch.FileDispatcherImpl.read0(Native Method) ~[na:1.8.0_60]
	at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39) ~[na:1.8.0_60]
	at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223) ~[na:1.8.0_60]
	at sun.nio.ch.IOUtil.read(IOUtil.java:192) ~[na:1.8.0_60]
	at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380) ~[na:1.8.0_60]
	at org.xnio.nio.NioSocketConduit.read(NioSocketConduit.java:286) [xnio-nio-3.3.6.Final.jar:3.3.6.Final]
	at org.xnio.conduits.ConduitStreamSourceChannel.read(ConduitStreamSourceChannel.java:127) ~[xnio-api-3.3.6.Final.jar:3.3.6.Final]
	at io.undertow.server.protocol.http.HttpReadListener.handleEventWithNoRunningRequest(HttpReadListener.java:153) ~[undertow-core-1.3.23.Final.jar:1.3.23.Final]
	at io.undertow.server.protocol.http.HttpReadListener.handleEvent(HttpReadListener.java:131) ~[undertow-core-1.3.23.Final.jar:1.3.23.Final]
	at io.undertow.server.protocol.http.HttpReadListener.handleEvent(HttpReadListener.java:57) ~[undertow-core-1.3.23.Final.jar:1.3.23.Final]
	at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92) ~[xnio-api-3.3.6.Final.jar:3.3.6.Final]
	at org.xnio.conduits.ReadReadyHandler$ChannelListenerHandler.readReady(ReadReadyHandler.java:66) ~[xnio-api-3.3.6.Final.jar:3.3.6.Final]
	at org.xnio.nio.NioSocketConduit.handleReady(NioSocketConduit.java:88) [xnio-nio-3.3.6.Final.jar:3.3.6.Final]
	at org.xnio.nio.WorkerThread.run(WorkerThread.java:559) [xnio-nio-3.3.6.Final.jar:3.3.6.Final]

credulous02:09:33

the first two lines are just printlns, with the json being the return from the handler.

yogthos03:09:51

if I recall correctly, dispatch! doesn’t actually change the URL, but simply runs the handler for it. You could try using (set! (.-href js/location) "#/dashboard”) instead to set the location and that should trigger the handler as well

yogthos03:09:07

the other thing to check is what the /dashboard route is doing to make sure it’s not causing the page to reload

danielgrosse07:09:13

Hello, I try to build a REST API which returns XML. Where could I find Info for compojure-api, how I can set the response format and output xml?

credulous09:09:38

Thanks @yogthos. What I discovered is that my service is being called, but the client is aborting the connection. (Calling the service from swatter-ui with the same authoriziation works fine.)

credulous09:09:00

I must be abusing the ajax call. But I don’t know how.

credulous09:09:28

Stripped down version: (password, userid and error are locally defined in the repl for testing)

(try
      (ajax/POST "/api/login"
                {:headers        {"Authorization" (encode-auth (string/trim id) password)}
                 :handler        #(prn "Success")
                 :error-handler  #(prn "Failed" %)
                 })
       (catch js/Object e
         (prn "Caught " e (.-stack e))
         )
       )

credulous09:09:31

which gives output "Caught " #object[TypeError TypeError: Cannot call property call in object [JavaPackage com.cognitect.transit.writer]. It is not a function, it is "object".] “"

credulous09:09:09

no success handler call, no error handler call

credulous09:09:14

and it aborts the connection

yogthos12:09:48

@credulous the error sounds like there’s something in the payload that transit is chocking on

credulous15:09:56

@yogthos you mean in the payload from the server? I went to the Network list in the Chrome dev tools, copied the call into curl, and executed it:

credulous15:09:17

# curl '' -H 'Accept: application/transit+json' -H 'Referer: ?' -H 'Origin: ' -H 'x-csrf-token: IbuLKGpnC1Lgc6rngWlanm2iQNvlQUSHACNdtoC/7P0fdRMvySnG+SRPbINMlicPXLcKP0gn8JAysD71' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36' -H 'Authorization: Basic cm9kZnJleTp0d2VlZGxl' -H 'Content-Type: application/transit+json' --data-binary $'["~#\'",null]' --compressed
["^ ","~:result","~:ok”]

credulous15:09:25

Looks like valid transit to me… not sure why the client would be choking. Maybe I have to do something special to enable transit parsing?

yogthos15:09:31

you’re seeing a Java exception, so I think the server is failing to parse the payload from the client

credulous16:09:36

Hm, possibly.. I have prn statements at every step in the login process, including right before the return... they all print on the server console.

credulous16:09:50

And there is no payload, it's just the Authorization header

manutter5118:09:18

noob question: got my app all build, looks great, the boss loves it, now it’s time to deploy. Is there a way to run the migratus migrations without leiningen in prod?

manutter5118:09:50

Ah, never fails, figured it out immediately after posting: just type DATABASE_URL=“…” java -jar myapp.jar migrate

manutter5118:09:10

I was typing “migratus migrate” and just getting stack dumps