ruby on rails - redirecting logged in users to index if they try to access /login -
so i'm after simple solution can't seem work out. new rails, trying learn , basics down.
when users aren't logged in , try , access various pages, redirected /login. want stop them form being able view /signin page if logged in.
how can achieve this?
update
application_controller
def current_user @current_user ||= user.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token] end
sessions_controller
class sessionscontroller < applicationcontroller layout "empty" def create user = user.find_by_user_name(params[:user_name]) 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 flash[:success] = "you logged in" redirect_to '/' else flash[:error] = "invalid username or password" redirect_to '/login' end end def destroy cookies.delete(:auth_token) flash[:error] = "you have logged out" redirect_to '/login' end private end
add before_action
action /login
view, if have current_user
can this
before_action :check_session, only: [:login] def login # login page action end private def check_session redirect_to root_path unless current_user.blank? end
hope helps!
Comments
Post a Comment