password generation

This commit is contained in:
Mark Moser 2016-09-28 22:39:42 -05:00
parent 2a10a93ac9
commit 7d7e040c2b
5 changed files with 20 additions and 2 deletions

View File

@ -18,6 +18,10 @@ function revealPassword($src){
// Use this instead of typical $().ready // Use this instead of typical $().ready
// because turbolinks. // because turbolinks.
document.addEventListener("turbolinks:load", function() { document.addEventListener("turbolinks:load", function() {
$("[data-id=genpass]").on("ajax:success", "a", function(e, data){
$(e.target).parent().find("input").val(data.hash);
});
$("[data-id=passwd]").on("ajax:success", "a", function(e, data){ $("[data-id=passwd]").on("ajax:success", "a", function(e, data){
getPassword($(e.target).parent(), data.hash); getPassword($(e.target).parent(), data.hash);
}); });

View File

@ -10,7 +10,7 @@ class AccountsController < ApplicationController
end end
def new def new
@account = Account.new @account = Account.new(password: SecureRandom.urlsafe_base64(12))
end end
def edit def edit
@ -20,6 +20,10 @@ class AccountsController < ApplicationController
render json: { hash: @account.password }.to_json render json: { hash: @account.password }.to_json
end end
def genpass
render json: { hash: SecureRandom.urlsafe_base64(12) }.to_json
end
def create def create
@account = Account.new(account_params) @account = Account.new(account_params)

View File

@ -16,9 +16,10 @@
<%= f.text_field :username %> <%= f.text_field :username %>
</div> </div>
<div class="field"> <div class="field" data-id="genpass">
<%= f.label :password %> <%= f.label :password %>
<%= f.text_field :password %> <%= f.text_field :password %>
<%= link_to 'Generate', genpass_url, remote: true %>
</div> </div>
<div class="field"> <div class="field">

View File

@ -1,5 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
Rails.application.routes.draw do Rails.application.routes.draw do
get 'accounts/genpass', to: 'accounts#genpass', as: :genpass
get 'accounts/reveal/:id', to: 'accounts#reveal', as: :reveal_password get 'accounts/reveal/:id', to: 'accounts#reveal', as: :reveal_password
resources :accounts, except: [:destroy] resources :accounts, except: [:destroy]

View File

@ -37,4 +37,12 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
assert_response :success assert_response :success
assert_match '1q2w3e4r5t6y7u', json['hash'] assert_match '1q2w3e4r5t6y7u', json['hash']
end end
test 'genpass should provide password' do
get genpass_url, xhr: true
json = JSON.parse(response.body).to_hash
assert_response :success
assert json['hash']
end
end end