**Root page of my app is post's index page**

```
Rails.application.routes.draw do
  root 'posts#index'
  resources :users
  resources :posts 
  resource :sessions, only: [:new, :create, :destroy]
  resources :passwords, only: [:new, :create, :edit, :update]
end

```

My uses cookies to store user sessions. At the time of login, if the user checks the remember me checkbox, its session is remembered and he is able to access the app after closing the browser without needing to log in again.

Step I follow: 
1. Run rails s on the terminal.

2. Type localhost:3000 on the browsers address bar
3. The app opens up,I land on **posts#index** page as it is root path my app follows.
4. I click on the login link, I fill email, password & **check remember me checkbox**
5. I land on user's profile page i.e., users#show page

[![enter image description here](https://i.stack.imgur.com/iqT5Z.png)](https://i.stack.imgur.com/iqT5Z.png)

Terminal till this point

4. Now I close the browsers window. **(I do not log out.)(cookies should rememeber my details)**
5. I again open the browser. type localhost:3000 on browser's addreess bar.

[![enter image description here](https://i.stack.imgur.com/Ehn0s.png)](https://i.stack.imgur.com/Ehn0s.png)

I land **'posts#index'** page. But I want to not on the user's profile i.e, users#show page as my details are stored in cookies

But I want to land on the user's profile page.

On the application controller, if do...

```
 before_action :set_cache_buster,:redirect_if_logged_in

  def redirect_if_logged_in
    redirect_to user_path(current_user) if logged_in?
   end

```

And login if says **redirect too many times**

Application Controller:

```
# frozen_string_literal: true

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception

  helper_method :current_user,:logged_in?, :logged_in_user,:current_user

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

  def current_user
     @current_user ||= User.where("auth_token =?", cookies[:auth_token]).first if cookies[:auth_token]
  end

   def logged_in?
     !current_user.nil?
   end

   def logged_in_user
     unless logged_in?
       flash[:danger] = "Please log in."
       redirect_to new_sessions_path
     end
   end

   def current_user?(user)
     user == current_user
    end

    private
    def set_cache_buster
      response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
      response.headers["Pragma"] = "no-cache"
      response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
    end

end

```

User Contoller 

```
# frozen_string_literal: true
require 'resolv-replace'

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:show, :edit, :update]
  before_action :correct_user, only: [:show, :edit, :update]

  def index
    @users = User.all
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(set_params)
    if @user.save
      UserNotifierMailer.send_signup_email(@user).deliver
      flash[:success] ="Success"
      redirect_to new_sessions_path
    else
      render 'new'
    end
  end

  def show
    @user = User.find(params[:id])
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update(update_params)
      redirect_to @user
    else
      render 'edit'
   end
  end

  private

  def set_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation)
  end

  def update_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation)
  end

  def correct_user
    @user = User.find(params[:id])
    rescue ActiveRecord::RecordNotFound
    redirect_to(@current_user) unless current_user?(@user)
  end

end

```

After I type localhost:3000 redirecting to 'post#index' is fine. but I add a conditional redirect. If cookies are not empty redirect to some other path.

I hope i am able to explain the problem well at least now.