How to redirect user to sign in page if not logged in


I am trying to redirect the user to sign-in page if the user tries to access MyAccountController. Now the issue is that the sign-in route is defined in router.js and I am not able to figure-out a way to access vue routes in rails controller.

class MyAccountController < ApplicationController
  before_action :authenticate_user!
  before_action :require_user

  private

  def require_user
    head(401) unless user_signed_in?
  end

  def authenticate_user
   if user_signed_in?
      super
    else
      redirect_to 'sign-in'
    end
  end   
end

router.js

const SessionsVue = () => import('views/sessions/new.vue')

const routes = [
 { 'path': '/sign-in', component: SessionsVue, meta: { requiresAuth: true } }
]

Source: Vue – Stack Overflow

November 10, 2021
Category : News
Tags: router | routes | ruby-on-rails | Vue.js

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.