Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP User Authentication Adding Authentication to Your Application Logging Out

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Logout link not showing up

Hi all,

Would anyone be able to clone this repo and find out why I'm not getting the logout link to show up like Alena? github.com/jg-digital-media/php_auth OP note: Edited link as I've since updated the github repo.

Registration should be working so you can create an account using the on your localhost and attempt to log in. I'm pretty sure login is working as normal but I don't understand why the login conditional isn't working! :)

Thanks

3 Answers

Benjamin Larson
Benjamin Larson
34,055 Points

I remember having trouble setting the cookie at first. I think in your case it is because of your cookie domain setting in the .env file. Perhaps try setting:

COOKIE_DOMAIN="localhost"

The domain shouldn't include the full path. I was going to suggest creating a new COOKIE_PATH env variable, but it looks like you already added that path to your $accessToken variable in procedures/doLogin.php

You can add the login functionality to the doRegister procedure so when a user registers they are automatically logged in, but for now just be aware that you'll have to explicitly login after registering before checking the JWT.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Blimey! I just got in!!

You're right, I just got in after changing the COOKIE_DOMAIN property and while I'm still not seeing any JWT settings in DevTools I'm getting the logged in message, a welcome message and the logout link. So clearly I've made some progress.

Actually it's showing up as having stored a cookie so that's cool! :)

Thanks for your help and following up

Benjamin Larson
Benjamin Larson
34,055 Points

Hi Jonathan -

Your anchor tag wasn't closed and given a label for the link.

<li><a href="/procedures/doLogout.php"</li>
<!-- CHANGE TO -->
<li><a href="/procedures/doLogout.php">Logout</li>

Also you have lots of references, particularly in redirects, using absolute paths, which makes portability difficult when you want to move it from development to production (or when you want others to clone and test it :D ).

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Hi Benjamin,

I thought someone might comment on the paths! :)

That's down to the way I've organised my files on localhost. I couldn't follow the videos exactly the same way so I just had to remember those default paths every now and then!

Hmmm! Unfortunately the fix didn't work and the logout link still isn't showing up!

Benjamin Larson
Benjamin Larson
34,055 Points

Hmm, that fixed the issue when I tried it with my code base. Are the Login and Register links displaying after you login?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Yes they are! I've logged in multiple times though, when I've logged in from Add Book or from the login link itself. But I'm pretty sure the login itself works because the browser reacts like it's found a new password to save.

Benjamin Larson
Benjamin Larson
34,055 Points

It may not be working, I found it difficult to debug with all the redirects because you redirect away from the page without displaying any errors. This eventually gets remedied (to some extent) by adding some flash messages.

Maybe check that the cookie is being set? If you are using Chrome, you can trying installing the extension JWT Analyzer. It was linked in one of the teacher's notes. You could at least confirm that it's being set and inspect the contents of the JWT.

Benjamin Larson
Benjamin Larson
34,055 Points

If you do install that, you can then just right-click anywhere on your site page and hit "Inspect", and then select the last Tab that says JWT.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Hi Ben,

Sorry it's been so long responding :)

I installed the JWT Analyser and I'm not getting any JWT data returned so you may be right and I'm not actually successfully logging in! I'm not sure what to do next but I'll keep plugging away. At least I have a new tool in my arsenal, I must have missed it when it was linked in the notes :)