Commit 3d8622e8 by liyijie

Finish user password function

parent 90f08b80
......@@ -2,6 +2,7 @@ require "rails_api_authentication/version"
require "rails_api_authentication/configuration"
require "rails_api_authentication/auth_action"
require "rails_api_authentication/auth_session"
require "rails_api_authentication/auth_password"
require "rails_api_authentication/auth_token"
require "rails_api_authentication/authable"
require "rails_api_authentication/acts_as_authenticationable"
......
......@@ -6,12 +6,16 @@ module RailsApiAuthentication
end
def acts_as_auth_session(klass_sym)
include RailsApiAuthentication::AuthAction
include RailsApiAuthentication::AuthSession
auth_action klass_sym, only: [:destroy]
auth_session klass_sym
end
def acts_as_auth_password(klass_sym)
include RailsApiAuthentication::AuthAction
include RailsApiAuthentication::AuthPassword
auth_action klass_sym, only: [:update]
auth_password klass_sym
end
end
......
......@@ -7,9 +7,11 @@ module RailsApiAuthentication
# Reset password with token
def create
auth_key = self.class.klass.auth_key
auth_password = self.class.klass.auth_password
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
rescue UserError => e
render json: { error: e.message }, status: e.status
......@@ -31,10 +33,11 @@ module RailsApiAuthentication
end
def reset_password_params
auth_key = self.class.klass.auth_key
auth_password = self.class.klass.auth_password
valid_key = self.class.klass.valid_key
params.require(self.class.klass_sym).permit(
auth_password, valid_key
auth_key, auth_password, valid_key
)
end
......
......@@ -13,11 +13,13 @@ module RailsApiAuthentication
def update_password password
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
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
......@@ -31,7 +33,7 @@ module RailsApiAuthentication
def valid_for params
@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
end
......@@ -56,7 +58,7 @@ module RailsApiAuthentication
def register(name, password, attrs={})
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)
rescue ActiveRecord::RecordInvalid => e
raise UserError.new(401, '-1', e.message)
......@@ -69,6 +71,10 @@ module RailsApiAuthentication
register(name, password, attrs)
end
def valid! name, valid_code
raise(UserError.new(401, '-1', 'valid token is not correct')) unless valid?(name, valid_code)
end
private
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