前提・実現したいこと
ただいまrailsチュートリアル11章を学習中なのですが、
サインアップと同時にユーザーにトークンとユーザーのemailを含めた認証用メールを送り、そこに記載されているURLへアクセスするとアカウントが有効になる、という機能が紹介されていたので自分もそれに挑戦したのですが、何度か試して見たのですがうまく行かなかったので質問させて頂きました。
発生している問題
メールに記載されているURLを踏むと↓アクションに飛ぶのですが、条件式の「user.authenticated?(:activation, params[:id])」がtrueを返して来ず、else以下の処理を行ってしまいます。
※user.authenticated?(:activation, params[:id])をコメントアウトすると全てtrueになり、無事処理されます。
def edit user = User.find_by(email: params[:email]) if user && !user.activated? && user.authenticated?(:activation, params[:id])←ここ user.activate log_in user flash[:success] = "Account activated!" redirect_to user else flash[:danger] = "Invalid activation link" redirect_to root_url end end
該当のソースコード
メールを送られたユーザーの情報
<User id: 113, name: "authenticateテスト", email: ***(伏せてます), created_at: "2019-07-27 07:47:33", updated_at: "2019-07-27 08:29:18", password_digest: "$2a$10$LSSLB18esxbEBcdGb5W6TOFReJuZzQIsvbXEw19f9Xd...", admin: nil, activation_digest: "$2a$10$a2q6NrZbZTWZ0Ep7J.J9XucLxFh199sfQG6D.Q1LjV8zdVCOHUNY2" activated: false, activated_at: "2019-07-27 08:29:18", reset_digest: nil, reset_sent_at: nil>
メールを送る処理
def send_activation_email UserMailer.account_activation(self).deliver_now end
メールのオブジェクト(html形式)
<%= @user.name %>, Welcome to the Sample App! Click on the link below to activate your account: <%= link_to "Activate", edit_account_activation_url(@user.activation_token, email: @user.email) %>
authenticated?メソッド
def authenticated?(attribute, token) digest = self.send("#{attribute}_digest") return false if digest.nil? BCrypt::Password.new(digest).is_password?(token) end
関連するメソッド
attr_accessor :remember_token, :activation_token,:reset_token ・ ・ ・ ・ private def create_activation_digest self.activation_token = User.new_token self.activation_digest = User.digest(self.activation_token) end def User.new_token SecureRandom.urlsafe_base64 end def User.digest(string) cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost BCrypt::Password.create(string, cost: cost) end
サーバーの反応は以下になります
Started GET "/account_activations/i788eilvO80XVhwNnomZJA/edit?email=apmjzdr3418%40yahoo.co.jp" for 175.177.40.32 at 2019-07-27 18:00:37 +0900 Cannot render console from 175.177.40.32! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AccountActivationsController#edit as HTML Parameters: {"email"=>"***(伏せてます)", "id"=>"i788eilvO80XVhwNnomZJA"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "apmjzdr3418@yahoo.co.jp"], ["LIMIT", 1]] Redirected to https://52aa539a3b004211bd41dd1eeed00741.vfs.cloud9.ap-southeast-1.amazonaws.com/ Completed 302 Found in 79ms (ActiveRecord: 0.2ms)
試したこと
authenticatedメソッド内の
BCrypt::Password.new(digest).is_password?(token)
のdigestとtokenの部分に"i788eilvO80XVhwNnomZJA"(トークン),
"$2a$10$a2q6NrZbZTWZ0Ep7J.J9XucLxFh199sfQG6D.Q1LjV8zdVCOHUNY2"(ダイジェスト)を直接打ち込んでもfalseが返ってくるので、もしかしたら上の文章が間違っているのかもしれないと色々調べたのですが、該当する理由が見つからず質問させて頂きました。
補足情報(FW/ツールのバージョンなど)
rails 5.1.6 ruby2.6.3
回答1件
あなたの回答
tips
プレビュー