如果不能正常显示,请查看原文 , 或返回

How to share session when login between Laravel and Ruby

The way I see it, this question should have asked about sharing sessions between Ruby and PHP or sharing sessions between Laravel and Rails. Either way, my answer will address how Laravel stores session information and how this can be leveraged from outside the framework.

Laravel comes with user authentication and session management built in. It is not a full fledged stock user management. If you need a more comprehensive user and session management module, you can use projects like Zizaco Entrust which I personally find fits all my needs (Multiple user roles, multiple user and/or role permissions etc).

What I have noticed in the default users DB table in laravel there is a column called "remember_token". What this does is when a user is logged in, the remember token is stored in the database. The same token, please correct me if I'm wrong, is stored on the browser in the cookies. When the user opens the app without having logged out, the browser cookie is compared to the DB remember token. If they match, then the user is automatically logged in.

So how can this be leveraged from Ruby/Rails? As far as this goes, the DB remember token column can be leveraged directly from the database. Otherwise you will require the user to use the same browser to access both the Laravel and Rails sides so as to have access to the browser cookies.

So Laravel already has its own session tokens stored. If you can find a way to write this token at a place where Ruby/Rails can access without messing with the Laravel side of things, then I think you can maintain a seamless session.

You could try write the remember token or a hashed version of it in another DB table. Then use this value on the Ruby/Rails side to compare and automatically log in a user who is logged in in Laravel.

返回