How to detect a user is logged in via Javascript? Topic is solved

General questions relating to Joomla! 4.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Post Reply
roumenkey
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Thu Jul 07, 2011 1:32 pm

How to detect a user is logged in via Javascript?

Post by roumenkey » Mon Jun 27, 2022 9:16 pm

I have a custom Login button which I want to switch to "Logout" once the user is logged in. How can I detect a user being logged in to switch the buttons (links?) And what is the link/command to log out?

thank you to all willing to help. much appreciated.

User avatar
AMurray
Joomla! Exemplar
Joomla! Exemplar
Posts: 7832
Joined: Sat Feb 13, 2010 7:35 am
Location: Australia

Re: How to detect a user is logged in via Javascript?

Post by AMurray » Mon Jun 27, 2022 9:58 pm

For your custom buttons, I think you could write plugin (or module) so you can display it where you need to.
Suggested initial steps:
  • Create menu item links for Login and Logout (login to the login component), logout is a standard menu item in Menu manager.
  • Make the items hidden (not displayed on menus). This way you can use the links for your custom buttons
  • Your code for switching the buttons would contain logic such as an IF-THEN-ELSE statement to determine if a user is "Guest" or "Registered" (note I don't know the specific PHP syntax).
  • I would suggest you would use PHP given that's what Joomla is written in.

For reference;

The Login Module does basically what you want - it swaps the 'state' of the login to logout (whether it be buttons or text links), perhaps study that module's code to see how it works.

https://docs.joomla.org/J4.x:Creating_a ... for_Joomla
https://docs.joomla.org/J3.x:Access_Con ... t_Tutorial

Others may tune in giving further advice..
Regards - A Murray

User avatar
sozzled
Joomla! Master
Joomla! Master
Posts: 12239
Joined: Sun Jul 05, 2009 3:30 am
Location: Canberra, Australia

Re: How to detect a user is logged in via Javascript?

Post by sozzled » Mon Jun 27, 2022 10:11 pm

There may be a way to detect if a person has logged in using Javasript but I don't know of one. @AMurray's idea, of using menu items—see viewtopic.php?t=988255 for more information—is one method. The standard J! login module also does what you want (the text of the button changes from login to logout depending on whether you are logged-in or not). If you want to find out if a person is logged-in you need to write PHP; not Javascript. 8)

I don't know about "custom"/third-party login modules; speak to the developers who write them.
“If you think I’m wrong then say, ‘I think you’re wrong.’ If you say ‘You’re wrong!’, how do you know?”
Walking the talk: https://j4xdemo.enduring.com.au
:)

roumenkey
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Thu Jul 07, 2011 1:32 pm

Re: How to detect a user is logged in via Javascript?

Post by roumenkey » Tue Jun 28, 2022 5:00 pm

thank you for your help. In the end I did it as suggested via the menu.


Post Reply

Return to “General Questions/New to Joomla! 4.x”