Class: DeviseTokenAuth::PasswordsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- DeviseTokenAuth::PasswordsController
- Defined in:
- app/controllers/devise_token_auth/passwords_controller.rb
Instance Method Summary collapse
-
#create ⇒ Object
this action is responsible for generating password reset tokens and sending emails.
-
#edit ⇒ Object
this is where users arrive after visiting the email confirmation link.
- #password_resource_params ⇒ Object
- #resource_params ⇒ Object
- #update ⇒ Object
Instance Method Details
#create ⇒ Object
this action is responsible for generating password reset tokens and sending emails
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/controllers/devise_token_auth/passwords_controller.rb', line 8 def create unless resource_params[:email] return render json: { success: false, errors: ['You must provide an email address.'] }, status: 401 end unless params[:redirect_url] return render json: { success: false, errors: ['Missing redirect url.'] }, status: 401 end @user = resource_class.where({ email: resource_params[:email], provider: 'email' }).first errors = nil if @user @user.send_reset_password_instructions({ email: resource_params[:email], provider: 'email', redirect_url: params[:redirect_url], client_config: params[:config_name] }) if @user.errors.empty? render json: { success: true, message: "An email has been sent to #{@user.email} containing "+ "instructions for resetting your password." } else errors = @user.errors end else errors = ["Unable to find user with email '#{resource_params[:email]}'."] end if errors render json: { success: false, errors: errors }, status: 400 end end |
#edit ⇒ Object
this is where users arrive after visiting the email confirmation link
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/controllers/devise_token_auth/passwords_controller.rb', line 61 def edit @user = resource_class.reset_password_by_token({ reset_password_token: resource_params[:reset_password_token] }) if @user and @user.id client_id = SecureRandom.urlsafe_base64(nil, false) token = SecureRandom.urlsafe_base64(nil, false) token_hash = BCrypt::Password.create(token) expiry = (Time.now + DeviseTokenAuth.token_lifespan).to_i @user.tokens[client_id] = { token: token_hash, expiry: expiry } # ensure that user is confirmed @user.skip_confirmation! unless @user.confirmed_at @user.save! redirect_to(@user.build_auth_url(params[:redirect_url], { token: token, client_id: client_id, reset_password: true, config: params[:config] })) else raise ActionController::RoutingError.new('Not Found') end end |
#password_resource_params ⇒ Object
135 136 137 |
# File 'app/controllers/devise_token_auth/passwords_controller.rb', line 135 def password_resource_params params.permit(devise_parameter_sanitizer.for(:account_update)) end |
#resource_params ⇒ Object
139 140 141 |
# File 'app/controllers/devise_token_auth/passwords_controller.rb', line 139 def resource_params params.permit(:email, :password, :password_confirmation, :reset_password_token) end |
#update ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/controllers/devise_token_auth/passwords_controller.rb', line 93 def update # make sure user is authorized unless @user return render json: { success: false, errors: ['Unauthorized'] }, status: 401 end # make sure account doesn't use oauth2 provider unless @user.provider == 'email' return render json: { success: false, errors: ["This account does not require a password. Sign in using "+ "your #{@user.provider.humanize} account instead."] }, status: 422 end # ensure that password params were sent unless password_resource_params[:password] and password_resource_params[:password_confirmation] return render json: { success: false, errors: ['You must fill out the fields labeled "password" and "password confirmation".'] }, status: 422 end if @user.update_attributes(password_resource_params) return render json: { success: true, data: { user: @user, message: "Your password has been successfully updated." } } else return render json: { success: false, errors: @user.errors }, status: 422 end end |