Check if user is logged from outside


(Martial) #1

I have a website and i need to know if user is logged to allow him some part of the website.

In the API the request

http://forum.discourse.com/session/current.json

Allow you to retrieve data of the current user. I tried to use this url to get the information with jquery like :

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
 <script>
       $.get('http://forum.discourse.com/session/current.json', function( data ) {
        console.log(data);
      });
  </script>
</body>
</html> 

CORS is enabled and i get 404 (Not Found)

How can i know if user is logged into the forum ?


(Rafael dos Santos Silva) #2

Discourse.User.current()


(Martial) #3

Can you explain ? Where i get the lib ?


(Rafael dos Santos Silva) #4

Discourse is a global variable, so you can use it anywhere.

So, you want to know if the user is logged on Discourse outside Discourse? Sorry misread you previously. This is harder.

Maybe using Discourse as an SSO provider?


(Martial) #5

Finally, from inside, where should i put Discourse.User.current() ?


(Rafael dos Santos Silva) #6

It’s a javascript variable. You can use inside script tags.