Class: Holivia::Auth
- Inherits:
-
Object
- Object
- Holivia::Auth
- Defined in:
- lib/holivia/auth.rb
Instance Method Summary collapse
- #access_token ⇒ Object
-
#initialize(client: Client.new(auth: nil)) ⇒ Auth
constructor
A new instance of Auth.
- #login(email:, password:) ⇒ Object
- #logout ⇒ Object
- #refresh ⇒ Object
- #refresh_token ⇒ Object
Constructor Details
Instance Method Details
#access_token ⇒ Object
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 login(email:, password:) response = client.post("/api/v1/auth/login", body: { email: email, password: password }) store_tokens(response) response end |
#logout ⇒ Object
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 |
#refresh ⇒ Object
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_token ⇒ Object
38 39 40 |
# File 'lib/holivia/auth.rb', line 38 def refresh_token credentials&.dig("refresh_token") end |