diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d1..1f1890d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 710db28..835e049 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -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 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0d7fd54..4698680 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,5 +1,6 @@ class UsersController < ApplicationController def new + @user = User.new end def create diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb index b39a3bc..c22d0c3 100644 --- a/app/views/sessions/new.html.erb +++ b/app/views/sessions/new.html.erb @@ -1,2 +1,8 @@ -

Sessions#new

-

Find me in app/views/sessions/new.html.erb

+

Login

+ <%= form_tag '/login' do %> + <%= label_tag :username%> + <%= text_field_tag :username %> + <%= label_tag :password%> + <%= password_field_tag :password%> + <%= submit_tag "Login"%> +<%end%> \ No newline at end of file diff --git a/app/views/sessions/welcome.html.erb b/app/views/sessions/welcome.html.erb index c63e0c1..71e1369 100644 --- a/app/views/sessions/welcome.html.erb +++ b/app/views/sessions/welcome.html.erb @@ -1,3 +1,6 @@

Welcome

+<% if logged_in? %> +

You are Logged In, <%= current_user.username %>

+<%end%> <%= button_to "Login", '/login', method: :get%> <%= button_to "Sign Up", '/users/new', method: :get%> \ No newline at end of file diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb new file mode 100644 index 0000000..cd64b2f --- /dev/null +++ b/spec/controllers/application_controller_spec.rb @@ -0,0 +1,18 @@ +require 'rails_helper' + +RSpec.describe ApplicationController do + describe 'current_user' do + before(:all) do + User.create(username: 'karol.selak@gmail.com', password: 'abcde') + end + context 'when a user is logged in' do + it 'returns the user' do + # TODO + # expect(current_user.username).to eql('karol.selak@gmail.com') + end + end + end + describe 'logged_in?' do + # TODO + end +end \ No newline at end of file diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 3a5b173..c959bc7 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -8,6 +8,11 @@ RSpec.describe SessionsController do end end describe 'get create' do + # TODO test session status + subject { get 'create' } + it 'redirects to /welcome' do + expect(subject).to redirect_to('/welcome') + end end describe 'get welcome' do subject { get 'welcome' } diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 56f7616..bf36687 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -8,5 +8,19 @@ RSpec.describe UsersController do end end describe 'get create' do + before(:all) do # TODO change it to cleanup after each test + User.destroy_all + end + subject { + get :create, params: {user: {username: 'karol.selak@gmail.com', password: 'abcde'}} + } + it 'creates a user' do + subject + expect(User.where(username: 'karol.selak@gmail.com').size).to eql(1) + end + it 'redirects to /welcome' do + subject + expect(subject).to redirect_to('/welcome') + end end end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ce33d66..383bef3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -93,4 +93,4 @@ RSpec.configure do |config| # as the one that triggered the failure. Kernel.srand config.seed =end -end +end \ No newline at end of file