Fork me on GitHub
#fulcro
<
2020-07-19
>
solussd03:07:02

I'm storing an auth token in the app db that I need to add to the request headers in my remote. I think the right place to do this is in request middleware, but I don't have access to the app-db from there. Using cookies isn't an option. Maybe I should provide the app db atom to my fulcro application and then access it directly?

solussd03:07:53

or just def an atom to hold the token ... :man-shrugging:

tony.kay15:07:47

This is why the book tells you to def your app: (defonce SPA (fulcro-app …)) Now you have access to state anywhere with (app/current-state SPA)

tony.kay15:07:54

You can also use an atom

tony.kay15:07:03

Ah, but you have a circular ref problem if you’re doing middleware, since your middleware would need the app, but app needs your remote, which in turn needs the middleware 😜

tony.kay15:07:07

ah, dynamic langs

tony.kay15:07:20

yeah, I’d just use my own atom probably

solussd16:07:13

I ended up just closing over the app itself in the request middleware function (declared app, first).

solussd16:07:43

(declare app)

(defn access-token []
  (-> app ::app/state-atom deref :pawlytics/access-token))

(defn auth-middleware
  [request]
  (let [access-token (access-token)]
    (cond-> request
            access-token
            (assoc-in
              [:headers "Authorization"]
              (str "Bearer " access-token)))))
(defonce app
         (app/fulcro-app
          {:remotes
           {:remote (http-remote/fulcro-http-remote
                      {:url "/api/admin"
                       :request-middleware (http-remote/wrap-fulcro-request
                                             auth-middleware)})}}))

Quentin Le Guennec08:07:40

Hello, running the fulcro template, under the main app, I'm getting a blank page with "Not found. Missing index.html."

lgessler15:07:34

did you navigate to /index.html?

jb recluse22:07:01

the main app requires the server to be running (and serving) - i assume you've started it and that is the httpd you are hitting when you browse? (you should see it logging like this:

20-07-19 22:30:11 surface-system DEBUG [app.server-components.middleware:38] - Serving index.html
)

Quentin Le Guennec07:07:51

@U017JCS7L2Y I am running (shadow/repl :main) , as said in the docs

jb recluse13:07:34

that is the front end - clojurescript component. you also need the api server (or workspaces) in order to load the spa. the docs describe this too