Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Current without autenticated #85

Closed
johannesschobel opened this issue Oct 4, 2023 · 5 comments
Closed

Current without autenticated #85

johannesschobel opened this issue Oct 4, 2023 · 5 comments

Comments

@johannesschobel
Copy link

Dear @lazaronixon ,

thanks again for this wonderful gem. I have been playing around a bit, and i really like it.
Following up on #81 , i have another question - maybe you could help and point me into the right direction?

In my application layout, i have a navbar that shows the email-address of the currently logged in user (i.e., Current.user.email). This navbar is present in all views.

Normal pages (i.e., everything related to imprint, about, contact, ...) should be accessible for "guest" users, therefore i have the skip_before_action :authenticate in my PagesController.
However, when calling the pages#contact route, the Current.user.email is not shown, because Current.user is not present. This is, because the authenticate method is not called (i.e., i use the skip_before_action)

How can i still get the Current.user, if the user has been logged in before?

Thank you very much for helping me out. All the best,
Johannes

@lazaronixon
Copy link
Owner

3 options…
1 - add a type to the user and customize the flow
2 - if it’s just the email set it as a cookie.
3 - create a guest model, and add a guest attribute to Current.rb, load it from cookies in a before_action…

@johannesschobel
Copy link
Author

Dear @lazaronixon ,

i am not sure, if i explained it correctly. Actually, i don't want some kind of Guest user (that can be "converted" into a real user, if the user signs up; as described in #75 ).

I want:

  • to make a controller accessible for everyone (i.e., logged in users, as well as users that are not currently logged in)
  • still get the Current.user, if the user has been logged in (before he moved to the page, i.e., if a cookie is set)

I guess, we could have another method, like get_session in the application_controller in order to address this issue:

# ApplicationController

before_action :get_session

def get_session
  return unless session_record = Session.find_by_id(cookies.signed[:session_token])
  Current.session = session_record
end

This way, the Current.user would work (if the user is logged in).
What do you think about this solution?

All the best,
Johannes

@walterdavis
Copy link

walterdavis commented Oct 5, 2023 via email

@johannesschobel
Copy link
Author

Dear @walterdavis ,

thank you very much for your extensive explanation and answer on this topic.
I think, having such a set_session method, as described above, solves my issue.

All the best,
Johannes

@kwhandy
Copy link

kwhandy commented Feb 18, 2024

im surprised that it's not just me realize this issue @johannesschobel

when we work with another auth gem(read: devise) we can add <% if user_signed_in? %> tag(cmiiw) to the navbar section, but i've just notice that this gem basically built based on 37signals's HEY influence

then why this important? if you put bit more attention, you'll notice that HEY has 2 domains, www.hey.com ONLY FOR MARKETING and app.hey.com ONLY FOR MAIN SERVICE

with these 2 purpose, we know that authentication on app.hey.com trying to ISOLATE ENTIRE APP FROM PUBLIC EXPOSE rather than what rails programmer usually do

so if anyone plan to do something like in e-commerce site case, i suggest to add new line on main app controller to set session which can help to check if user already logged in or not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants