Class: Holivia::Auth

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

Instance Method Summary collapse

Constructor Details

#initialize(client: Client.new(auth: nil)) ⇒ Auth

Returns a new instance of Auth.



8
9
10
# File 'lib/holivia/auth.rb', line 8

def initialize(client: Client.new(auth: nil))
  @client = client
end

Instance Method Details

#access_tokenObject



34
35
36
# File 'lib/holivia/auth.rb', line 34

def access_token
  credentials&.dig("access_token")
end

#login(email:, password:) ⇒ Object



12
13
14
15
16
# File 'lib/holivia/auth.rb', line 12

def (email:, password:)
  response = client.post("/api/v1/auth/login", body: { email: email, password: password })
  store_tokens(response)
  response
end

#logoutObject

Raises:



26
27
28
29
30
31
32
# File 'lib/holivia/auth.rb', line 26

def logout
  raise ApiError.new("Already logged out.", status: 401) unless access_token

  response = client.delete("/api/v1/auth/logout", headers: { "Authorization" => "Bearer #{access_token}" })
  FileUtils.rm_f(credentials_path)
  response
end

#refreshObject

Raises:



18
19
20
21
22
23
24
# File 'lib/holivia/auth.rb', line 18

def refresh
  raise ApiError.new("No refresh token. Run `holivia login`.", status: 401) unless refresh_token

  response = client.post("/api/v1/auth/refresh", headers: { "Authorization" => "Bearer #{refresh_token}" })
  store_tokens(response)
  response
end

#refresh_tokenObject



38
39
40
# File 'lib/holivia/auth.rb', line 38

def refresh_token
  credentials&.dig("refresh_token")
end