diff --git a/Gemfile b/Gemfile index 6e9abb1..d7ec304 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,8 @@ gem 'turbolinks' gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc +# Devise gem for authentication +gem 'devise', '~> 3.5.2' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' @@ -53,4 +55,7 @@ group :development, :test do gem 'faker' # Use database_cleaner in place of transactional fixtures gem 'database_cleaner' + # Use pry-rails for debugging + gem 'pry-rails' + gem "pry-byebug" end diff --git a/Gemfile.lock b/Gemfile.lock index 0afe943..5a49e22 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -40,13 +40,15 @@ GEM autoprefixer-rails (6.0.3) execjs json + bcrypt (3.1.10) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) bootstrap-sass (3.3.5.1) autoprefixer-rails (>= 5.0.0.1) sass (>= 3.3.0) builder (3.2.2) - byebug (6.0.2) + byebug (5.0.0) + columnize (= 0.9.0) capybara (2.5.0) mime-types (>= 1.16) nokogiri (>= 1.3.3) @@ -61,8 +63,16 @@ GEM coffee-script-source execjs coffee-script-source (1.9.1.1) + columnize (0.9.0) database_cleaner (1.5.0) debug_inspector (0.0.2) + devise (3.5.2) + bcrypt (~> 3.0) + orm_adapter (~> 0.1) + railties (>= 3.2.6, < 5) + responders + thread_safe (~> 0.1) + warden (~> 1.2.3) diff-lcs (1.2.5) em-websocket (0.5.1) eventmachine (>= 0.12.9) @@ -130,11 +140,17 @@ GEM notiffany (0.0.8) nenv (~> 0.1) shellany (~> 0.0) + orm_adapter (0.5.0) pg (0.18.3) - pry (0.10.2) + pry (0.10.3) coderay (~> 1.1.0) method_source (~> 0.8.1) slop (~> 3.4) + pry-byebug (3.2.0) + byebug (~> 5.0) + pry (~> 0.10) + pry-rails (0.3.4) + pry (>= 0.9.10) rack (1.6.4) rack-test (0.6.3) rack (>= 1.0) @@ -167,6 +183,8 @@ GEM rb-inotify (0.9.5) ffi (>= 0.5.0) rdoc (4.2.0) + responders (2.1.0) + railties (>= 4.2.0, < 5) rspec (3.3.0) rspec-core (~> 3.3.0) rspec-expectations (~> 3.3.0) @@ -217,6 +235,8 @@ GEM uglifier (2.7.2) execjs (>= 0.3.0) json (>= 1.8.0) + warden (1.2.3) + rack (>= 1.0) web-console (2.2.1) activemodel (>= 4.0) binding_of_caller (>= 0.7.2) @@ -234,6 +254,7 @@ DEPENDENCIES capybara coffee-rails (~> 4.1.0) database_cleaner + devise (~> 3.5.2) factory_girl_rails faker font-awesome-rails (~> 4.4.0.0) @@ -242,6 +263,8 @@ DEPENDENCIES jbuilder (~> 2.0) jquery-rails pg + pry-byebug + pry-rails rails (= 4.2.3) rspec-rails sass-rails (~> 5.0) diff --git a/Guardfile b/Guardfile index 6a0ea2c..fc9b54e 100644 --- a/Guardfile +++ b/Guardfile @@ -26,11 +26,12 @@ guard 'livereload' do watch(%r{app/views/.+\.(erb|haml|slim)$}) + watch(%r{app/controllers/.+\.(rb)$}) watch(%r{app/helpers/.+\.rb}) - watch(%r{public/.+\.(css|js|html)}) + watch(%r{public/.+\.(css|scss|js|html)}) watch(%r{config/locales/.+\.yml}) # Rails Assets Pipeline - watch(%r{(app|vendor)(/assets/\w+/(.+\.(css|js|html|png|jpg))).*}) { |m| "/assets/#{m[3]}" } + watch(%r{(app|vendor)(/assets/\w+/(.+\.(css|scss|js|html|png|jpg))).*}) { |m| "/assets/#{m[3]}" } end guard :rspec, cmd: "bundle exec rspec" do 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/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..b8915c3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,19 @@ 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 + before_action :authenticate_user! + before_action :devise_user_params, if: :devise_controller? + + private + + def devise_user_params + devise_parameter_sanitizer.for(:sign_up) { + |user| user.permit(:username, :email, :password, :password_confirmation) + } + + devise_parameter_sanitizer.for(:account_update) { + |user| user.permit(:username, :email, :biography, :password, + :password_confirmation, :current_password) + } + end end diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index 91da0c2..35cb7ae 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -1,4 +1,6 @@ class StaticPagesController < ApplicationController + skip_before_action :authenticate_user! + def index end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..d7673b0 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,9 @@ +class UsersController < ApplicationController + def show + if params[:id] + @user = User.find(params[:id]) + else + @user = current_user + end + end +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..f5e3336 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,7 @@ +class User < ActiveRecord::Base + # Include default devise modules. Others available are: + # :confirmable, :lockable, :timeoutable and :omniauthable + devise :database_authenticatable, :registerable, + :recoverable, :rememberable, :trackable, :validatable + validates :username, presence: true +end diff --git a/app/views/devise/confirmations/new.html.erb b/app/views/devise/confirmations/new.html.erb new file mode 100644 index 0000000..826672f --- /dev/null +++ b/app/views/devise/confirmations/new.html.erb @@ -0,0 +1,16 @@ +
Welcome <%= @email %>!
+ +You can confirm your account email through the link below:
+ +<%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %>
diff --git a/app/views/devise/mailer/reset_password_instructions.html.erb b/app/views/devise/mailer/reset_password_instructions.html.erb new file mode 100644 index 0000000..f667dc1 --- /dev/null +++ b/app/views/devise/mailer/reset_password_instructions.html.erb @@ -0,0 +1,8 @@ +Hello <%= @resource.email %>!
+ +Someone has requested a link to change your password. You can do this through the link below.
+ +<%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %>
+ +If you didn't request this, please ignore this email.
+Your password won't change until you access the link above and create a new one.
diff --git a/app/views/devise/mailer/unlock_instructions.html.erb b/app/views/devise/mailer/unlock_instructions.html.erb new file mode 100644 index 0000000..41e148b --- /dev/null +++ b/app/views/devise/mailer/unlock_instructions.html.erb @@ -0,0 +1,7 @@ +Hello <%= @resource.email %>!
+ +Your account has been locked due to an excessive number of unsuccessful sign in attempts.
+ +Click the link below to unlock your account:
+ +<%= link_to 'Unlock my account', unlock_url(@resource, unlock_token: @token) %>
diff --git a/app/views/devise/passwords/edit.html.erb b/app/views/devise/passwords/edit.html.erb new file mode 100644 index 0000000..6a796b0 --- /dev/null +++ b/app/views/devise/passwords/edit.html.erb @@ -0,0 +1,25 @@ +<%= notice %>
+<%= alert %>
+ + <%= yield %> +