Sha256: 7e6cf74293e3176f742b1134c748704425e0f31158af2b9687142b01cf3bac42

Contents?: true

Size: 1.56 KB

Versions: 5

Compression:

Stored size: 1.56 KB

Contents

# typed: false
module Mangadex
  class Auth
    extend T::Sig

    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?

      session = response.dig('token', 'session')
      refresh = response.dig('token', 'refresh')

      mangadex_user = Mangadex::Internal::Request.get('/user/me', headers: { Authorization: session })

      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

    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?

      Mangadex::Api::Context.user = nil
      true
    end

    sig { returns(T::Boolean) }
    def self.refresh_token
      !(Mangadex::Api::Context.user&.refresh!).nil?
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mangadex-5.3.3.1 lib/mangadex/auth.rb
mangadex-5.3.3 lib/mangadex/auth.rb
mangadex-5.3.2 lib/mangadex/auth.rb
mangadex-5.3.1.3 lib/mangadex/auth.rb
mangadex-5.3.1.2 lib/mangadex/auth.rb