gpt4 book ai didi

ruby-on-rails - 为什么在 Controller 中渲染时调用 current_user?

转载 作者:数据小太阳 更新时间:2023-10-29 07:15:52 25 4
gpt4 key购买 nike

尝试访问我的 sessions Controller 的登录方法时出现以下错误:

JWT::DecodeError (Nil JSON web token):

lib/json_web_token.rb:11:in `decode'
app/helpers/sessions_helper.rb:15:in `current_user'
app/controllers/api/sessions_controller.rb:11:in `create'

如果我在我的 Controller 响应中注释掉我的 render json: user,一切都很好,除了我需要响应用户...为什么 current_user 方法通过 sessions_controller.rb 的第 11 行调用。这是相关代码:

lib/json_web_token.rb

require 'jwt'

class JsonWebToken
def self.encode(payload, expiration = 24.hours.from_now)
payload = payload.dup
payload['exp'] = expiration.to_i
JWT.encode(payload, Rails.application.secrets.json_web_token_secret)
end

def self.decode(token)
JWT.decode(token, Rails.application.secrets.json_web_token_secret).first
end
end

sessions_helper.rb

require 'json_web_token'

module SessionsHelper
def create_session(user)
session[:user_id] = user.id
end

def current_user
auth_token = request.headers["Authorization"]
if auth_token
auth_token = auth_token.split(" ").last
begin
decoded_token = JsonWebToken.decode auth_token
rescue JWT::ExpiredSignature
return
end
@current_user ||= User.find_by(auth_token: auth_token)
end
end

def log_out(user)
logged_in? ? user.generate_authentication_token! : user.destroy_token!
auth_token = user.auth_token
user.update_attribute(:auth_token, auth_token)
end

def logged_in?
current_user.present?
end

def authenticate_with_token!
render json: { errors: "Not authenticated" }, status: :unauthorized unless logged_in?
end

def log_in(user)
create_session(user)
user.generate_authentication_token!
user.update_attribute(:auth_token, user.auth_token)
end

def authenticate_as_self_or_admin!
render json: { errors: "Not authorized" }, status: :unauthorized unless is_self? || is_admin?
end

def is_self?
user = User.find(params[:id])
auth_token = request.headers["Authorization"]
auth_token = auth_token.split(" ").last if auth_token
user.auth_token != auth_token
end

def is_admin?
if logged_in? && current_user.authenticate(params[:password])
current_user.admin
end
end
end

sessions_controller.rb

class Api::SessionsController < ApplicationController
before_action :authenticate_with_token!, only: [:destroy]
def new
end

def create
user = User.find_by(email: params[:session][:email].downcase)

if user && user.authenticate(params[:session][:password])
log_in user
render json: user, status: :created
else
render json: user, status: :unprocessable_entity
end
end

def destroy
log_out current_user
render status: 204
end
end

用户.rb

require 'json_web_token'

class User < ApplicationRecord
attr_reader :current_password

before_save { email.downcase! }
before_create :generate_authentication_token!
before_update :reset_confirmed!, :if => :email_changed?
has_secure_password
has_many :posts
has_many :comments
has_many :votes
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :username, presence: true, length: { maximum: 24 }, uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 8 }
validates :auth_token, uniqueness: true

def generate_authentication_token!
begin
self.auth_token = JsonWebToken.encode('id' => self.id, 'username' => self.username, 'email' => self.email, 'bio' => self.bio, 'confirmed' => self.confirmed, 'admin' => self.admin, 'points' => self.points)
end while self.class.exists?(auth_token: auth_token)
end

def destroy_token!
self.auth_token = nil
end

def reset_confirmed!
self.confirmed = false
end

def upvotes
self.votes.where(polarity: 1)
end

def downvotes
self.votes.where(polarity: -1)
end

def update_with_password(user_params)
current_password = user_params.delete(:current_password)
user_params[:password] = current_password if user_params[:password].nil?

if self.authenticate(current_password)
self.update(user_params)
else
self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
false
end
end
end

不,我没有使用设计。我真的希望我的眼睛在这里感到疲倦......

最佳答案

事实证明,current_user 实际上被调用了,因为它是事件模型序列化程序的默认 scope_name。我更改了 current_user 方法的名称以避免此冲突。 Here are the relevant docs.

关于ruby-on-rails - 为什么在 Controller 中渲染时调用 current_user?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39115305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com