关闭浏览器后自动退出(红宝石)

时间:2018-11-22 13:43:19

标签: ruby-on-rails session

我在处理会话方面遇到问题。我要尝试的是每次关闭浏览器时都需要注销 用户帐户,除非选中remeber me复选框 。但是,当我关闭并重新打开浏览器时,用户保持登录状态,而无需选中remember me复选框。有人帮忙吗?

查看

<%= form_tag sessions_path do %>
        <div class="form-group">
          <label for="email" class="text-info">Email:</label><br>
          <input type="text" name="email" id="email" class="form-control">
        </div>
        <div class="form-group">
          <label for="password" class="text-info">Password:</label><br>
          <input type="password" name="password" id="password" class="form-control">
        </div>
        <div class="field">
          <%= label_tag :remember_me %>
          <%= check_box_tag :remember_me, 1, params[:remember_me] %>
        </div>
        <div class="form-group">
          <input type="submit" name="Sign_in" class="btn btn-info btn-md" value="submit">
        </div>
<% end %>

SessionsController

  def create
    user = User.find_by(email: params[:email])
    if user && user.authenticate(params[:password])

      if params[:remember_me]
        cookies.permanent[:auth_token] = user.auth_token
      else
        cookies[:auth_token] = user.auth_token
      end

      log_in(user)
      redirect_to user_path(user.id)
    else    
      redirect_to new_session_path
    end
  end


  def destroy
    cookies.delete(:auth_token)
    log_out
    redirect_to '/'
  end

Sessionshelper

  def log_in(user)
    session[:user_id] = user.id
  end

  def current_user
    @current_user ||= User.find_by_auth_token( cookies[:auth_token]) if cookies[:auth_token]
  end

  def log_out
    session.delete(:user_id)
    @current_user = nil
  end

0 个答案:

没有答案