8

में लॉग इन पासवर्ड भूल गए। एक उपयोगकर्ता फेसबुक के साथ लॉग इन करता है, उनके लिए एक नकली पासवर्ड उत्पन्न होता है 2. उपयोगकर्ता फिर अपना ईमेल/नाम/पासवर्ड बदलना चाहते हैं, या सिर्फ गैर फेसबुक लॉगिनवसीयत अगर वहाँ बुला</p> <p>मामले मैं में चल रहा हूँ लॉग आउट करने के लिए मजबूर कर मेरे उपयोगकर्ता बिना प्रक्रिया 'पासवर्ड भूल गया' का एक तरह से किया जाता है मैं सोच रहा हूँ के लिए उपयोगकर्ता

के बाद से इन क्षेत्रों को बदलने के लिए पासवर्ड की आवश्यकता होती है, जैसा कि यह होना चाहिए, उपयोगकर्ता उन्हें संशोधित करने में असमर्थ है

मैंने सोचा था कि सिर्फ फोर्क नहीं पासवर्ड सेट करने के लिए, लेकिन यह सुरक्षा के अनुसार समझ में नहीं आता है, इसलिए मैं सिर्फ फ़ील्ड को टेक्स्ट के रूप में प्रदर्शित करता हूं और उपयोगकर्ता को पासवर्ड सेट करने के लिए 'भूल गए पासवर्ड' प्रक्रिया का पालन करने के लिए सूचित करता हूं और फिर वे

फ़ील्ड बदल सकते हैं

मुद्दा तो यह है कि मैं बस उपयोगकर्ता प्रोफ़ाइल से यह करने के लिए लिंक नहीं कर सकते क्योंकि वसीयत उपयोगकर्ता कि वे इस जबकि पहले से लॉग इन नहीं कर सकते बता देंगे है।

तो वहाँ भूल गया पासवर्ड अधिभावी की एक तरह से है या/उपयोगकर्ता/पासवर्ड/संपादन विधि ताकि उपयोगकर्ता में लॉग इन भी इस क्रिया को कर सके?

उत्तर

2

मेरा पूरा समाधान यहां है, क्योंकि मैंने यह भी सीखा है कि उपयोगकर्ता को ईमेल में लिंक पर क्लिक करने के बाद लॉग आउट करना होगा, वास्तव में पासवर्ड को संपादित करने के साथ-साथ इसे सहेजने के लिए कुछ अतिरिक्त उपयोगकर्ता नियंत्रक क्रियाएं जोड़ना था। यह एक आदर्श समाधान नहीं है और ठंडा शायद बेहतर तरीके से किया जा सकता है लेकिन यह मेरे लिए काम करता है।

उपयोगकर्ता नियंत्रक; रीसेट करने के लिए अतिरिक्त तरीके

before_filter :authenticate_user!, :except => [:do_reset_password, :reset_password_edit] 

    def reset_password 
     id = params[:id] 
     if id.nil? 
      id = current_user.id 
     end  
     if (!user_signed_in? || current_user.id.to_s != id.to_s) 
     flash[:alert] = "You don't have that right." 
      redirect_to '/home' 
      return 
     end 

     @user = User.find(id) 
     @user.send_reset_password_instructions 

     respond_to do |format| 
      format.html { redirect_to '/users/edit', notice: 'You will receive an email with instructions about how to reset your password in a few minutes.' } 
     end 
    end 


    def do_reset_password 
     id = params[:id] 
     if id.nil? && !current_user.nil? 
      id = current_user.id 
     end 

     if id.nil? 
      @user = User.where(:reset_password_token => params[:user][:reset_password_token]).first 
     else 
      @user = User.find(id) 
     end 
     if @user.nil? || @user.reset_password_token.to_s != params[:user][:reset_password_token] 
      flash[:alert] = "Url to reset was incorrect, please resend reset email." 
      redirect_to '/home' 
      return 
     end 

     # there may be a better way of doing this, devise should be able to give us these messages 
     if params[:user][:password] != params[:user][:password_confirmation] 
      flash[:alert] = "Passwords must match." 
       redirect_to :back 
       return 
     end 
     if @user.reset_password!(params[:user][:password],params[:user][:password_confirmation]) 
      @user.hasSetPassword = true 
      @user.save 
      respond_to do |format| 
       format.html { redirect_to '/home', notice: 'Your password has been changed.' } 
      end 
     else 
      flash[:alert] = "Invalid password, must be at least 6 charactors." 
       redirect_to :back 
     end 
    end 

    def reset_password_edit 
     @user = User.where(:reset_password_token => params[:reset_password_token]).first 
     if @user.nil? || [email protected]_password_period_valid? 
      flash[:alert] = "Password reset period expired, please resend reset email" 
      redirect_to "/home" 
      return 
     end 
    end 

विचार/devise/registrations/संपादित करें; दृश्य न जाने के लिए खेतों उपयोगकर्ता संपादित करें कि पासवर्ड के

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %> 
     <%= devise_error_messages! %> 

     <% if !resource.hasSetPassword %>           
      <%= f.label :name %><br /> 
      <p style="line-height:24px;"><b><%= @user.name %></b></p>    
      <div><%= f.label :email %><br /> 
       <p style="line-height:24px;"><b><%= @user.email %> </b></p> 
       <p style="position:relative; left:150px; width:420px;"> 
       <i>you cannot change any settings because you have not set a password <br />yet, you can do so by following the </i> 
       <%= link_to "Forgot your password", "https://stackoverflow.com/users/reset_password" %> <i> procedure</i> 
       </p> 
      </div> 
     <% else %>      
      <p><%= f.label :name %><br /> 
      <%= f.text_field :name %></p>   
      <div><%= f.label :email %><br /> 
      <%= f.email_field :email %></div> 

      <div><%= f.label :password %> <br /> 
      <%= f.password_field :password %><i>(leave blank if you don't want to change it)</i></div> 

      <div><%= f.label :password_confirmation %><br /> 
      <%= f.password_field :password_confirmation %></div> 

      <div><%= f.label :current_password %> <br /> 
      <%= f.password_field :current_password %> 
      <i>(we need your current password to confirm your changes)</i> 
      </div> 
     <div><%= f.submit "Update" %></div> 
     <% end %> 
    <% end %> 

विचारों/वसीयत/मेलर/reset_password_instructions आवश्यकता बदल दिया; हमारे नए मामले में सही URL को इंगित करने के लिए इसे बदलना पड़ा

<p>Hello <%= @resource.email %>!</p> 

    <p>Someone has requested a link to change your password, and you can do this through the link below.</p> 

    <% if [email protected] %> 
     <p><%= link_to 'Change my password', 'http://streetsbehind.me/users/reset_password_edit?reset_password_token='[email protected]_password_token %></p> 
    <!-- todo: there's probably a better way of doing this than just hardcoding streetsbehind.me --> 
    <% else %> 
     <p><%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token) %></p> 
    <% end %> 
    <p>If you didn't request this, please ignore this email.</p> 
    <p>Your password won't change until you access the link above and create a new one.</p> 

विचारों/उपयोगकर्ताओं/reset_password_edit.erb

<%= form_for(@user, :url => url_for(:action => :do_reset_password) , :html => { :method => :post }) do |f| %> 

    <%= f.hidden_field :reset_password_token %> 

    <div><%= f.label :password, "New password" %><br /> 
    <%= f.password_field :password %></div> 

    <div><%= f.label :password_confirmation, "Confirm new password" %><br /> 
    <%= f.password_field :password_confirmation %></div> 

    <div><%= f.submit "Change my password" %></div> 
<% end %> 

config/routes.rb

get "users/reset_password" 
get "users/reset_password_edit" 

resource :users do 
    post 'do_reset_password' 
end 
4

पासवर्ड रीसेट टोकन जेनरेट करने और ईमेल भेजने के लिए आप @user.send_reset_password_instructions का उपयोग कर सकते हैं। यदि आप सीधे मेलर को कॉल करते हैं, तो रीसेट प्रमाणीकृत करने के लिए पासवर्ड रीसेट टोकन जेनरेट नहीं किया जाएगा।

+0

की तरह हैं। – fiestacasey

15

कारण यह है कि आप पासवर्ड रीसेट नहीं कर सकते हैं क्योंकि devise उपयोगकर्ता को वर्तमान सत्र के साथ प्रमाणित करने का प्रयास करता है और जब आप सफल होते हैं तो आपको स्वचालित रूप से उस मार्ग पर रीडायरेक्ट कर दिया जाता है जिसे इसे जाना है। आपको इस चरण को छोड़ने के लिए पासवर्ड नियंत्रक के संपादन और अद्यतन कार्रवाई को ओवरराइड करना है।

यहां कोड है। अपने पासवर्ड नियंत्रक में निम्नलिखित कोड जोड़ें (आप अपने लिए नियंत्रक उत्पन्न करने के लिए तैयार हो सकते हैं, या आप केवल निम्न नियंत्रक बना सकते हैं)। अद्यतन के लिए ओवरराइड आवश्यक है क्योंकि अन्यथा लॉग इन उपयोगकर्ता आपके रीसेट पासवर्ड के बाद स्वचालित रूप से साइनआउट हो जाएगा।(या यदि आप इसे पसंद है कि आप #update ओवरराइड से छुटकारा मिल सकता है होना चाहता हूँ)

class PasswordsController < Devise::PasswordsController 
    # here we need to skip the automatic authentication based on current session for the following two actions 
    # edit: shows the reset password form. need to skip, otherwise it will go directly to root 
    # update: updates the password, need to skip otherwise it won't even reset if already logged in 
    skip_before_filter :require_no_authentication, :only => [:edit, :update] 

    # we need to override the update, too. 
    # After a password is reset, all outstanding sessions are gone. 
    # When already logged in, sign_in is a no op, so the session will expire, too. 
    # The solution is to logout and then re-login which will make the session right. 
    def update 
    super 
    if resource.errors.empty? 
     sign_out(resource_name) 
     sign_in(resource_name, resource) 
    end 
    end 
end 

मार्गों एक बहुत बेहतर काम किया है कि निम्नलिखित

# config/routes.rb 
devise_for :users, :controllers => {:passwords => 'passwords'} 
संबंधित मुद्दे

 संबंधित मुद्दे