简体   繁体   English

禁止经过身份验证的用户访问nuxt js中的登录页面

[英]Forbid authenticated users from visiting login page in nuxt js

I have the following code snipped from my middleware/auth.js that redirects users to login page if the users are unauthenticated.我从我的middleware/auth.js了以下代码,如果用户未经身份验证,这些代码会将用户重定向到登录页面。

 export default function ({ store, redirect }) { if (!store.state.admin.isAdmin) { redirect('/auth/login') } }

Now I want to forbid authenticated users visiting login page and redirect to ('/manage/dashboard') when they hit the login url.现在我想禁止经过身份验证的用户访问登录页面并在他们点击登录 url 时重定向到 ('/manage/dashboard')。

I have used the following approach in my login view:我在登录视图中使用了以下方法:

 mounted () { if (localStorage.getItem('token') && localStorage.getItem('userId')) { this.$router.replace({ path: '/admin/dashboard' }) } }

This works but the execution is too slow and user can see login page for few seconds.这有效,但执行速度太慢,用户可以看到登录页面几秒钟。

What is the efficient way to achieve the redirect ?实现重定向的有效方法是什么?

I wouldn't access localStorage from the login view, I would prefer to set it in your store and then access the store.我不会从登录视图访问 localStorage,我更愿意将它设置在您的商店中,然后访问商店。 I have not run this code but I think you want to do something like this.我还没有运行这段代码,但我认为你想做这样的事情。

You can create another check-auth.js middleware and add that to your login view.您可以创建另一个 check-auth.js 中间件并将其添加到您的登录视图中。

check-auth.js检查 auth.js

export default function (context) {
    context.store.dispatch('initAuth', context.req);
}

store/index.js商店/index.js

state:{
     authenticated:false;
}
mutations: {
    setAuthentication(state, auth) {
            state.authenticated = auth;
        },
}
actions: {
    initAuth(vuexContext, req) {
        if (localStorage.getItem('token') && localStorage.getItem('userId')){
            vuexContext.commit('setAuthentication', true);
        }else{
            vuexContext.commit('setAuthentication', false);
        }
    }
},
getters: {
        isAuthenticated(state) {
            return state.authenticated;
        },
    },

Then check if the user is authenticated auth.js然后检查用户是否通过身份验证 auth.js

if (!context.store.getters.isAuthenticated) {
    context.redirect('/auth/login');
}

add middleware to your view将中间件添加到您的视图中

export default {
    middleware: ['check-auth', 'auth'],
    components: {},
    methods: {},
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vue.js - 将未经过身份验证的用户重定向到特定页面,与登录不同 - Vue.js - redirecting user not authenticated to specific page, different from login 仅在MEAN js中对经过身份验证的用户的页面浏览权限 - Page view permission to authenticated users only in MEAN js 检测访问当前页面的用户(标签)数量 - Detecting number of users (tabs) visiting the current page Vue 路由器重定向到 404 而不是登录页面(Nuxt.js) - Vue router redirects to 404 instead of login page (Nuxt.js) 页面上仅允许经过身份验证的用户 - Allow only authenticated users on the page 如何在无需登录的情况下从访问我的 web 页面的每个用户那里获取唯一 ID? - How to get an unique ID from every user visiting my web page without the need of login in? 如何禁止从JS访问DOM - how forbid access to DOM from JS 从 Nuxt.js 中的商店加载页面标题 - Load page title from store in Nuxt.js 用户在会话仍处于身份验证状态时单击后退按钮,并重定向到node.js中的登录页面 - User clicks on back button while session is still authenticated and gets redirected to login page in node.js React - 未通过身份验证时重定向到登录页面 - React - redirect to login page when not authenticated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM