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?
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.
session_start(); Check if $_SESSION["loggedIn" ] (is not) true - If not, redirect them to the login page. Save this answer.
is_user_logged_in(): bool. Determines whether the current visitor is a logged in user.
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.
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}" />
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With