Skip to content

Commit

Permalink
sessions create and destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
gouravthakur39 committed Sep 6, 2019
1 parent 5b69d74 commit 8e75d3f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
18 changes: 18 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,22 @@ def new

end

def create
user = User.find_by(username: params[:session][:username])
if user && user.authenticate(params[:session][:password])
session[:user_id] = user.id
flash[:success] = "You have successfully logged in"
redirect_to root_path
else
flash.now[:error] = "There was something wrong with your login information"
render 'new'
end
end

def destroy
session[:user_id] = nil
flash[:success] = "You have successfully logged out"
redirect_to login_path
end

end
14 changes: 9 additions & 5 deletions app/views/layouts/_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
<div class="ui dropdown item">
Account <i class="dropdown icon"></i>
<div class="menu">
<a class="item">Log out</a>
<% if logged_in? %>
<%= link_to "Log out" , logout_path, method: :delete, class: "item" %>
<% else %>
<%= link_to "Log in", login_path, class: "item" %>
<a class="item">Sign up</a>
<% end %>
</div>
</div>
<div class="item">
<div class="ui primary button">Sign Up</div>
</div>
<% if !logged_in? %>
<div class="item">
<%= link_to "Log in", login_path, class: "ui primary button" %>
</div>
<% end %>
</div>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rails.application.routes.draw do
root 'chatroom#index'
get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy'
end

0 comments on commit 8e75d3f

Please sign in to comment.