Skip to content

Commit 88ef437

Browse files
committed
Created basic authentication and login screen. User can now login, or log off. author_sessions_controller manages new, create and destroy for accessing login and logout data
1 parent e070d34 commit 88ef437

File tree

8 files changed

+55
-1
lines changed

8 files changed

+55
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://coffeescript.org/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the AuthorSessions controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class AuthorSessionsController < ApplicationController
2+
def new
3+
end
4+
5+
def create
6+
if login(params[:email], params[:password])
7+
redirect_back_or_to(articles_path, notice: 'Logged in successfully.')
8+
else
9+
flash.now.alert = 'Login failed. Please try again.'
10+
render action: :new
11+
end
12+
end
13+
14+
def destroy
15+
logout
16+
redirect_to(:authors, notice: 'Logged Out!')
17+
end
18+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module AuthorSessionsHelper
2+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<h1>Login</h1>
2+
3+
<%= form_tag author_sessions_path, method: :post do %>
4+
<div class='field'>
5+
<%= label_tag :email %>
6+
<%= text_field_tag :email %><br>
7+
</div>
8+
<div class="field">
9+
<%= label_tag :password %>
10+
<%= password_field_tag :password %></br>
11+
</div>
12+
<div class="actions">
13+
<%= submit_tag "Login" %>
14+
</div>
15+
<% end %>
16+
17+
<%= link_to 'Back', articles_path %>

app/views/layouts/application.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
<h6>
2121
<% if logged_in? %>
2222
<%= "Logged in as #{current_user.email}" %>
23+
<%= link_to '(Logout)', logout_path %>
2324
<% else %>
24-
Logged Out
25+
<%= link_to '(Login)', login_path%>
2526
<% end %>
2627
</h6>
2728
</div>

config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
end
55
resources :tags
66
resources :authors
7+
resources :author_sessions, only: [:new, :create, :destroy]
8+
get 'login' => 'author_sessions#new'
9+
get 'logout' => 'author_sessions#destroy'
710

811
root to: 'articles#index'
912
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'test_helper'
2+
3+
class AuthorSessionsControllerTest < ActionDispatch::IntegrationTest
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

0 commit comments

Comments
 (0)