Permalink
Browse files

4. hook up the signup process using strategic javascript

  • Loading branch information...
cainlevy committed Nov 25, 2016
1 parent aae06eb commit f52a2794e80e7c13f62fb17930c4284c0892ed36
Showing with 60 additions and 3 deletions.
  1. +20 −2 app.rb
  2. +38 −0 public/application.js
  3. +1 −0 views/layout.erb
  4. +1 −1 views/signup.erb
View
22 app.rb
@@ -3,8 +3,10 @@
migration "create users table" do
database.create_table :users do
primary_key :id
integer :account_id
string :email
index :account_id, unique: true
index :email, unique: true
end
end
@@ -22,11 +24,15 @@ class User < Sequel::Model
helpers do
def logged_in?
!! current_user
!! current_account_id
end
def current_user
@current_user ||= User[id: nil]
@current_user ||= User[account_id: current_account_id]
end
def current_account_id
Keratin::AuthN.subject_from(request.cookies['authn'])
end
def gravatar_url(email, size: 200)
@@ -45,3 +51,15 @@ def gravatar_url(email, size: 200)
get '/signup' do
erb :signup
end
post '/signup' do
if logged_in?
user = User.create(
email: params[:user][:email],
account_id: current_account_id
)
redirect to('/')
else
erb :signup
end
end
View
@@ -0,0 +1,38 @@
(function() {
/*
* Integrate KeratinAuthN.signup
*/
var signupForm = document.querySelector("form#signup");
if (signupForm) {
var username = signupForm.querySelector("input#user_email");
var password = signupForm.querySelector("input#user_password");
signupForm.addEventListener('submit', function (event) {
event.preventDefault();
// remove any existing error feedback. it's a clean slate!
signupForm.querySelectorAll(".has-danger").forEach(function(e) {
e.classList.remove('has-danger');
});
function submitWithoutKeratin() {
signupForm.submit();
}
function showErrors(errorData) {
errorData.forEach(function(data) {
if (data.field === "username") {
username.parentNode.classList.add("has-danger");
} else if (data.field === "password") {
password.parentNode.classList.add("has-danger");
}
});
}
KeratinAuthN
.signup({ username: username.value, password: password.value })
.then(submitWithoutKeratin, showErrors);
})
}
})();
View
@@ -18,5 +18,6 @@
KeratinAuthN.setCookieStore('authn');
KeratinAuthN.restoreSession().catch(console.log);
</script>
<script src="application.js"></script>
</body>
</html>
View
@@ -1,6 +1,6 @@
<h2>Sign Up</h2>
<form method="post" action="/signup">
<form id="signup" method="post" action="/signup">
<!-- Email (Username) -->
<div class="form-group">

0 comments on commit f52a279

Please sign in to comment.