Devise registration and routes

jamesperet 10 lat temu
rodzic
commit
7994b07960

+ 1 - 0
Gemfile

@@ -31,6 +31,7 @@ end
31 31
 
32 32
 gem "therubyracer"
33 33
 gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
34
+gem "twitter-bootstrap-rails"
34 35
 gem 'flatstrap-rails'
35 36
 gem 'bootstrap-timepicker-rails'
36 37
 gem 'simple_form'

+ 1 - 0
Gemfile.lock

@@ -161,4 +161,5 @@ DEPENDENCIES
161 161
   summernote-rails
162 162
   therubyracer
163 163
   turbolinks
164
+  twitter-bootstrap-rails
164 165
   uglifier (>= 1.3.0)

+ 8 - 0
app/controllers/application_controller.rb

@@ -2,4 +2,12 @@ class ApplicationController < ActionController::Base
2 2
   # Prevent CSRF attacks by raising an exception.
3 3
   # For APIs, you may want to use :null_session instead.
4 4
   protect_from_forgery with: :exception
5
+  
6
+  before_filter :configure_permitted_parameters, if: :devise_controller?
7
+  
8
+  def configure_permitted_parameters
9
+    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :email, :current_password) }
10
+    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :email, :password) }
11
+  end
12
+  
5 13
 end

+ 2 - 0
app/controllers/start_controller.rb

@@ -1,4 +1,6 @@
1 1
 class StartController < ApplicationController
2
+  
2 3
   def index
3 4
   end
5
+
4 6
 end

+ 9 - 0
app/controllers/users/confirmations_controller.rb

@@ -0,0 +1,9 @@
1
+class users::confirmationsController < Devise::confirmationsController
2
+  # def new
3
+  #   super
4
+  # end
5
+
6
+  # def create
7
+  #   super
8
+  # end
9
+end

+ 9 - 0
app/controllers/users/passwords_controller.rb

@@ -0,0 +1,9 @@
1
+class users::passwordsController < Devise::passwordsController
2
+  # def new
3
+  #   super
4
+  # end
5
+
6
+  # def create
7
+  #   super
8
+  # end
9
+end

+ 43 - 0
app/controllers/users/registrations_controller.rb

@@ -0,0 +1,43 @@
1
+class Users::RegistrationsController < Devise::RegistrationsController
2
+  
3
+  # layout 'auth'
4
+  
5
+  def update
6
+     @user = User.find(current_user.id)
7
+
8
+     successfully_updated = if needs_password?(@user, params)
9
+       @user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
10
+     else
11
+       # remove the virtual current_password attribute
12
+       # update_without_password doesn't know how to ignore it
13
+       params[:user].delete(:current_password)
14
+       params[:user].delete(:password)
15
+       params[:user].delete(:password_confirmation)
16
+       @user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update))
17
+     end
18
+
19
+     if successfully_updated
20
+       set_flash_message :notice, :updated
21
+       # Sign in the user bypassing validation in case his password changed
22
+       sign_in @user, :bypass => true
23
+       redirect_to after_update_path_for(@user)
24
+     else
25
+       render "edit"
26
+     end
27
+   end
28
+
29
+  def after_sign_up_path_for(resource)
30
+    root_path
31
+  end
32
+  
33
+  private
34
+
35
+  # check if we need password to update user data
36
+  # ie if password or email was changed
37
+  # extend this as needed
38
+  def needs_password?(user, params)
39
+    user.email != params[:user][:email] ||
40
+      params[:user][:password].present?
41
+  end
42
+  
43
+end

+ 12 - 0
app/controllers/users/sessions_controller.rb

@@ -0,0 +1,12 @@
1
+class users::sessionsController < Devise::sessionsController
2
+  
3
+  layout 'auth'
4
+  
5
+  # def new
6
+  #   super
7
+  # end
8
+
9
+  # def create
10
+  #   super
11
+  # end
12
+end

+ 9 - 0
app/controllers/users/unlocks_controller.rb

@@ -0,0 +1,9 @@
1
+class users::unlocksController < Devise::unlocksController
2
+  # def new
3
+  #   super
4
+  # end
5
+
6
+  # def create
7
+  #   super
8
+  # end
9
+end

+ 8 - 0
app/models/user.rb

@@ -3,4 +3,12 @@ class User < ActiveRecord::Base
3 3
   # :confirmable, :lockable, :timeoutable and :omniauthable
4 4
   devise :database_authenticatable, :registerable,
5 5
          :recoverable, :rememberable, :trackable, :validatable
6
+         
7
+  validates :password, presence: true, length: {minimum: 5, maximum: 120}, on: :create
8
+  validates :password, length: {minimum: 5, maximum: 120}, on: :update, allow_blank: true
9
+             
10
+  def full_name
11
+    name = self.first_name.to_s + ' ' + self.last_name.to_s
12
+    return name
13
+  end
6 14
 end

+ 4 - 2
app/views/devise/registrations/edit.html.erb

@@ -4,7 +4,9 @@
4 4
   <%= f.error_notification %>
5 5
 
6 6
   <div class="form-inputs">
7
-    <%= f.input :email, required: true, autofocus: true %>
7
+    <%= f.input :first_name, required: true, autofocus: true %>
8
+    <%= f.input :last_name, required: true %>
9
+    <%= f.input :email, required: true %>
8 10
 
9 11
     <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
10 12
       <p>Currently waiting confirmation for: <%= resource.unconfirmed_email %></p>
@@ -12,7 +14,7 @@
12 14
 
13 15
     <%= f.input :password, autocomplete: "off", hint: "leave it blank if you don't want to change it", required: false %>
14 16
     <%= f.input :password_confirmation, required: false %>
15
-    <%= f.input :current_password, hint: "we need your current password to confirm your changes", required: true %>
17
+    <%= f.input :current_password, hint: "we need your current password to confirm your changes", required: false %>
16 18
   </div>
17 19
 
18 20
   <div class="form-actions">

+ 3 - 1
app/views/devise/registrations/new.html.erb

@@ -4,7 +4,9 @@
4 4
   <%= f.error_notification %>
5 5
 
6 6
   <div class="form-inputs">
7
-    <%= f.input :email, required: true, autofocus: true %>
7
+    <%= f.input :first_name, required: true, autofocus: true %>
8
+    <%= f.input :last_name, required: true %>
9
+    <%= f.input :email, required: true %>
8 10
     <%= f.input :password, required: true %>
9 11
     <%= f.input :password_confirmation, required: true %>
10 12
   </div>

+ 25 - 0
app/views/layouts/_navigation_links.html.erb

@@ -0,0 +1,25 @@
1
+<div class="container nav-collapse">
2
+  <ul class="nav">
3
+    <li><%= link_to "Link1", "#"  %></li>
4
+    <li><%= link_to "Link2", "#"  %></li>
5
+    <li><%= link_to "Link3", "#"  %></li>
6
+  </ul>
7
+  
8
+<ul class="nav pull-right">
9
+	<% if user_signed_in? %>
10
+		<li class="dropdown">
11
+			<a href="#" class="dropdown-toggle" data-toggle="dropdown">
12
+				<%= current_user.full_name %> <b class="caret"></b>
13
+			</a>
14
+		    	<ul class="dropdown-menu">
15
+	  			<li><%= link_to 'Account', edit_user_registration_path %></li>
16
+	  			<li><%= link_to "Logout", destroy_user_session_path, method: :delete %></li>
17
+			</ul>
18
+		</li>
19
+	<% else %>
20
+		<li><%= link_to "Login", new_user_session_path  %></li>
21
+		<li><%= link_to "Sign Up", new_user_registration_path  %></li>
22
+	<% end %>
23
+</ul>
24
+  
25
+</div><!--/.nav-collapse -->

+ 2 - 7
app/views/layouts/application.html.erb

@@ -48,13 +48,8 @@
48 48
           </a>
49 49
           <%= render 'layouts/logo' %>
50 50
 
51
-          <div class="container nav-collapse">
52
-            <ul class="nav">
53
-              <li><%= link_to "Link1", "/path1"  %></li>
54
-              <li><%= link_to "Link2", "/path2"  %></li>
55
-              <li><%= link_to "Link3", "/path3"  %></li>
56
-            </ul>
57
-          </div><!--/.nav-collapse -->
51
+          <%= render 'layouts/navigation_links' %>
52
+
58 53
         </div>
59 54
       </div>
60 55
     </div>

+ 3 - 3
config/routes.rb

@@ -7,8 +7,8 @@ RailsWebsiteTemplate::Application.routes.draw do
7 7
     post 'login' => 'devise/sessions#create', :as => :user_session
8 8
     delete 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session
9 9
     get   'signup' => 'devise/registrations#new',    :as => :new_user_registration
10
-    post  'signup' => 'users/registrations#create', :as => :user_registration
11
-    put  'signup' => 'devise/registrations#update', :as => :user_registration_update
10
+    post  'signup' => 'devise/registrations#create', :as => :user_registration
11
+    put  'signup' => 'users/registrations#update', :as => :user_registration_update
12 12
     scope '/account' do
13 13
         # password reset
14 14
         get   '/reset-password'        => 'devise/passwords#new',    as: 'new_user_password'
@@ -22,7 +22,7 @@ RailsWebsiteTemplate::Application.routes.draw do
22 22
         # settings & cancellation
23 23
         get '/cancel'   => 'devise/registrations#cancel', as: 'cancel_user_registration'
24 24
         get '/settings' => 'devise/registrations#edit',   as: 'edit_user_registration'
25
-        put '/settings' => 'devise/registrations#update'
25
+        put '/settings' => 'users/registrations#update', as: 'update_user_registration'
26 26
         # account deletion
27 27
         delete '' => 'devise/registrations#destroy'
28 28
     end