Skip to content

Commit 6d7dd7a

Browse files
committed
✨Don't change current alert system, but rework jwt display index.html.erb in JwtsController#index, to a more user friendly way.
1 parent bcb5090 commit 6d7dd7a

5 files changed

Lines changed: 50 additions & 0 deletions

File tree

app/assets/javascripts/main.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ document.addEventListener("turbolinks:load", function() {
4545
});
4646
}
4747

48+
var copyButtons = document.querySelectorAll('[data-copy-target]');
49+
if (copyButtons.length) {
50+
for (var i = 0; i < copyButtons.length; i++) {
51+
copyButtons[i].addEventListener('click', function() {
52+
var target = document.getElementById(this.getAttribute('data-copy-target'));
53+
if (!target) {
54+
return;
55+
}
56+
target.focus();
57+
target.select();
58+
if (navigator.clipboard && navigator.clipboard.writeText) {
59+
navigator.clipboard.writeText(target.value);
60+
} else {
61+
document.execCommand('copy');
62+
}
63+
var copiedText = this.getAttribute('data-copied-text');
64+
var label = this.querySelector('span');
65+
if (copiedText && label) {
66+
label.textContent = copiedText;
67+
}
68+
});
69+
}
70+
}
71+
4872
// Login Page Flipbox control
4973
$('.login-content [data-toggle="flip"]').click(function() {
5074
$('.login-box').toggleClass('flipped');

app/controllers/jwts_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def create
1414
jwt = Warden::JWTAuth::TokenEncoder.new.call(payload)
1515
Rails.logger.debug "jwt: #{jwt}"
1616
if @user.allowlisted_jwts.create(jti: payload['jti'], aud: payload['aud'], exp: Time.at(payload['exp']))
17+
flash[:jwt] = jwt
1718
redirect_to jwts_path, alert: t('.created', jwt: jwt)
1819
else
1920
render :index

app/views/jwts/index.html.erb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@
3131
</div>
3232
<div><%= submit_tag t(".actions.create"), class: 'btn btn-primary' %></div>
3333
<% end -%>
34+
<% if flash[:jwt].present? %>
35+
<div class="alert alert-info mt-3" role="alert">
36+
<h4 class="alert-heading"><%= t(".token_title") %></h4>
37+
<p><%= t(".token_help") %></p>
38+
<div class="form-group">
39+
<label for="new-jwt-token"><%= t(".token_label") %></label>
40+
<textarea id="new-jwt-token" class="form-control" rows="3" readonly style="font-family: monospace;"><%= flash[:jwt] %></textarea>
41+
</div>
42+
<p class="mb-0">
43+
<button type="button" class="btn btn-secondary icon-btn" data-copy-target="new-jwt-token" data-copied-text="<%= t(".token_copied") %>">
44+
<i class="fa fa-clipboard"></i> <span><%= t(".token_copy") %></span>
45+
</button>
46+
</p>
47+
</div>
48+
<% end %>
3449
<table class="table table-hover table-bordered mt-3">
3550
<thead>
3651
<tr>

config/locales/en.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ en:
101101
title: "Management application and its JWT authorization"
102102
your_jwts_list: "Your JWT authorization list"
103103
clean_expired_jwts: "Clean expired JWTs"
104+
token_title: "New JWT token"
105+
token_help: "Copy this token now. It will only be shown once."
106+
token_label: "JWT token"
107+
token_copy: "Copy token"
108+
token_copied: "Copied"
104109
form:
105110
aud: "Audience"
106111
exp_hours: "Expiration hours"

config/locales/zh-CN.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ zh-CN:
101101
title: "管理应用程序及其JWT授权"
102102
your_jwts_list: "您的JWT授权列表"
103103
clean_expired_jwts: "清理已经过期的JWT"
104+
token_title: "新的JWT令牌"
105+
token_help: "请立即复制该令牌,它只会显示一次。"
106+
token_label: "JWT 令牌"
107+
token_copy: "复制令牌"
108+
token_copied: "已复制"
104109
form:
105110
aud: "Audience"
106111
exp_hours: "过期时间(小时)"

0 commit comments

Comments
 (0)