开发者

How to log in a user right after signing up?

开发者 https://www.devze.com 2023-02-20 09:46 出处:网络
I know I should put the code in the create action of the users controller, but I\'m not sure what code I should put. I also assume it should call the create action in my sessions controller, but again

I know I should put the code in the create action of the users controller, but I'm not sure what code I should put. I also assume it should call the create action in my sessions controller, but again I'm not sure how...

By the way I tried render :template => 'sessions/create' in the creat开发者_运维知识库e action of the users controller, but I get this error when signing up:

Template is missing

Missing template sessions/create with {:locale=>[:en, :en], :formats=>[:html], :handlers=>[:rjs, :rhtml, :erb, :rxml, :builder]} in view paths "/rubyprograms/dreamstill/app/views", "/rubyprograms/dreamstill/vendor/plugins/facebox_render/app/views"

This is all in my application controller:

protected 
  # Returns the currently logged in user or nil if there isn't one
  def current_user
    return unless session[:user_id]
    @current_user ||= User.find_by_id(session[:user_id]) 
  end


  # Make current_user available in templates as a helper
  helper_method :current_user

  # Filter method to enforce a login requirement
  # Apply as a before_filter on any controller you want to protect
  def authenticate
    logged_in? ? true : access_denied
  end

  # Predicate method to test for a logged in user    
  def logged_in?
    current_user.is_a? User
  end

  # Make logged_in? available in templates as a helper
  helper_method :logged_in?

  def access_denied
    respond_to do |format|
      format.html do
        flash[:alert] = "You must log in to peform this action."
        redirect_to root_path
      end

      format.js do
        render_to_facebox(:partial => 'sessions/login_box')
      end
    end
     false
  end


Somewhere in your controllers you have something that looks like this:

user = User.new
# set attributes
user.save
render :template => 'sessions/create' # Probably based on your question

All you need to do is update the session to:

user = User.new
# set attributes
if(user.save)
   session[:user_id] = user.id
   # Send them somewhere useful
else
   # Handle the error
end

They're signed in once session[:user_id] is set.


Technically?

In your controller, after you create your user, this code:

@current_user = user

should get you going (looks like you're using restful_authentication).

Now, whether it's a good idea to log in a user automatically without verifying their email address / whatever else is up for debate.


You seem that you just begin with Rails right ? I would highly recommend that you use a gem like Devise to handle your user registrations.

However, if you insist on doing it manually, you would just need to create a session variable that verifies whether a user is logged in or not. Then, you can add a helper like current_user, to get the user if user session shows he/she is logged in.

I see that you have a sessions controller there. Are you trying to use restful_authentication ? If so, once more i highly recommend switching to Devise :)

OLD CODE USING RESTFUL AUTHENTICATION - SESSIONS CONTROLLER

# This controller handles the login/logout function of the site.  
class SessionsController < ApplicationController
  # Be sure to include AuthenticationSystem in Application Controller instead
  include AuthenticatedSystem

  # render new.erb.html
  def new
  end

  def create
    logout_keeping_session!
    user = User.authenticate(params[:login], params[:password])
    if user
      # Protects against session fixation attacks, causes request forgery
      # protection if user resubmits an earlier form using back
      # button. Uncomment if you understand the tradeoffs.
      # reset_session
      self.current_user = user
      new_cookie_flag = (params[:remember_me] == "1")
      handle_remember_cookie! new_cookie_flag
      flash[:notice] = "Logged in successfully"
      redirect_to :controller=>'Town'
    else
      note_failed_signin
      @login       = params[:login]
      @remember_me = params[:remember_me]
      render :action => 'new'
    end
  end

  def destroy
    logout_killing_session!
    flash[:notice] = "You have been logged out."
    redirect_back_or_default('/')
  end

protected
  # Track failed login attempts
  def note_failed_signin
    flash[:error] = "Couldn't log you in as '#{params[:login]}'"
    logger.warn "Failed login for '#{params[:login]}' from #{request.remote_ip} at #{Time.now.utc}"
  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消