Skip to content
This repository was archived by the owner on Mar 9, 2020. It is now read-only.

Commit 0738413

Browse files
committed
Implement login/logout flow.
1 parent b61600f commit 0738413

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
11
class SessionsController < ApplicationController
2+
3+
def create
4+
user = login(params[:email], params[:password], params[:remember_me])
5+
if user
6+
redirect_back_or_to root_url, :notice => "Logged in!"
7+
else
8+
flash.now.alert = "Email or password was invalid"
9+
render :new
10+
end
11+
end
12+
13+
def destroy
14+
logout
15+
redirect_to root_url, :notice => "Logged out!"
16+
end
17+
218
end

app/views/layouts/application.html.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,13 @@
1010

1111
<%= yield %>
1212

13+
<% if current_user %>
14+
Logged in as <%= current_user.email %>.
15+
<%= link_to "Log out", logout_path %>
16+
<% else %>
17+
<%= link_to "Sign up", signup_path %> or
18+
<%= link_to "log in", login_path %>.
19+
<% end %>
20+
1321
</body>
1422
</html>

app/views/sessions/new.html.erb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<%= form_tag sessions_path do %>
2+
<div class="field">
3+
<%= label_tag :email %>
4+
<%= text_field_tag :email, params[:email] %>
5+
</div>
6+
<div class="field">
7+
<%= label_tag :password %>
8+
<%= password_field_tag :password %>
9+
</div>
10+
<div class="field">
11+
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
12+
<%= label_tag :remember_me %>
13+
</div>
14+
<div class="actions"><%= submit_tag "Log in" %></div>
15+
<% end %>

config/routes.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
# You can have the root of your site routed with "root"
88
root 'welcome#index'
99

10+
# authentication routes for sorcery
11+
get "logout" => "sessions#destroy", :as => "logout"
12+
get "login" => "sessions#new", :as => "login"
13+
get "signup" => "people#new", :as => "signup"
14+
1015
# Example of regular route:
1116
# get 'products/:id' => 'catalog#view'
1217

@@ -17,6 +22,7 @@
1722
# resources :products
1823

1924
resources :people
25+
resources :sessions
2026

2127
# Example resource route with options:
2228
# resources :products do

0 commit comments

Comments
 (0)