设计..首次登录后应要求更改密码 [英] Devise ..After first login should ask for change password

查看:47
本文介绍了设计..首次登录后应要求更改密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用设计作为身份验证.

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.

我试过模型

 after_create :update_pass_change

    def update_pass_change
     self.pass_change = true
     self.save
    end 

推荐答案

检查current_user.sign_in_count是判断首次登录的方式.

Checking current_user.sign_in_count is way to judge first login.

你会做这样的事情.

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

config/routes.rb

resource :passwords

app/views/passwords/_form.html.erb

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 %>

这篇关于设计..首次登录后应要求更改密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆