mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
Add instructions to api key usagea and allow user generate new api key
This commit is contained in:
parent
cf71df3f0a
commit
b76f75a494
8 changed files with 51 additions and 6 deletions
File diff suppressed because one or more lines are too long
|
|
@ -1,6 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Api::V1::PointsController < ApplicationController
|
class Api::V1::Owntracks::PointsController < ApplicationController
|
||||||
skip_forgery_protection
|
skip_forgery_protection
|
||||||
before_action :authenticate_api_key
|
before_action :authenticate_api_key
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,4 +8,10 @@ class SettingsController < ApplicationController
|
||||||
|
|
||||||
redirect_back(fallback_location: root_path)
|
redirect_back(fallback_location: root_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def generate_api_key
|
||||||
|
current_user.update(api_key: SecureRandom.hex)
|
||||||
|
|
||||||
|
redirect_back(fallback_location: root_path)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,18 @@
|
||||||
<code><%= current_user.api_key %></code>
|
<code><%= current_user.api_key %></code>
|
||||||
<p class='py-2'>
|
<p class='py-2'>
|
||||||
Usage example:
|
Usage example:
|
||||||
<p><code><%= api_v1_points_url(api_key: current_user.api_key) %></code></p>
|
|
||||||
|
<div role="tablist" class="tabs tabs-boxed">
|
||||||
|
<input type="radio" name="my_tabs_2" role="tab" class="tab" aria-label="OwnTracks" />
|
||||||
|
<div role="tabpanel" class="tab-content bg-base-100 border-base-300 rounded-box p-6">
|
||||||
|
<p><code><%= api_v1_owntracks_points_url(api_key: current_user.api_key) %></code></p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<input type="radio" name="my_tabs_2" role="tab" class="tab" aria-label="Overland" checked />
|
||||||
|
<div role="tabpanel" class="tab-content bg-base-100 border-base-300 rounded-box p-6">
|
||||||
|
<p><code><%= api_v1_overland_batches_url(api_key: current_user.api_key) %></code></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</p>
|
</p>
|
||||||
<p class='py-2'>
|
<p class='py-2'>
|
||||||
<%= link_to "Generate new API key", generate_api_key_path, data: { confirm: "Are you sure?", turbo_confirm: "Are you sure?", turbo_method: :post }, class: 'btn btn-primary' %>
|
<%= link_to "Generate new API key", generate_api_key_path, data: { confirm: "Are you sure?", turbo_confirm: "Are you sure?", turbo_method: :post }, class: 'btn btn-primary' %>
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ Rails.application.routes.draw do
|
||||||
root to: 'home#index'
|
root to: 'home#index'
|
||||||
devise_for :users
|
devise_for :users
|
||||||
|
|
||||||
post 'settings/generate_api_key', to: 'devise/api_keys#create', as: :generate_api_key
|
post 'settings/generate_api_key', to: 'settings#generate_api_key', as: :generate_api_key
|
||||||
|
|
||||||
get 'map', to: 'map#index'
|
get 'map', to: 'map#index'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,7 @@ RSpec.describe Stat, type: :model do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#self.year_distance' do
|
describe '#self.year_distance' do
|
||||||
subject { described_class.year_distance(year) }
|
subject { described_class.year_distance(year, user) }
|
||||||
|
|
||||||
let(:user) { create(:user) }
|
let(:user) { create(:user) }
|
||||||
let(:expected_distance) do
|
let(:expected_distance) do
|
||||||
|
|
|
||||||
|
|
@ -40,4 +40,32 @@ RSpec.describe 'Settings', type: :request do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'POST /generate_api_key' do
|
||||||
|
context 'when user is not signed in' do
|
||||||
|
it 'redirects to the sign in page' do
|
||||||
|
post '/settings/generate_api_key'
|
||||||
|
|
||||||
|
expect(response).to redirect_to(new_user_session_path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when user is signed in' do
|
||||||
|
let(:user) { create(:user) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
sign_in user
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'generates an API key for the user' do
|
||||||
|
expect { post '/settings/generate_api_key' }.to change { user.reload.api_key }
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'redirects back' do
|
||||||
|
post '/settings/generate_api_key'
|
||||||
|
|
||||||
|
expect(response).to redirect_to(root_path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -180,7 +180,7 @@ paths:
|
||||||
lat: 52.502397
|
lat: 52.502397
|
||||||
lon: 13.356718
|
lon: 13.356718
|
||||||
tid: Swagger
|
tid: Swagger
|
||||||
tst: 1716636973
|
tst: 1716638268
|
||||||
servers:
|
servers:
|
||||||
- url: http://{defaultHost}
|
- url: http://{defaultHost}
|
||||||
variables:
|
variables:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue