@RomainLanz commented on Fri May 20 2016
Hi there !
I have a weird problem with a condition in my view to display different content when I user is logged in or not.
So I'm using the authUser
variable which is send by default to any views.
I have got in my navbar
partials a condition to display the Login / Register
button or user information.
<div id="nav-bar-right">
{% if authUser %}
<div class="nav-bar-user">
<!-- ... -->
</div>
<div class="nav-bar-separator"></div>
<div class="nav-bar-actions">
<!-- -->
</div>
{% else %}
<div class="nav-bar-login">
<p><a href="#" @click.prevent="showLoginModal">Connexion</a> / <a href="#" @click.prevent="showRegisterModal">Inscription</a></p>
</div>
{% endif %}
</div>
This is working perfectly (I have only tested it when I'm not logged in).
I also have a condition to display modal when you're not logged in. You can find the code below.
{% if not authUser %}
{% include 'frontend.modals.login' %}
{% include 'frontend.modals.register' %}
{% endif %}
This doesn't work. I have also try some "hacks" to make it works but nothing happens.
{% if authUser %}
{% include 'frontend.modals.login' %}
{% include 'frontend.modals.register' %}
{% endif %}
{% if not authUser %}
{% include 'frontend.modals.login' %}
{% include 'frontend.modals.register' %}
{% else %}
{% include 'frontend.modals.login' %}
{% include 'frontend.modals.register' %}
{% endif %}
Both example under display nothing. I have tried to display the variable authUser
in my view to see his content. So I have put {{ authUser | json }}
before the two conditions. Nothing is display.
@RomainLanz commented on Sun May 22 2016
I come back to give you updates.
So I have finish my login/register function and the if / else
in the navbar doesn't work either (it always display the else
content.
authUser
seems always empty
(not null
).
The code to auth my user that you can see below works perfectly:
* store(request, response) {
const username = request.input('username')
const password = request.input('password')
try {
const user = yield request.auth.attempt(username, password)
} catch(e) {
if (request.ajax) {
return response.status(400).send({ error: true })
}
yield request.withOut('password').andWith({ error: true }).flash()
}
return response.redirect('back')
}
@thetutlage commented on Sun May 22 2016
@RomainLanz authUser
is only to the views by the named auth
middleware.
Route.get('/profile', 'UserController.profile').middleware('auth')
Also make sure to update the adonis-auth
package from github, coz I added this global around a week ago and may be your app has the old version
@thetutlage commented on Sun May 22 2016
Reference from the conversation on Gitter
Stateless and stateful authentication will have a little difference in how you access the user from your controllers and views.
Stateful Authentication will have the access to a variable called currentUser
on each request and view after the user has loggedIn
Whereas Stateless authentication like JWT
and API
will have access to the variable called auth
on the request object. There will be no view's global for stateless auth, since creating a view with JWT in place is not expected.
@niallobrien commented on Wed May 25 2016
When can we expect this to be implemented? Thanks.