Class: Mangadex::Auth

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mangadex/auth.rb

Class Method Summary collapse

Class Method Details

.check_tokenObject



33
34
35
36
37
38
39
40
# File 'lib/mangadex/auth.rb', line 33

def self.check_token
  JSON.parse(
    Mangadex::Internal::Request.get(
      '/auth/check',
      raw: true,
    )
  )
end

.login(username, password) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mangadex/auth.rb', line 7

def self.(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

.logoutObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mangadex/auth.rb', line 43

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

.refresh_tokenObject



56
57
58
# File 'lib/mangadex/auth.rb', line 56

def self.refresh_token
  !(Mangadex::Api::Context.user&.refresh!).nil?
end