I need a URL that will return 200 for a logged in user and 401 or 403 if the user is not logged in.
If require_login is checked, every page does a 301 to the login page.
I need a URL that will return 200 for a logged in user and 401 or 403 if the user is not logged in.
If require_login is checked, every page does a 301 to the login page.
Is this a user request, i.e. using the current users session?
OrâŚ
Is this an admin API request using an admin API key?
Yeah. Itâs me not understanding the question Iâm asking.
Iâm trying to do an auth_request in NGINX to tell whether the request is coming from a user thatâs logged in by querying an URL to see whether it gets a 200 response or not.
Itâs occurring to me that doing that is not quite as simple as Iâd hoped.
You could try /session/current.json
It will return 200 if authenticated and 404 if not.
Generally .json / API requests donât redirect.
That seems promising. Iâll keep poking at it.
Many thanks!
Is there an easy way to do this from a different subdomain?
Example, the forum is at forum.example.com and the request is coming from example.com (either from the frontend or backend code).
Sure. You can make an API call from anywhere.
But this specific call needs to be done from the frontend, since it will use the session cookies sent by the browser to the forum.
The Discourse session cookie appears to be just for the subdomain, so would the cookie be accessible from the top-level domain? I see _forum_session on the Discourse subdomain but it doesnât appear when visiting the TLD.
If the cookie were available on the top-level domain, I was thinking that it would also be passed to the backend, so the backend could forward it to Discourse, but Iâm not sure.
Maybe it requires using Discourse as an SSO provider? If it isnât known whether the user is logged in, then we could redirect through the SSO process to check. Iâm currently setting it up on a test server to see if it would work.
Edit: my end goal is to generate a JWT with the user data from Discourse (only if logged in to Discourse) and pass it to Firebase. There is a Discourse server on the subdomain, an extra backend server that can perform additional logic, and an SPA that connects to Firebase if given a JWT.
If you want anything fancy like this you would need to implement your own CurrentUserProvider
Thanks, I just looked it up and found this other thread, so Iâll ask some more questions about it over there.
Edit: it looks like we can do what we need with Discourse as an SSO provider.
If you can do it with SSO I highly recommend you go that path vs a provider
Thanks, it looks like we can check if a user is logged in and then redirect through Discourseâs SSO route if not logged in. It seems to work well on my laptop. The user logout webhook from Discourse can then log them out of the other app.
Potreste spiegarmi come verificare se un utente è loggato su Discourse da un altro sottodominio? Sto cercando di implementare un middleware di autenticazione nei miei gestori di rotta (sul server) per controllare se la sessione SSO dellâutente su Discourse è ancora valida.
Sto cercando di implementare quasi la stessa cosa per unâapp web, ma non riesco a capire come verificare se lâutente è ancora loggato. (Preferibilmente loggato dallo stesso browser attualmente utilizzato per inviare la richiesta al server)
Grazie!
Il mio codice non è ancora attivo, ma se lâutente viene reindirizzato tramite un altro server (con Discourse come provider SSO), allora una sessione esisterĂ su quel server esterno. Ho creato una rotta lĂŹ, qualcosa come /auth/is-authenticated che restituisce lo stato dellâutente. Serve principalmente per rimuovere i pulsanti âAccediâ quando lâutente è giĂ loggato. Quando lâutente si disconnette da Discourse, penso che un webhook lo disconnetta anche dallâaltro server. Non ho controllato il codice da un poâ, ma credo che sia cosĂŹ che lâho configurato.
Come può un server esterno verificare se il browser dellâutente è ancora connesso a Discourse? Non credo che un altro dominio possa accedere ai cookie di Discourse impostati dopo il login.
Il mio obiettivo è disconnettere lâutente dal server esterno (non Discourse) se lâutente si è disconnesso da Discourse utilizzando solo il browser attivo corrente. (Ă possibile?)
Grazie per la risposta.
Quando lâutente si trova nellâapplicazione esterna, clicca sul pulsante di accesso e viene reindirizzato attraverso il flusso di SSO di Discourse, per poi tornare allâapplicazione esterna. Questâultima può memorizzare una sessione con i dati dellâutente. Quando lâutente esce da Discourse, il webhook può eliminare la sessione dellâapplicazione esterna. Non ne sono certo, ma credo che lâintestazione del webhook sia X-Discourse-Event: user_logged_out.
Modifica: lâuscita dal sito esterno viene effettuata tramite lâAPI di Discourse.
Invece di chiedere a Discourse se un utente è connesso, puoi interrogare lâapplicazione esterna. Nel mio caso, serve solo per cose come rimuovere il pulsante di accesso dal sito esterno.
Posso ricontrollare il mio codice in seguito. Non lo ho esaminato da un poâ, ma credo che facesse qualcosa di simile.
Grazie per la risposta! Apprezzo molto.
SĂŹ, ora è chiaro. Lâunico limite nella disconnessione tramite API è che lâutente verrĂ disconnesso da tutte le sessioni (su tutti i dispositivi, poichĂŠ lâAPI non può distinguere tra la sessione del browser corrente e gli altri browser connessi).