feat(api_token): add api token controller

This commit is contained in:
Paul Chavard 2022-11-30 10:12:04 +01:00
parent ebbada752f
commit 3d1f57df83
3 changed files with 41 additions and 3 deletions

View file

@ -0,0 +1,38 @@
class APITokensController < ApplicationController
before_action :authenticate_administrateur!
def create
@api_token, @packed_token = APIToken.generate(current_administrateur)
respond_to do |format|
format.turbo_stream { render :index }
format.html { redirect_back(fallback_location: profil_path) }
end
end
def update
@api_token = current_administrateur.api_tokens.find(params[:id])
@api_token.update!(api_token_params)
respond_to do |format|
format.turbo_stream { render :index }
format.html { redirect_back(fallback_location: profil_path) }
end
end
def destroy
@api_token = current_administrateur.api_tokens.find(params[:id])
@api_token.destroy
respond_to do |format|
format.turbo_stream { render :index }
format.html { redirect_back(fallback_location: profil_path) }
end
end
private
def api_token_params
params.require(:api_token).permit(:name)
end
end

View file

@ -0,0 +1,2 @@
= turbo_stream.morph dom_id(current_administrateur, :profil_api_token) do
= render Profile::APITokenCardComponent.new created_api_token: @api_token, created_packed_token: @packed_token

View file

@ -173,6 +173,7 @@ Rails.application.routes.draw do
resources :attachments, only: [:show, :destroy]
resources :recherche, only: [:index]
resources :api_tokens, only: [:create, :update, :destroy]
get "patron" => "root#patron"
get "suivi" => "root#suivi"
@ -306,9 +307,6 @@ Rails.application.routes.draw do
get 'demarches' => 'demarches#index'
get 'profil' => 'profil#show'
post 'renew-api-token' => 'profil#renew_api_token'
# allow refresh 'renew api token' page
get 'renew-api-token' => redirect('/profil')
patch 'update_email' => 'profil#update_email'
post 'transfer_all_dossiers' => 'profil#transfer_all_dossiers'
post 'accept_merge' => 'profil#accept_merge'