Check user is logged in or not

Is it possible to check somehow whether user is logged in or not from theme front-end?

It’s Not Possible In Ghost At Present But You Can Private Your Blog.

1 Like

Thanks @Rubel vai, I was trying to add a admin bar like wordpress :smiley:

Ghost’s admin panel is an entirely separate app to the site that is rendered with your theme. If you are logged in via the admin panel (or Ghost desktop) there’s absolutely no way for the theme to know about this.

There are however a bunch of shortcut URLs. If you’re on a post and you want to edit it, type /edit/ on the end of the URL and if you’re logged in the editor will load. The same thing works on tag and author pages.

1 Like

I’m just thinking about doing this as well and will likely just check for the ghost-admin-api-session cookie client-side. That should work, no?
Edit: should work if frontend and admin-client are on the same domain, I guess.

It probably won’t work since the cookie is limited to the Ghost path and is HTTPOnly

I have not tried it, but this might work if you are using cloudflare:

  • Create a worker which returns cookie value for ghost-admin-api-session
  • expose that worker as :///ghost/cookie via cloudfalre worker routes
  • Make an ajax call to the endpoint.

Hopefully, I will test this approach soon and will post the findings here.

If this approach works, we don’t really need to return the cookie, but instead we can return a flag to check if staff is logged in.

Vahid

Ok, confirmed, I am able to get the ghost admin cookie cookie via cloudflare worker. Now need to test it via ajax call. Fingers crossed.

vahid

I am able to make it work via AJAX call using above approach.

— UPDATE—

Ok more updates, just checking the presence of the cookie is not enough, to really check if the user is logged is or not, the cookie has to be verified, which can be done by calling

(http or https)://(domain-name)/ghost/api/canary/admin/users/me/?include=roles

The cloudflare worker, I have written, will upload that to Github and share the link.

Vahid