Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if is user logged in?

Tags:

jsf

jsf-2

I want do display a login link when the user isn't logged in and a logout link when the user is logged in. I'm using container managed security as defined in web.xml.

How can I achieve this?

like image 633
kravemir Avatar asked Dec 04 '11 09:12

kravemir


People also ask

How can I check which user is logged in?

Method 1: See Currently Logged in Users Using Query CommandType cmd and press Enter. When the Command Prompt window opens, type query user and press Enter. It will list all users that are currently logged on your computer.

How do we check if user is logged in in PHP?

session_start(); Check if $_SESSION["loggedIn" ] (is not) true - If not, redirect them to the login page. Save this answer.

How do I know if a user is logged in to WordPress or not?

is_user_logged_in(): bool. Determines whether the current visitor is a logged in user.

How do I check if a user is logged in node?

connect('mongodb://localhost/nyg', {useNewUrlParser: true}); const Schema = mongoose. Schema; const userSchema = new Schema({ email: String, password: String }); const User = mongoose. model('users', userSchema); app. post('/register', async (req, res, next) => { const user = await User.


2 Answers

The username of the logged-in user is available by ExternalContext#getRemoteUser() which delegates under the covers to HttpServletRequest#getRemoteUser(). Both are available in EL by #{facesContext.externalContext.remoteUser} and #{request.remoteUser} respectively. If it is null, then it means that the user is not logged in.

So, in your view you can check it in the rendered attribute as follows:

<h:form rendered="#{not empty request.remoteUser}">
    <h:commandLink value="Logout" action="#{auth.logout}" />
</h:form>
<h:link value="Login" outcome="login" rendered="#{empty request.remoteUser}" />

See also:

  • Conditionally displaying JSF components
like image 67
BalusC Avatar answered Sep 20 '22 20:09

BalusC


This depends on your definition of "logged in". Usually you can login an user in your application by implementing your own login mechanism. Otherwise you are using some container dependent mechanism which your server will take care of.

For the container managed method you can usually check FacesContext with its ExternalContext.

FacesContext.getExternalContext().getRemoteUser();

You can put that method into a helper bean and check it with the rendered attribute of your link component.

If you implement your own system its totally up to you.

like image 21
Udo Held Avatar answered Sep 22 '22 20:09

Udo Held