Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing devise current_user inside another rails engine

So here is my issue. I have a main application which has devise in it,for which I am creating an engine. In the engine I need the current_user from main app. Not sure how I will get that, I tried this link but to no avail. Any help would be great. If this is not recommended please do explain why.

like image 911
abhilash Avatar asked Jul 11 '16 12:07

abhilash


2 Answers

Solved it

In the engine you can access the main app using 'main_app'. In this variable you have access to the current user, assuming you have devise. In order to access devise current_user all you have to do is

main_app.scope.env['warden'].user

Devise stores the user inside warden key in the session. Since it uses warden internally to authenticate and store the details in session.

If there is an alternative solution please do let me know.

like image 94
abhilash Avatar answered Sep 24 '22 00:09

abhilash


If you are using Rails 5.1+ this would instead be

main_app.scope.request.env['warden'].user

Thats because env has been deprecated.

like image 20
Vikram Bahl Avatar answered Sep 24 '22 00:09

Vikram Bahl