Commit c5640dd9 by liyijie

Add oauth relation with token

parent 1e9cf58f
......@@ -73,10 +73,25 @@ module RailsApiAuthentication
end
def oauth_login(oauth_type, oauth_id)
if @oauth_only.present?
user = self.find_or_create_by oauth_type: oauth_type, oauth_id: oauth_id
AuthToken.create(
self, {oid: user.id, oauth_type: oauth_type, oauth_id: oauth_id }
)
else
auth = AuthToken.find(oauth_type: oauth_type, oauth_id: oauth_id)&.first
user = self.find_by(id: auth.oid)
raise(UserError.new(401, '-1', 'Unauthorized')) unless user.present?
end
end
def oauth_relate(token, oauth_type, oauth_id)
auth = AuthToken.find(token: token)&.first
if auth.present? && self.find_by(id: auth.oid).present?
auth.update(oauth_type: oauth_type, oauth_id: oauth_id)
else
raise(UserError.new(401, '-1', 'Unauthorized')) unless user.present?
end
end
def auth!(request)
......
......@@ -12,11 +12,28 @@ module RailsApiAuthentication
render json: { error: e.message }, status: e.status
end
def update
@auth_token = self.class.klass.oauth_relate(
session_relate_params.delete(:token),
session_relate_params.delete(:oauth_type),
session_relate_params.delete(:oauth_id),
)
render json: { token: @auth_token.token }, status: 200
rescue UserError => e
render json: { error: e.message }, status: e.status
end
def destroy
self.send("current_#{self.class.klass_sym}")&.logout
render json: { message: "logout successful" }, status: 200
end
def session_relate_params
params.require(self.class.klass_sym).permit(
:oauth_type, :oauth_id, :token
)
end
def session_params
params.require(self.class.klass_sym).permit(:oauth_type, :oauth_id)
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