lib/mangadex/auth.rb in mangadex-5.3.1.1 vs lib/mangadex/auth.rb in mangadex-5.3.1.2

- old
+ new

@@ -1,56 +1,60 @@ # typed: false module Mangadex class Auth - class << self - def login(username, password) - response = Mangadex::Internal::Request.post( - '/auth/login', - payload: { - username: username, - password: password, - }, - ) - return response if response.is_a?(Mangadex::Api::Response) && response.errored? + extend T::Sig - session = response.dig('token', 'session') - refresh = response.dig('token', 'refresh') + sig { params(username: String, password: String).returns(T.any(T::Boolean, Mangadex::Api::Response)) } + def self.login(username, password) + response = Mangadex::Internal::Request.post( + '/auth/login', + payload: { + username: username, + password: password, + }, + ) + return response if response.is_a?(Mangadex::Api::Response) && response.errored? - mangadex_user = Mangadex::Internal::Request.get('/user/me', headers: { Authorization: session }) + session = response.dig('token', 'session') + refresh = response.dig('token', 'refresh') - user = Mangadex::Api::User.new( - mangadex_user.data.id, - session: session, - refresh: refresh, - data: mangadex_user.data, - ) - Mangadex::Api::Context.user = user - !user.session_expired? - end + mangadex_user = Mangadex::Internal::Request.get('/user/me', headers: { Authorization: session }) - def check_token - JSON.parse( - Mangadex::Internal::Request.get( - '/auth/check', - raw: true, - ) + user = Mangadex::Api::User.new( + mangadex_user.data.id, + session: session, + refresh: refresh, + data: mangadex_user.data, + ) + Mangadex::Api::Context.user = user + !user.session_expired? + end + + sig { returns(Hash) } + def self.check_token + JSON.parse( + Mangadex::Internal::Request.get( + '/auth/check', + raw: true, ) - end + ) + end - def logout - return true if Mangadex::Api::Context.user.nil? + sig { returns(T.any(T::Boolean, Mangadex::Api::Response)) } + def self.logout + return true if Mangadex::Api::Context.user.nil? - response = Mangadex::Internal::Request.post( - '/auth/logout', - ) - return reponse if response.is_a?(Mangadex::Api::Response) && response.errored? + response = Mangadex::Internal::Request.post( + '/auth/logout', + ) + return reponse if response.is_a?(Mangadex::Api::Response) && response.errored? - Mangadex::Api::Context.user = nil - true - end + Mangadex::Api::Context.user = nil + true + end - def refresh_token - !(Mangadex::Api::Context.user&.refresh!).nil? - end + sig { returns(T::Boolean) } + def self.refresh_token + !(Mangadex::Api::Context.user&.refresh!).nil? end end end