I'm currently trying to figure out how to detect if a Wordpress user is logged in when browsing the decoupled frontend (NextJS app, GraphQL via the WPGraphQL plugin) for showing options based on this, like an edit button for the current page. It's a simple check on monolithic Wordpress pages, but I'm clueless about doing it this way.
Asked
Active
Viewed 329 times
0
-
1Might help: https://stackoverflow.com/questions/42381521/how-to-get-current-logged-in-user-using-wordpress-rest-api – cabrerahector Jul 15 '22 at 15:54
2 Answers
0
If a user is logged in there should be a key in the local or session storage you can check for. If it it populated that user is logged in. I would reach out to you team to learn what key to look for. Code for doing this would look similar to
const key = localStorage.getItem("KEY");
if (key != null):
// user is logged in
https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem

Micky
- 422
- 2
- 6
0
Hacky, but if you have the default wp body class you can check it logged-in. Not tested, but shouuld work. get_body_class has :
if ( is_user_logged_in() ) {
$classes[] = 'logged-in';
}

Josh H
- 1
- 1
- 1