Commit b63b8f89 by liyijie

Merge branch 'lan/update_register' into 'master'

Update register to make it can accept more attrs See merge request !1
parents 9980927d cd30aecd
......@@ -11,12 +11,12 @@ module RailsApiAuthentication
@auth_token = self.class.klass.login(session_params[auth_key], session_params[auth_password])
render json: { token: @auth_token.token }, status: 200
rescue UserError => e
render json: e.message, status: e.status
render json: { error: e.message }, status: e.status
end
def destroy
self.send("current_#{self.class.klass_sym}")&.logout
render json: "logout successful", status: 200
render json: { message: "logout successful" }, status: 200
end
private
......
......@@ -35,13 +35,20 @@ module RailsApiAuthentication
user.nil? ? raise(UserError.new(401, '-1', 'Unauthorized')) : user
end
def register(name, password)
def register(name, password, attrs={})
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)
rescue ActiveRecord::RecordInvalid => e
raise UserError.new(401, '-1', e.message)
end
def register_with(attrs={})
attrs = attrs.clone
name = attrs.delete @auth_key
password = attrs.delete @auth_password
register(name, password, attrs)
end
private
def salt(password, suffix)
......@@ -78,3 +85,4 @@ module RailsApiAuthentication
end
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