Commit 3d8622e8 by liyijie

Finish user password function

parent 90f08b80
...@@ -2,6 +2,7 @@ require "rails_api_authentication/version" ...@@ -2,6 +2,7 @@ require "rails_api_authentication/version"
require "rails_api_authentication/configuration" require "rails_api_authentication/configuration"
require "rails_api_authentication/auth_action" require "rails_api_authentication/auth_action"
require "rails_api_authentication/auth_session" require "rails_api_authentication/auth_session"
require "rails_api_authentication/auth_password"
require "rails_api_authentication/auth_token" require "rails_api_authentication/auth_token"
require "rails_api_authentication/authable" require "rails_api_authentication/authable"
require "rails_api_authentication/acts_as_authenticationable" require "rails_api_authentication/acts_as_authenticationable"
......
...@@ -6,12 +6,16 @@ module RailsApiAuthentication ...@@ -6,12 +6,16 @@ module RailsApiAuthentication
end end
def acts_as_auth_session(klass_sym) def acts_as_auth_session(klass_sym)
include RailsApiAuthentication::AuthAction
include RailsApiAuthentication::AuthSession include RailsApiAuthentication::AuthSession
auth_action klass_sym, only: [:destroy]
auth_session klass_sym auth_session klass_sym
end end
def acts_as_auth_password(klass_sym) def acts_as_auth_password(klass_sym)
include RailsApiAuthentication::AuthAction
include RailsApiAuthentication::AuthPassword include RailsApiAuthentication::AuthPassword
auth_action klass_sym, only: [:update]
auth_password klass_sym auth_password klass_sym
end end
end end
......
...@@ -7,9 +7,11 @@ module RailsApiAuthentication ...@@ -7,9 +7,11 @@ module RailsApiAuthentication
# Reset password with token # Reset password with token
def create def create
auth_key = self.class.klass.auth_key
auth_password = self.class.klass.auth_password auth_password = self.class.klass.auth_password
valid_key = self.class.klass.valid_key valid_key = self.class.klass.valid_key
self.send("current_#{self.class.klass_sym}")&.reset_password(reset_password_params[auth_password], reset_password_params[valid_key]) current_authable = self.class.klass.send(:find_by!, auth_key => reset_password_params[auth_key])
current_authable.reset_password(reset_password_params[auth_password], reset_password_params[valid_key])
render json: { meesage: "reset password successful"}, status: 200 render json: { meesage: "reset password successful"}, status: 200
rescue UserError => e rescue UserError => e
render json: { error: e.message }, status: e.status render json: { error: e.message }, status: e.status
...@@ -31,10 +33,11 @@ module RailsApiAuthentication ...@@ -31,10 +33,11 @@ module RailsApiAuthentication
end end
def reset_password_params def reset_password_params
auth_key = self.class.klass.auth_key
auth_password = self.class.klass.auth_password auth_password = self.class.klass.auth_password
valid_key = self.class.klass.valid_key valid_key = self.class.klass.valid_key
params.require(self.class.klass_sym).permit( params.require(self.class.klass_sym).permit(
auth_password, valid_key auth_key, auth_password, valid_key
) )
end end
......
...@@ -13,11 +13,13 @@ module RailsApiAuthentication ...@@ -13,11 +13,13 @@ module RailsApiAuthentication
def update_password password def update_password password
raise(UserError.new(401, '-1', 'password is blank')) if password.blank? raise(UserError.new(401, '-1', 'password is blank')) if password.blank?
self.update(@auth_password => generate_password(password)) auth_password = self.class.auth_password
self.update(auth_password => self.class.send(:generate_password, password))
end end
def reset_password password, valid_code def reset_password password, valid_code
update_password(password) if self.class.valid?(self.send(@auth_key), valid_code) auth_key = self.class.auth_key
update_password(password) if self.class.valid!(self.send(auth_key), valid_code)
end end
end end
...@@ -31,7 +33,7 @@ module RailsApiAuthentication ...@@ -31,7 +33,7 @@ module RailsApiAuthentication
def valid_for params def valid_for params
@valid_key = params[:key]&.to_sym || :valid_code @valid_key = params[:key]&.to_sym || :valid_code
@valid_expire = params[:expire]&.to_sym || 60 @valid_expire = params[:expire]&.to_sym || 600
@valid_length = params[:length]&.to_sym || 4 @valid_length = params[:length]&.to_sym || 4
end end
...@@ -56,7 +58,7 @@ module RailsApiAuthentication ...@@ -56,7 +58,7 @@ module RailsApiAuthentication
def register(name, password, attrs={}) def register(name, password, attrs={})
raise(UserError.new(401, '-1', 'password is blank')) if password.blank? raise(UserError.new(401, '-1', 'password is blank')) if password.blank?
raise(UserError.new(401, '-1', 'valid token is not correct')) unless valid?(name, attrs.delete(@valid_key)) valid! name, attrs.delete(@valid_key)
self.create!({@auth_key => name, @auth_password => generate_password(password)}.merge attrs) self.create!({@auth_key => name, @auth_password => generate_password(password)}.merge attrs)
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
raise UserError.new(401, '-1', e.message) raise UserError.new(401, '-1', e.message)
...@@ -69,6 +71,10 @@ module RailsApiAuthentication ...@@ -69,6 +71,10 @@ module RailsApiAuthentication
register(name, password, attrs) register(name, password, attrs)
end end
def valid! name, valid_code
raise(UserError.new(401, '-1', 'valid token is not correct')) unless valid?(name, valid_code)
end
private private
def salt(password, suffix) def salt(password, suffix)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment