我想先重定向用户,然后以编程方式登录用户。
我有一个 multi-tanancy 应用程序,当用户尝试在根域登录时,我的应用程序不仅应该将用户重定向到他的子域,还应该让用户登录。
到目前为止,我正在尝试通过以下代码来完成此操作:
# In sessions#create ( Root )
redirect_to "http://#{account.subdomain}.localhost.com:3000/users/sign_in"
Apartment::Tenant.switch(account.subdomain)
sign_in(:user, account.owner) # Basically, owner is the user here.
当我运行代码时,它成功地重定向到子域,但没有让用户登录。
我收到以下错误:
Completed 302 Found in 93ms (ActiveRecord: 39.7ms)
此外,它不会切换schemawhen I do Apartment::Tenant.switch(account.subdomain),因为在运行Apartment::Tenant.switch(account.subdomain)when I run之后user = User.first,可以在日志中看到的查询是:
User Load (0.9ms) SELECT "public"."users".* FROM "public"."users" ORDER BY "public"."users"."id" ASC LIMIT 1
这里,public对应的schema是根域,而不是schema子域。