Skip to content

Commit be6ca5a

Browse files
committed
Implement basic login
1 parent c2ccd41 commit be6ca5a

36 files changed

+295
-38
lines changed

app/assets/config/manifest.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
//= link_tree ../images
22
//= link_directory ../stylesheets .css
3+
//= link_tree ../../javascript .js
4+
//= link_tree ../../../vendor/javascript .js

app/assets/stylesheets/custom.scss

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ footer {
102102
}
103103
}
104104
}
105+
@media (max-width: 800px) {
106+
footer {
107+
small {
108+
display: block;
109+
float: none;
110+
margin-bottom: 1em;
111+
}
112+
ul {
113+
float: none;
114+
padding: 0;
115+
li {
116+
float: none;
117+
margin-left: 0;
118+
}
119+
}
120+
}
121+
}
105122

106123
/* miscellaneous */
107124

@@ -110,6 +127,7 @@ footer {
110127
float: left;
111128
width: 100%;
112129
margin-top: 45px;
130+
@include box_sizing;
113131
}
114132

115133
/* sidebar */
@@ -176,3 +194,9 @@ input {
176194
color: $state-danger-text;
177195
}
178196
}
197+
198+
/* Dropdown menu */
199+
200+
.dropdown-menu.active {
201+
display: block;
202+
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
class ApplicationController < ActionController::Base
2-
3-
def hello
4-
render html: "hello, world!"
5-
end
2+
include SessionsHelper
63
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class SessionsController < ApplicationController
2+
3+
def new
4+
end
5+
6+
def create
7+
user = User.find_by(email: params[:session][:email].downcase)
8+
if user && user.authenticate(params[:session][:password])
9+
reset_session
10+
log_in user
11+
redirect_to user
12+
else
13+
flash.now[:danger] = 'Invalid email/password combination'
14+
render 'new', status: :unprocessable_entity
15+
end
16+
end
17+
18+
def destroy
19+
log_out
20+
redirect_to root_url, status: :see_other
21+
end
22+
end

app/controllers/static_pages_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def help
77

88
def about
99
end
10-
10+
1111
def contact
1212
end
1313
end

app/controllers/users_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ def new
1111
def create
1212
@user = User.new(user_params)
1313
if @user.save
14+
reset_session
15+
log_in @user
1416
flash[:success] = "Welcome to the Sample App!"
1517
redirect_to @user
1618
else
@@ -24,4 +26,4 @@ def user_params
2426
params.require(:user).permit(:name, :email, :password,
2527
:password_confirmation)
2628
end
27-
end
29+
end

app/helpers/sessions_helper.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module SessionsHelper
2+
3+
# Logs in the given user.
4+
def log_in(user)
5+
session[:user_id] = user.id
6+
end
7+
8+
# Returns the current logged-in user (if any).
9+
def current_user
10+
if session[:user_id]
11+
@current_user ||= User.find_by(id: session[:user_id])
12+
end
13+
end
14+
15+
# Returns true if the user is logged in, false otherwise.
16+
def logged_in?
17+
!current_user.nil?
18+
end
19+
20+
# Logs out the current user.
21+
def log_out
22+
reset_session
23+
@current_user = nil
24+
end
25+
end

app/javascript/application.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Configure your import map in config/importmap.rb.
2+
// Read more: https://github.com/rails/importmap-rails
3+
import "@hotwired/turbo-rails"
4+
import "controllers"
5+
import "custom/menu"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Application } from "@hotwired/stimulus"
2+
3+
const application = Application.start()
4+
5+
// Configure Stimulus development experience
6+
application.debug = false
7+
window.Stimulus = application
8+
9+
export { application }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
export default class extends Controller {
4+
connect() {
5+
this.element.textContent = "Hello World!"
6+
}
7+
}

0 commit comments

Comments
 (0)