Class: Admin::TokensController

Inherits:
ApplicationController show all
Includes:
Koi::Controller::JsonWebToken
Defined in:
app/controllers/admin/tokens_controller.rb

Constant Summary

Constants included from Koi::Controller::JsonWebToken

Koi::Controller::JsonWebToken::SECRET_KEY

Instance Method Summary collapse

Methods included from Koi::Controller::JsonWebToken

#decode_token, #encode_token

Instance Method Details

#createObject



22
23
24
25
26
27
# File 'app/controllers/admin/tokens_controller.rb', line 22

def create
  admin = Admin::User.find(params[:id])
  token = encode_token(admin_id: admin.id, exp: 5.minutes.from_now.to_i, iat: Time.current.to_i)

  render locals: { token: }
end

#showObject



10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/admin/tokens_controller.rb', line 10

def show
  return redirect_to new_admin_session_path, notice: I18n.t("koi.auth.token_invalid") if @token.blank?

  admin = Admin::User.find(@token[:admin_id])

  if token_utilised?(admin, @token)
    return redirect_to new_admin_session_path, notice: I18n.t("koi.auth.token_invalid")
  end

  render locals: { admin:, token: params[:token] }, layout: "koi/login"
end

#updateObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/admin/tokens_controller.rb', line 29

def update
  return redirect_to admin_dashboard_path, status: :see_other if admin_signed_in?

  if @token.blank?
    return redirect_to new_admin_session_path, status: :see_other, notice: I18n.t("koi.auth.token_invalid")
  end

  admin = Admin::User.find(@token[:admin_id])
  (admin)

  redirect_to admin_admin_user_path(admin)
end