Class: Mangadex::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/mangadex/auth.rb

Class Method Summary collapse

Class Method Details

.check_tokenObject



30
31
32
33
34
35
36
37
# File 'lib/mangadex/auth.rb', line 30

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

.login(username, password) ⇒ Object



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

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



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mangadex/auth.rb', line 39

def 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



51
52
53
# File 'lib/mangadex/auth.rb', line 51

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