diff --git a/Gemfile b/Gemfile index 6535ffa..b5b160d 100644 --- a/Gemfile +++ b/Gemfile @@ -21,7 +21,7 @@ gem 'jbuilder', '~> 2.7' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use Active Model has_secure_password -# gem 'bcrypt', '~> 3.1.7' + gem 'bcrypt', '~> 3.1.7' # Use Active Storage variant # gem 'image_processing', '~> 1.2' diff --git a/Gemfile.lock b/Gemfile.lock index 15bdfd6..c307c0d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -62,6 +62,7 @@ GEM zeitwerk (~> 2.3) addressable (2.7.0) public_suffix (>= 2.0.2, < 5.0) + bcrypt (3.1.16) bindex (0.8.1) bootsnap (1.7.3) msgpack (~> 1.0) @@ -205,6 +206,7 @@ PLATFORMS x86_64-darwin-20 DEPENDENCIES + bcrypt (~> 3.1.7) bootsnap (>= 1.4.4) byebug capybara (>= 3.26) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d1..b94b3bc 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,18 @@ class ApplicationController < ActionController::Base + before_action :authorized + 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 + + def authorized + redirect_to '/welcome' unless logged_in? + end + end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..0146b09 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,37 @@ +class SessionsController < ApplicationController + skip_before_action :authorized, only: [:new, :create, :welcome] + + def new + end + + def create + @user = User.find_by(username: params[:username]) + if @user && @user.authenticate(params[:password]) + session[:user_id] = @user.id + redirect_to welcome_path + else + flash[:warning] = "The username or password is incorrect:/" + redirect_to login_path + end + end + + def login + end + + def welcome + end + + def page_requires_login + + end + + def destroy + #clear the sessions[:user_id] + puts session[:user_id] + session.delete(:user_id) + #params[:id] = nil + #redirect the user back to the welcome page + + redirect_to '/welcome' + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..bf95d64 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,28 @@ +class UsersController < ApplicationController + skip_before_action :authorized, only: [:new, :create, :show] + + def new + @user = User.new + end + + def show + + end + + def create + @user = User.new(user_params) + if @user.save + session[:user_id] = @user.id + redirect_to welcome_path + else + flash[:warning] = "Please try again" + redirect_to new_user_path + end + end + + private + + def user_params + params.require(:user).permit(:unique_id, :username, :password) + end +end diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index f9b859b..79df9a7 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,4 +1,6 @@ class WelcomeController < ApplicationController + skip_before_action :authorized, only: [:index] + def index end end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..917e883 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,6 @@ +class User < ApplicationRecord + has_secure_password + + validates :username, presence: true, uniqueness: true + validates :password, presence: true +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ffa9ec0..4464b9a 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,8 +11,18 @@
-all code below this is written in app/views/welcome/index.html.erb
+