Class: CoPlan::Settings::TokensController

Inherits:
ApplicationController show all
Defined in:
app/controllers/coplan/settings/tokens_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

controller_path

Instance Method Details

#createObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/coplan/settings/tokens_controller.rb', line 8

def create
  @api_token, @raw_token = ApiToken.create_with_raw_token(user: current_user, name: params[:api_token][:name])
  @api_tokens = current_user.api_tokens.order(created_at: :desc)

  respond_to do |format|
    format.turbo_stream
    format.html do
      flash[:raw_token] = @raw_token
      flash[:notice] = "Token created. Copy it now — it won't be shown again."
      redirect_to settings_tokens_path, status: :see_other
    end
  end
rescue ActiveRecord::RecordInvalid => e
  @api_tokens = current_user.api_tokens.order(created_at: :desc)
  flash.now[:alert] = e.message
  render :index, status: :unprocessable_entity
end

#destroyObject



26
27
28
29
30
31
32
33
34
# File 'app/controllers/coplan/settings/tokens_controller.rb', line 26

def destroy
  @token = current_user.api_tokens.find(params[:id])
  @token.revoke!

  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to settings_tokens_path, notice: "Token revoked.", status: :see_other }
  end
end

#indexObject



4
5
6
# File 'app/controllers/coplan/settings/tokens_controller.rb', line 4

def index
  @api_tokens = current_user.api_tokens.order(created_at: :desc)
end