ruby on rails - How to use Spree's Authentication in form -
i learning rails , creating web app got ecommerce in there form user can fill if logged in, using devise, e-commerce installed spree spree got own login authentication, , there no authenticate_user! in controllers too, removed devise , having tough time finding how use spree's authentication form
here updated form's controller: complaints_controller.rb
module spree class complaintscontroller < spree::storecontroller before_action :require_login before_action :set_complaint, only: [:show, :edit, :update, :destroy] # /complaints # /complaints.json def require_login redirect_to spree_login_path unless current_spree_user end def index @complaints = complaint.all end # /complaints/1 # /complaints/1.json def show end # /complaints/new def new @complaint = complaint.new end # /complaints/1/edit def edit end # post /complaints # post /complaints.json def create @complaint = complaint.new(complaint_params) respond_to |format| if @complaint.save format.html { redirect_to @complaint, notice: 'complaint created.' } format.json { render :show, status: :created, location: @complaint } else format.html { render :new } format.json { render json: @complaint.errors, status: :unprocessable_entity } end end end # patch/put /complaints/1 # patch/put /complaints/1.json def update respond_to |format| if @complaint.update(complaint_params) format.html { redirect_to @complaint, notice: 'complaint updated.' } format.json { render :show, status: :ok, location: @complaint } else format.html { render :edit } format.json { render json: @complaint.errors, status: :unprocessable_entity } end end end # delete /complaints/1 # delete /complaints/1.json def destroy @complaint.destroy respond_to |format| format.html { redirect_to complaints_url, notice: 'complaint destroyed.' } format.json { head :no_content } end end private # use callbacks share common setup or constraints between actions. def set_complaint @complaint = complaint.find(params[:id]) end # never trust parameters scary internet, allow white list through. def complaint_params params.require(:complaint).permit(:id_society, :id_user, :heading, :text, :active, :action, :isdelete, :flat_number) end end end <% end %>
index.html.erb
<% if spree_current_user %> <p id="notice"><%= notice %></p> <h1>listing complaints</h1> <table> <thead> <tr> <th>id society</th> <th>id user</th> <th>heading</th> <th>text</th> <th>active</th> <th>action</th> <th>isdelete</th> <th>flat number</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @complaints.each |complaint| %> <tr> <td><%= complaint.id_society %></td> <td><%= complaint.id_user %></td> <td><%= complaint.heading %></td> <td><%= complaint.text %></td> <td><%= complaint.active %></td> <td><%= complaint.action %></td> <td><%= complaint.isdelete %></td> <td><%= complaint.flat_number %></td> <td><%= link_to 'show', complaint %></td> <td><%= link_to 'edit', edit_complaint_path(complaint) %></td> <td><%= link_to 'destroy', complaint, method: :delete, data: { confirm: 'are sure?' } %></td> </tr> <% end %> </tbody> </table> <br> <%= link_to 'new complaint', new_complaint_path %> <% else %> <h1> please login</h1> <% end %>
this works, verifies user's authentication in view, there way check in controller? if user logged in sent action or else redirected login?
thank you
spree uses devise authentication through extension:
https://github.com/spree/spree_auth_devise
for authenticate actions @ controller(your own controllers) level, need define own authentication filter. can manage this:
before_action :require_login def require_login redirect_to login_url unless current_spree_user end
Comments
Post a Comment