OpenframeProject / Openframe-JSClient

Javascript API Client
10 stars 2 forks source link

Question: Is there any way to know if a user is still logged in? #4

Open jvolker opened 5 years ago

jvolker commented 5 years ago

How long is a token valid? Is there any way to know if a user is still logged (since it is stored internally) in or if the token expired?

I would like to play around with the web clipper and am wondering how often I would have to login again and how to check if it's still valid.

Thanks.

jmwohl commented 4 years ago

Two weeks. Loopback, the framework the API is built with, doesn't use JWTs, it has its own access token scheme, and there's no refresh built in.

EDIT: And to answer your other question, the only way to tell if a user is logged in is to hit an authenticated endpoint and see what the response is. (e.g. /v0/users/current, passing the access token, and if you get a 200 you're logged in, if you get a 401 you're not)

jvolker commented 4 years ago

Thanks! Would be nice to have a method for that implemented directly into the jsclient.

This works for me:

const OF = new (require("openframe-jsclient"))();
const auth = require("openframe-jsclient/src/auth");

const token = auth.getToken();

fetch(
  "https://api.openframe.io/v0/users/current?access_token=" + token
).then(response => {
  if (response.status == 200) loggedIn = true;
  else if (response.status == 404 || response.status == 401) loggedIn = false;
}