我正在尝试在Rails API应用程序中实现用户帐户。 我有用户逻辑工作注册和登录但我的问题是电子邮件链接是一个GET请求,所需的操作是POST。我可以在Postman中手动通过POST请求激活URL,如下所示:
http://localhost:3000/users/confirm-request?token=b96be863aced91480a2a
如何通过点击电子邮件中的链接来完成此操作?
我的用户控制器:
class UsersController < ApplicationController
def create
user = User.new(user_params)
if user.save
UserMailer.registration_confirmation(user).deliver
render json: { status: 201 }, status: :created
else
render json: { errors: user.errors.full_messages }, status: :bad_request
end
end
def confirm
token = params[:token].to_s
user = User.find_by(confirmation_token: token)
if user.present? && user.confirmation_token_valid?
user.mark_as_confirmed!
render json: {status: 'User confirmed successfully'}, status: :ok
else
render json: {status: 'Invalid token'}, status: :not_found
end
end
def login
user = User.find_by(email: params[:email].to_s.downcase)
if user && user.authenticate(params[:password])
if user.confirmed_at?
auth_token = JsonWebToken.encode({user_id: user.id})
render json: {auth_token: auth_token}, status: :ok
else
render json: {error: 'Email not verified' }, status: :unauthorized
end
else
render json: {error: 'Invalid username / password'}, status: :unauthorized
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
My Routes.rb:
Rails.application.routes.draw do
resources :users, only: :create do
collection do
post 'confirm'
post 'login'
end
end
registration_confirmation.text.erb:
Hi <%= @user.name %>,
Thanks for registering. To confirm your registration click the URL below.
<%= confirm_users_url(@user.confirmation_token) %>
答案 0 :(得分:1)
更改registration_confirmation.text.erb
Hi <%= @user.name %>,
Thanks for registering. To confirm your registration click the URL below.
<%#= confirm_users_url(token: @user.confirmation_token) %>
<a href="/users/confirm?token=<%=@user.confirmation_token%>"></a>
的routes.rb
Rails.application.routes.draw do
resources :users, only: :create do
collection do
get 'confirm'
post 'login'
end
end
end