创建帐户后登录用户

杰森·斯威特(Jason Swett)

我正在使用ng-token-auth模块和devise_token_auth gem。

默认行为似乎是当用户注册时,会向用户发送一封包含确认链接的电子邮件。我不想要这种行为;我只希望用户在创建帐户后立即登录,这当然是创建帐户工作的一种非常普遍的方式。

我不确定如何实现这一目标,我什至不知道该问什么。谁能提供一些见识之前,会有人碰巧使用过这些库吗?

林恩·迪伦·赫利(Lynn Dylan Hurley)

杰森,您的答案基本上是正确的。您还需要指示服务器绕过电子邮件确认步骤。如果不这样做,则在访问确认电子邮件中的链接之前,用户将无法登录。以下是完整的说明:

1.绕过电子邮件确认服务器端

假设模型被调用Userskip_confirmation!作为预创建回调来运行该方法。这实际上只是将用户的confirmed_at值设置为当前时间,但是该步骤是绕过电子邮件确认所必需的。

class User < ActiveRecord::Base
  include DeviseTokenAuth::Concerns::User
  before_create :skip_confirmation!
end

2.成功注册后立即登录

假设html表单如下所示:

<form ng-submit="submitRegistration(registrationForm)" role="form" ng-init="registrationForm = {}">
  <fieldset>
    <div>
      <label>email</label>
      <input type="email" name="email" ng-model="registrationForm.email" required>
    </div>

    <div class="form-group">
      <label>password</label>
      <input type="password" name="password" ng-model="registrationForm.password" required>
    </div>

    <div class="form-group">
      <label>password confirmation</label>
      <input type="password" name="password_confirmation" ng-model="registrationForm.password_confirmation" required>
    </div>

    <button type="submit">Register</button>
  </fieldset>
</form>

更新-从devise_token_auth版本0.1.29.beta2开始,如果使用上述before_create :skip_confirmation!回调,则用户将在注册时自动进行身份验证因此,上面的代码就足够了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章