Skip to content

Commit 39b38ee

Browse files
committed
feature/Add remote log in form
1 parent 5af4c43 commit 39b38ee

File tree

11 files changed

+91
-35
lines changed

11 files changed

+91
-35
lines changed

app/controllers/registrations_controller.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class RegistrationsController < Devise::RegistrationsController
22
def create
33
respond_to do |format|
44
format.html { super }
5-
format.json {
5+
format.json do
66
user = User.new(user_params)
77

88
if user.save
@@ -11,9 +11,8 @@ def create
1111
else
1212
render json: { message: 'There was a problem creating your account.' }, status: 400
1313
end
14-
}
14+
end
1515
end
16-
1716
end
1817

1918
def update
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class SessionsController < Devise::SessionsController
2+
def create
3+
respond_to do |format|
4+
format.html { super }
5+
format.json do
6+
user = User.authenticate(user_params)
7+
8+
if user
9+
sign_in(user)
10+
render json: { message: 'You are now logged in!' }
11+
else
12+
render json: { message: 'Invalid credentials.' }, status: 400
13+
end
14+
end
15+
end
16+
end
17+
18+
private
19+
20+
def user_params
21+
params.require(:user).permit(:email, :password)
22+
end
23+
end
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
class Users::ModalsController < ApplicationController
2-
def sign_in_sign_up
3-
render layout: false
4-
end
2+
layout false
3+
4+
def sign_in_sign_up; end
55

66
def sign_in
7-
render layout: false
7+
@user = User.new
88
end
99

1010
def sign_up
1111
@user = User.new
12-
13-
render layout: false
1412
end
1513
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Controller } from 'stimulus';
2+
3+
export default class extends Controller {
4+
5+
onSignInSuccess(event) {
6+
const [data, status, xhr] = event.detail;
7+
const urlWithToast = encodeURI(`${location.toString()}?notice=${data.message}`)
8+
}
9+
10+
onSignInError(event) {
11+
const [data, status, xhr] = event.detail;
12+
this.toast.display(data.message)
13+
}
14+
15+
get toast() {
16+
return document.getElementById('toast').toast
17+
}
18+
}

app/javascript/controllers/users_controller.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export default class extends Controller {
1212
presentModal(event) {
1313
event.stopPropagation()
1414
const target = event.currentTarget || event.target
15-
console.log('e', target.dataset)
1615
this.modal.present(target.dataset.modalUrl)
1716
}
1817

app/models/user.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ def snippets_for_feed
113113
Snippet.where(id: snippet_ids).order(created_at: :desc)
114114
end
115115

116+
def self.authenticate(params)
117+
user = find_for_authentication(email: params[:email])
118+
user&.valid_password?(params[:password]) ? user : nil
119+
end
120+
116121
private
117122

118123
def create_default_folder
Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
<card>
2-
<h2 style="margin-top: 0px;">Log in</h2>
1+
<h2 style="margin-top: 0px;">Log in</h2>
32

4-
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
5-
<div class="field">
6-
<%= f.label :email %><br />
7-
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
8-
</div>
3+
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
4+
<div class="field">
5+
<%= f.label :email %><br />
6+
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
7+
</div>
98

10-
<div class="field">
11-
<%= f.label :password %><br />
12-
<%= f.password_field :password, autocomplete: "current-password" %>
13-
</div>
9+
<div class="field">
10+
<%= f.label :password %><br />
11+
<%= f.password_field :password, autocomplete: "current-password" %>
12+
</div>
1413

15-
<% if devise_mapping.rememberable? %>
16-
<div class="field">
17-
<%= f.check_box :remember_me %>
18-
<%= f.label :remember_me %>
19-
</div>
20-
<% end %>
21-
22-
<div class="actions">
23-
<%= f.submit "Log in" %>
14+
<% if devise_mapping.rememberable? %>
15+
<div class="field">
16+
<%= f.check_box :remember_me %>
17+
<%= f.label :remember_me %>
2418
</div>
2519
<% end %>
2620

21+
<div class="actions">
22+
<%= f.submit "Log in" %>
23+
</div>
24+
<% end %>
25+
2726
<%= render "devise/shared/links" %>
28-
</card>

app/views/snippets/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<div>
99
<div style="display: flex; flex-direction: column;">
1010
<div style="display: flex; justify-content: flex-end; margin-bottom: 8px; align-items: center;">
11-
<% if comment.created_by?(current_user) %>
11+
<% if user_signed_in? && comment.created_by?(current_user) %>
1212
<%= render partial: 'shared/popover_trigger', locals: { popover_path: popover_comment_path(comment) } %>
1313
<% end %>
1414
</div>
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1-
<div >
1+
<div data-controller="sessions">
22
<h2 class="text-center">Sign in to your account</h2>
3+
4+
<%= form_for @user, url: user_session_path, remote: true, html: { data: { type: "json", action: "ajax:success->sessions#onSignInSuccess ajax:error->sessions#onSignInError" } } do |f| %>
5+
<div class="field">
6+
<%= f.label :email %><br />
7+
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
8+
</div>
9+
10+
<div class="field">
11+
<%= f.label :password %><br />
12+
<%= f.password_field :password, autocomplete: "current-password" %>
13+
</div>
14+
15+
<div class="actions">
16+
<%= f.submit "Log in" %>
17+
</div>
18+
<% end %>
319
</div>

app/views/users/modals/sign_up.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div data-controller="registrations">
22
<h2 class="text-center">Create an Account</h2>
33

4-
<%= form_for@user, url: user_registration_path, remote: true, html: { data: { type: "html", action: "ajax:success->registrations#onSignUpSuccess ajax:error->registrations#onSignUpError" } } do |f| %>
4+
<%= form_for @user, url: user_registration_path, remote: true, html: { data: { type: "json", action: "ajax:success->registrations#onSignUpSuccess ajax:error->registrations#onSignUpError" } } do |f| %>
55
<%# <%= render "devise/shared/error_messages", resource: @user %>
66

77
<div class="field">

0 commit comments

Comments
 (0)