简体   繁体   English

部署后Devise失去会话

[英]Devise loses session after deploy

I have a rails 4 app where I am using devise for authentication and it works perfectly. 我有一个rails 4应用程序,我正在使用设计进行身份验证,它工作得很好。 My only problem is that it loses the session of a user after I deploy it on the server and the users have to sign in again. 我唯一的问题是,在我将其部署到服务器上并且用户必须再次登录后,它会丢失用户的会话。

If I just do a restart of nginx/passenger (which I am using for my app) it doesn't loses it. 如果我只是重新启动nginx / passenger(我用于我的应用程序),它就不会丢失它。 When I am deploying my app I am losing it. 当我部署我的应用程序时,我正在失去它。 For deploying I am also wipe out all the database automatically and my deployment script runs the seeds file which it also generates the users. 对于部署,我也会自动清除所有数据库,我的部署脚本运行种子文件,它也会生成用户。

We are currently developing the app so this kind of behavior is acceptable for now, but in the future when the app will be ready, we won't do it like this way (of course!). 我们目前正在开发应用程序,所以这种行为现在是可以接受的,但是将来当应用程序准备就绪时,我们不会这样做(当然!)。

So is this an issue due to the reseeding or I should check something else? 那么这是一个问题,因为重新播种还是我应该检查别的东西? I see that the encrypted password changes everytime I run the wipe out/seed action, does this have to do with the losing of user session? 我看到每次运行wipe out / seed操作时加密的密码都会改变,这是否与丢失用户会话有关?

You should never wipe out a database during deployment. 您永远不应该在部署期间消灭数据库。 Imagine that your app is running and you have hundreds of users. 想象一下,您的应用程序正在运行,并且您拥有数百个用户。 Now you make some changes in the code and do a deploy. 现在,您在代码中进行了一些更改并进行部署。 POOF all your data and users are gone! POOF所有数据和用户都消失了! Certainly this is not what you want. 当然这不是你想要的。

Secondly, users getting logged out when you wipe out the database could be due one of the following reasons: 其次,当您清除数据库时用户退出可能是由于以下原因之一:

  • Are you seeding users with the same ID? 您是否为具有相同ID的用户播种? If the user ID changes when you re-seed, it will cause users to be logged out 如果重新播种时用户ID发生更改,则会导致用户注销

  • Are you storing sessions in the database using config.session_store :active_record_store instead of using cookies? 您是使用config.session_store :active_record_store而不是使用cookie在数据库中存储会话? In this case, wiping out the database will delete the sessions table and log out all users 在这种情况下,擦除数据库将删除会话表并注销所有用户

  • Rails 4 uses an encrypted cookie store by default. Rails 4默认使用加密的cookie存储。 Make you sure you're not changing your application's config.secret_token when re-deploying, in case its getting loaded from the database 在重新部署时确保您没有更改应用程序的config.secret_token ,以防它从数据库加载

Ultimately, wiping out the database is the sole reason why your users are getting logged out, and that is a bad practice. 最终,擦除数据库是用户退出登录的唯一原因,这是一种不好的做法。 So the most important thing to fix is do not wipe data during deployments . 因此,最重要的是要在部署期间不要擦除数据

The reason for this behavior is the following: 此行为的原因如下:

Everytime some user changes his password, devise automatically signs_out him. 每次有些用户更改密码时,都会自动设置签名。

So, basically by reseeding the data, the password is recalculated (even though the password is the same, the new encrypted password is different from the old one). 因此,基本上通过重新种植数据,重新计算密码(即使密码相同,新的加密密码也不同于旧密码)。 So the devise will automatically sign_out the user, because it seems like the password is changed (based on the different encrypted_password field). 因此设计将自动签署用户,因为它似乎更改了密码(基于不同的encrypted_pa​​ssword字段)。

I managed to bypass this behavior, by specifically setting up the encrypted_password in the seeds.rb file and bypassing the validation. 我设法绕过这种行为,专门设置seeds.rb文件中的encrypted_pa​​ssword并绕过验证。

If I just do a restart of nginx/passenger (which I am using for my app) it doesn't loses it. 如果我只是重新启动nginx / passenger(我用于我的应用程序),它就不会丢失它。 When I am deploying my app I am losing it. 当我部署我的应用程序时,我正在失去它。 For deploying I am also wipe out all the database automatically and my deployment script runs the seeds file which it also generates the users. 对于部署,我也会自动清除所有数据库,我的部署脚本运行种子文件,它也会生成用户。

If you generate new users, the old ones will lose their sessions. 如果您生成新用户,旧用户将丢失其会话。

This is because the values of the new users will be different. 这是因为新用户的价值会有所不同。 For example, they might not have a remember token set, or if the session_id uses the values of user.created_at or user.token_generated_at they will be different every time you drop and recreate your database. 例如,它们可能没有记忆标记集,或者如果session_id使用user.created_atuser.token_generated_at的值, user.created_at每次删除并重新创建数据库时它们都会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM