2022-01-22 18:17:44 +01:00
|
|
|
class AccountsController < ApplicationController
|
|
|
|
before_action :authenticate_user!, only: [:edit, :destroy, :update]
|
|
|
|
before_action :redirect_if_authenticated, only: [:create, :new]
|
|
|
|
|
|
|
|
def create
|
|
|
|
@user = User.new(create_user_params)
|
|
|
|
if @user.save
|
|
|
|
@user.send_confirmation_email!
|
|
|
|
redirect_to root_path, notice: "Please check your email for confirmation instructions."
|
|
|
|
else
|
|
|
|
render :new, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
current_user.destroy
|
|
|
|
reset_session
|
|
|
|
redirect_to root_path, notice: "Your account has been deleted."
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
@user = current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@user = User.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@user = current_user
|
|
|
|
if @user.authenticate(params[:user][:current_password])
|
|
|
|
if @user.update(update_user_params)
|
|
|
|
if params[:user][:unconfirmed_email].present?
|
|
|
|
@user.send_confirmation_email!
|
|
|
|
redirect_to root_path, notice: "Check your email for confirmation instructions."
|
|
|
|
else
|
|
|
|
redirect_to root_path, notice: "Account updated."
|
|
|
|
end
|
|
|
|
else
|
|
|
|
render :edit, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
else
|
|
|
|
flash.now[:error] = "Incorrect password"
|
|
|
|
render :edit, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_user_params
|
2022-01-22 18:48:11 +01:00
|
|
|
params.require(:user).permit(:email, :password, :password_confirmation, :name)
|
2022-01-22 18:17:44 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_user_params
|
2022-01-22 18:48:11 +01:00
|
|
|
params.require(:user).permit(:current_password, :password, :password_confirmation, :unconfirmed_email, :name)
|
2022-01-22 18:17:44 +01:00
|
|
|
end
|
|
|
|
end
|