Commit b6a5ce44 by ivan Lan

Update register to make it can accept more attrs

parent 9980927d
...@@ -11,12 +11,12 @@ module RailsApiAuthentication ...@@ -11,12 +11,12 @@ module RailsApiAuthentication
@auth_token = self.class.klass.login(session_params[auth_key], session_params[auth_password]) @auth_token = self.class.klass.login(session_params[auth_key], session_params[auth_password])
render json: { token: @auth_token.token }, status: 200 render json: { token: @auth_token.token }, status: 200
rescue UserError => e rescue UserError => e
render json: e.message, status: e.status render json: { error: e.message }, status: e.status
end end
def destroy def destroy
self.send("current_#{self.class.klass_sym}")&.logout self.send("current_#{self.class.klass_sym}")&.logout
render json: "logout successful", status: 200 render json: { message: "logout successful" }, status: 200
end end
private private
......
...@@ -35,9 +35,23 @@ module RailsApiAuthentication ...@@ -35,9 +35,23 @@ module RailsApiAuthentication
user.nil? ? raise(UserError.new(401, '-1', 'Unauthorized')) : user user.nil? ? raise(UserError.new(401, '-1', 'Unauthorized')) : user
end end
def register(name, password) def register(attrs_or_name, password=nil)
if attrs_or_name.respond_to?(:to_h)
# attrs_or_name is an Hash or ActionController::Parameters
@_attrs_copy = attrs_or_name.clone
name = @_attrs_copy[@auth_key]
password = @_attrs_copy.delete(@auth_password)
else
name = attrs_or_name
end
raise(UserError.new(401, '-1', 'password is blank')) if password.blank? raise(UserError.new(401, '-1', 'password is blank')) if password.blank?
self.create!(@auth_key => name, @auth_password => generate_password(password))
self.create!({
@auth_key => name,
@auth_password => generate_password(password)
}.merge( @_attrs_copy || {} )
)
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
raise UserError.new(401, '-1', e.message) raise UserError.new(401, '-1', e.message)
end end
......
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