Devise ..After first login should ask for change password

妖精的绣舞 提交于 2019-12-05 02:10:37

问题


I am using devise as authentication in my application.

I need to implement feature in devise. After first login user should ask to change password.

I tried through model

 after_create :update_pass_change

    def update_pass_change
     self.pass_change = true
     self.save
    end 

回答1:


Checking current_user.sign_in_count is way to judge first login.

You'll do something like this.

class ApplicationController < ActionController::Base
  def after_sign_in_path_for(resource)
    if current_user.sign_in_count == 1
      edit_passwords_path
    else
      root_path
    end
  end
end

You need Implement edit/update password action.

class PasswordsController < ApplicationController
  def edit
  end

  def update
    if current_user.update_with_password(user_params)
      flash[:notice] = 'password update succeed..'
      render :edit
    else
      flash[:error] = 'password update failed.'
      render :edit
    end
  end

  private
    def user_params
      params.require(:user).permit(:current_password, :password, :password_confirmation)
    end
end

config/routes.rb

resource :passwords

app/views/passwords/_form.html.erb

<%= form_for current_user, url: passwords_path do |f| %>
  current_password:<br />
  <%= f.password_field :current_password %><br />
  password:<br />
  <%= f.password_field :password %><br />
  password_confirmation:<br />
  <%= f.password_field :password_confirmation %><br />
  <br />
  <%= f.submit %>
<% end %>


来源:https://stackoverflow.com/questions/32395442/devise-after-first-login-should-ask-for-change-password

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!