diff --git a/src/Gemfile b/src/Gemfile index 2017436..31f93e8 100644 --- a/src/Gemfile +++ b/src/Gemfile @@ -34,6 +34,11 @@ gem 'jbuilder', '~> 2.5' # Use Bootstrap gem 'bootstrap-sass', '~> 3.3.7' +# Use sorcery for authentication +gem 'sorcery' +# Used for email validation +gem 'validates_email_format_of' + # Use Capistrano for deployment # gem 'capistrano-rails', group: :development diff --git a/src/Gemfile.lock b/src/Gemfile.lock index 319e6f1..ac6903a 100644 --- a/src/Gemfile.lock +++ b/src/Gemfile.lock @@ -41,6 +41,7 @@ GEM arel (7.1.4) autoprefixer-rails (6.7.2) execjs + bcrypt (3.1.11) bootstrap-sass (3.3.7) autoprefixer-rails (>= 5.2.1) sass (>= 3.3.4) @@ -57,6 +58,8 @@ GEM debug_inspector (0.0.2) erubis (2.7.0) execjs (2.7.0) + faraday (0.10.1) + multipart-post (>= 1.2, < 3) ffi (1.9.17) globalid (0.3.7) activesupport (>= 4.1.0) @@ -68,6 +71,7 @@ GEM rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) + jwt (1.5.6) listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -82,9 +86,18 @@ GEM mini_portile2 (2.1.0) minitest (5.10.1) multi_json (1.12.1) + multi_xml (0.6.0) + multipart-post (2.0.0) nio4r (1.2.1) nokogiri (1.7.0.1) mini_portile2 (~> 2.1.0) + oauth (0.5.1) + oauth2 (1.3.0) + faraday (>= 0.8, < 0.11) + jwt (~> 1.0) + multi_json (~> 1.3) + multi_xml (~> 0.5) + rack (>= 1.2, < 3) pg (0.19.0) puma (3.6.2) rack (2.0.1) @@ -128,6 +141,10 @@ GEM sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + sorcery (0.10.2) + bcrypt (~> 3.1) + oauth (~> 0.4, >= 0.4.4) + oauth2 (~> 1.0, >= 0.8.0) spring (2.0.1) activesupport (>= 4.2) spring-watcher-listen (2.0.1) @@ -151,6 +168,8 @@ GEM thread_safe (~> 0.1) uglifier (3.0.4) execjs (>= 0.3.0, < 3) + validates_email_format_of (1.6.3) + i18n web-console (3.4.0) actionview (>= 5.0) activemodel (>= 5.0) @@ -176,12 +195,14 @@ DEPENDENCIES rails-controller-testing rake (~> 12.0.0) sass-rails (~> 5.0) + sorcery spring spring-watcher-listen (~> 2.0.0) sqlite3 turbolinks (~> 5) tzinfo-data uglifier (>= 1.3.0) + validates_email_format_of web-console (>= 3.3.0) BUNDLED WITH diff --git a/src/app/assets/javascripts/dashboard.coffee b/src/app/assets/javascripts/dashboard.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/src/app/assets/javascripts/dashboard.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/src/app/assets/javascripts/sessions.coffee b/src/app/assets/javascripts/sessions.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/src/app/assets/javascripts/sessions.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/src/app/assets/javascripts/users.coffee b/src/app/assets/javascripts/users.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/src/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/src/app/assets/stylesheets/dashboard.scss b/src/app/assets/stylesheets/dashboard.scss new file mode 100644 index 0000000..e8f34fd --- /dev/null +++ b/src/app/assets/stylesheets/dashboard.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Dashboard controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/src/app/assets/stylesheets/sessions.scss b/src/app/assets/stylesheets/sessions.scss new file mode 100644 index 0000000..ccb1ed2 --- /dev/null +++ b/src/app/assets/stylesheets/sessions.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Sessions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/src/app/assets/stylesheets/users.scss b/src/app/assets/stylesheets/users.scss new file mode 100644 index 0000000..c47a13e --- /dev/null +++ b/src/app/assets/stylesheets/users.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the User controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/src/app/controllers/application_controller.rb b/src/app/controllers/application_controller.rb index 1c07694..78bed39 100644 --- a/src/app/controllers/application_controller.rb +++ b/src/app/controllers/application_controller.rb @@ -1,3 +1,10 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception + before_action :require_login + + private + def not_authenticated + flash[:warning] = 'You have to authenticate to access this page.' + redirect_to sign_in_path + end end diff --git a/src/app/controllers/dashboard_controller.rb b/src/app/controllers/dashboard_controller.rb new file mode 100644 index 0000000..391fa2e --- /dev/null +++ b/src/app/controllers/dashboard_controller.rb @@ -0,0 +1,4 @@ +class DashboardController < ApplicationController + def index + end +end diff --git a/src/app/controllers/sessions_controller.rb b/src/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..3c5ead3 --- /dev/null +++ b/src/app/controllers/sessions_controller.rb @@ -0,0 +1,22 @@ +class SessionsController < ApplicationController + skip_before_action :require_login, except: [:destroy] + + def new + end + + def create + if login(params[:email], params[:password]) + flash[:success] = 'Welcome back!' + redirect_back_or_to root_path + else + flash.now[:warning] = 'E-mail and/or password is incorrect.' + render 'new' + end + end + + def destroy + logout + flash[:success] = 'See you!' + redirect_to sign_in_path + end +end \ No newline at end of file diff --git a/src/app/controllers/static_pages_controller.rb b/src/app/controllers/static_pages_controller.rb index cf48a8b..72ad12d 100644 --- a/src/app/controllers/static_pages_controller.rb +++ b/src/app/controllers/static_pages_controller.rb @@ -1,4 +1,6 @@ class StaticPagesController < ApplicationController + skip_before_action :require_login + def home end end diff --git a/src/app/controllers/users_controller.rb b/src/app/controllers/users_controller.rb new file mode 100644 index 0000000..05a7e80 --- /dev/null +++ b/src/app/controllers/users_controller.rb @@ -0,0 +1,24 @@ +class UsersController < ApplicationController + skip_before_action :require_login, only: [:new, :create] + + def new + @user = User.new + end + + def create + @user = User.new(user_params) + if @user.save + login(params[:user][:email], params[:user][:password]) + flash[:success] = 'Welcome!' + redirect_to root_path + else + render 'new' + end + end + + private + + def user_params + params.require(:user).permit(:email, :password, :password_confirmation, :username) + end +end diff --git a/src/app/helpers/dashboard_helper.rb b/src/app/helpers/dashboard_helper.rb new file mode 100644 index 0000000..a94ddfc --- /dev/null +++ b/src/app/helpers/dashboard_helper.rb @@ -0,0 +1,2 @@ +module DashboardHelper +end diff --git a/src/app/helpers/sessions_helper.rb b/src/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..309f8b2 --- /dev/null +++ b/src/app/helpers/sessions_helper.rb @@ -0,0 +1,2 @@ +module SessionsHelper +end diff --git a/src/app/helpers/users_helper.rb b/src/app/helpers/users_helper.rb new file mode 100644 index 0000000..2310a24 --- /dev/null +++ b/src/app/helpers/users_helper.rb @@ -0,0 +1,2 @@ +module UsersHelper +end diff --git a/src/app/models/user.rb b/src/app/models/user.rb new file mode 100644 index 0000000..7201ec5 --- /dev/null +++ b/src/app/models/user.rb @@ -0,0 +1,8 @@ +class User < ApplicationRecord + authenticates_with_sorcery! + + validates :password, length: { minimum: 3 } + validates :password, confirmation: true + validates :email, uniqueness: true + validates :email, uniqueness: true, email_format: { message: 'has invalid format' } +end diff --git a/src/app/views/dashboard/index.html.erb b/src/app/views/dashboard/index.html.erb new file mode 100644 index 0000000..b9790e8 --- /dev/null +++ b/src/app/views/dashboard/index.html.erb @@ -0,0 +1,3 @@ +

Dashboard!

+ +

Restricted area for authorized users only.

\ No newline at end of file diff --git a/src/app/views/layouts/_guest_navigation.html.erb b/src/app/views/layouts/_guest_navigation.html.erb index d558882..ff33d83 100644 --- a/src/app/views/layouts/_guest_navigation.html.erb +++ b/src/app/views/layouts/_guest_navigation.html.erb @@ -1,5 +1,5 @@ \ No newline at end of file diff --git a/src/app/views/layouts/_header.html.erb b/src/app/views/layouts/_header.html.erb index 456b963..2b3885a 100644 --- a/src/app/views/layouts/_header.html.erb +++ b/src/app/views/layouts/_header.html.erb @@ -13,7 +13,7 @@ <% end %>