session creation

This commit is contained in:
Karol Selak 2021-03-20 20:02:37 +01:00
parent 0b2bd70f16
commit f8093d2c09
9 changed files with 63 additions and 3 deletions

View file

@ -1,2 +1,10 @@
class ApplicationController < ActionController::Base
helper_method :current_user
helper_method :logged_in?
def current_user
User.find_by(id: session[:user_id])
end
def logged_in?
!current_user.nil?
end
end

View file

@ -3,6 +3,11 @@ class SessionsController < ApplicationController
end
def create
@user = User.find_by(username: params[:username])
if @user && @user.authenticate(params[:password])
sessions[:user_id] = @user.id
end
redirect_to '/welcome'
end
def welcome

View file

@ -1,5 +1,6 @@
class UsersController < ApplicationController
def new
@user = User.new
end
def create

View file

@ -1,2 +1,8 @@
<h1>Sessions#new</h1>
<p>Find me in app/views/sessions/new.html.erb</p>
<h1>Login</h1>
<%= form_tag '/login' do %>
<%= label_tag :username%>
<%= text_field_tag :username %>
<%= label_tag :password%>
<%= password_field_tag :password%>
<%= submit_tag "Login"%>
<%end%>

View file

@ -1,3 +1,6 @@
<h1>Welcome</h1>
<% if logged_in? %>
<h1>You are Logged In, <%= current_user.username %></h1>
<%end%>
<%= button_to "Login", '/login', method: :get%>
<%= button_to "Sign Up", '/users/new', method: :get%>