diff --git a/Gemfile b/Gemfile index 8733eff..8bcb4ae 100644 --- a/Gemfile +++ b/Gemfile @@ -24,7 +24,7 @@ gem 'jbuilder', '~> 2.0' gem 'sdoc', '~> 0.4.0', group: :doc # Use ActiveModel has_secure_password -# gem 'bcrypt', '~> 3.1.7' +gem 'bcrypt', '~> 3.1.7' # Use Unicorn as the app server # gem 'unicorn' @@ -44,4 +44,3 @@ group :development do # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end - diff --git a/Gemfile.lock b/Gemfile.lock index 3e86bed..da1b023 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,6 +37,7 @@ GEM thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) arel (6.0.3) + bcrypt (3.1.10) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) builder (3.2.2) @@ -139,6 +140,7 @@ PLATFORMS ruby DEPENDENCIES + bcrypt (~> 3.1.7) byebug coffee-rails (~> 4.1.0) jbuilder (~> 2.0) diff --git a/app/assets/javascripts/session.coffee b/app/assets/javascripts/session.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/session.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/users.coffee b/app/assets/javascripts/users.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/users.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index f9cd5b3..125c24e 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -13,3 +13,11 @@ *= require_tree . *= require_self */ + +div.alert { + background: pink; + border: 1px solid black; + border-radius: 7px; + padding: 1rem; + font-size: 1.4rem; +} diff --git a/app/assets/stylesheets/session.scss b/app/assets/stylesheets/session.scss new file mode 100644 index 0000000..6fa5e44 --- /dev/null +++ b/app/assets/stylesheets/session.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the session controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/users.scss b/app/assets/stylesheets/users.scss new file mode 100644 index 0000000..1efc835 --- /dev/null +++ b/app/assets/stylesheets/users.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the users controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..9617794 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,26 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + + helper_method :current_user + + def welcome + render '/welcome' + end + + def amiloggedin + amiloggedin = !!session[:current_user_id] + + render json: current_user + end + + private + + def current_user + if session[:current_user_id] + @current_user ||= User.find(session[:current_user_id]) + else + @current_user = false + end + end end diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb new file mode 100644 index 0000000..e9743d4 --- /dev/null +++ b/app/controllers/session_controller.rb @@ -0,0 +1,27 @@ +class SessionController < ApplicationController + + def create + user = User.find_by(email: user_params[:email]) + + if user && user.authenticate(user_params[:password]) + session[:current_user_id] = user.id + flash[:message] = "Thanks for logging in, sinner." + else + flash[:message] = "Email / Password combo does not exist!" + end + + redirect_to root_path + end + + def destroy + session[:current_user_id] = nil + + redirect_to root_path + end + + private + + def user_params + return params.require(:user).permit(:email, :password) + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..b2263af --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,20 @@ +class UsersController < ApplicationController + + def create + @user = User.new(user_params) + + if @user.save + flash[:message] = "Good job, you're now an active sinner. Log in, to repent!" + else + flash[:message] = @user.errors.full_messages.to_sentence + end + + redirect_to root_path + end + + private + + def user_params + return params.require(:user).permit(:email, :password) + end +end diff --git a/app/helpers/session_helper.rb b/app/helpers/session_helper.rb new file mode 100644 index 0000000..f867f86 --- /dev/null +++ b/app/helpers/session_helper.rb @@ -0,0 +1,2 @@ +module SessionHelper +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 0000000..2310a24 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,2 @@ +module UsersHelper +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..b6922d9 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,10 @@ +class User < ActiveRecord::Base + has_secure_password + + validates :email, presence: true, uniqueness: true + validates :password_digest, presence: true + + validates :password, length: { + minimum: 8, allow_nil: true + } +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index a1d7db5..46ecdfa 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -7,8 +7,22 @@ <%= csrf_meta_tags %>
+ <% if flash[:message] %> +