Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to redirect from Login component if user already logged in #644

Closed
lucianobosco opened this issue Mar 28, 2021 · 1 comment
Closed

Comments

@lucianobosco
Copy link

lucianobosco commented Mar 28, 2021

I'm wondering how to handle redirects for the login page if a user is already logged in.
My login page is using the path / so if the domain is reached with nothing else than the domain name the user lands on the login page.
Once logged in, the user is redirected to dashboard view. If the user logs out then redirect to the login page accurs.
So far so good, but what about if the user tries to reach the login page while he's already logged in?

this is my router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import Meta from 'vue-meta'
import NotFound from '@/components/NotFound'

import globals from '@/globals'

// Layouts
import Layout1 from '@/layout/Layout1'

Vue.use(Router)
Vue.use(Meta)

const router = new Router({
    base: '/',
    mode: 'history',
    routes: [
        {
            name: 'login',
            path: '/',
            component: () => import('@/components/auth/LoginPage'),
            meta: {
                title: 'Acceso al Sistema',
                auth: false
            }
        },
        {
            path: '/',
            component: Layout1,
            children: [
                {
                    name: 'dashboard',
                    path: 'dashboard',
                    component: () => import('@/components/Home'),
                    meta: {
                        title: 'Inicio',
                        auth: true
                    }
                },
                {
                    name: 'sample',
                    path: 'sample',
                    component: () => import('@/components/Page2'),
                    meta: {
                        title: 'Sample',
                        auth: true
                    }
                }
            ]
        },
        {
            // 404 Not Found page
            path: '*',
            component: NotFound
        }
    ]
})

router.afterEach(() => {
    // On small screens collapse sidenav
    if (window.layoutHelpers && window.layoutHelpers.isSmallScreen() && !window.layoutHelpers.isCollapsed()) {
        setTimeout(() => window.layoutHelpers.setCollapsed(true, true), 10)
    }

    // Scroll to top of the page
    globals().scrollTop(0, 0)
})

router.beforeEach((to, from, next) => {
    // Set loading state
    document.body.classList.add('app-loading')

    // Add tiny timeout to finish page transition
    setTimeout(() => next(), 10)
})

export default router

This is my main.js

Vue.use(auth, {
    plugins: {
        http: Vue.axios, // Axios
        router: Vue.router
    },
    drivers: {
        auth: {
            request: function (req, token) {
                this.drivers.http.setHeaders.call(this, req, {
                    Authorization: 'Bearer ' + token
                })
                // Set auth_refresh_token on every request
                this.drivers.http.setHeaders.call(this, req, {
                    Refresh: this.token(this.options.refreshTokenKey)
                })
            },

            response: function (res) {
                if (res.status === 401) this.logout()

                var headers = this.drivers.http.getHeaders.call(this, res)

                // Set auth_refresh_token if available
                var refresh_token = headers.Refresh || headers.refresh
                if (refresh_token) this.token(this.options.refreshTokenKey, refresh_token)

                // Set auth_access_token if available
                var token = headers.Authorization || headers.authorization
                if (token) {
                    token = token.split(/Bearer:?\s?/i)
                    return token[token.length > 1 ? 1 : 0].trim()
                }
            }
        },
        http: driverHttpAxios,
        router: driverRouterVueRouter,
        oauth2: {
            google: driverOAuth2Google,
            facebook: driverOAuth2Facebook
        }
    },
    options: {
        tokenDefaultKey: 'auth_access_token',
        refreshTokenKey: 'auth_refresh_token',
        rememberkey: 'auth_remember',
        tokenImpersonateKey: 'auth_token_impersonate',
        stores: ['storage', 'cookie'],

        loginData: {
            url: '/login',
            method: 'POST',
            redirect: { name: 'dashboard' },
            fetchUser: false
        },

        logoutData: {
            url: '/logout',
            method: 'POST',
            redirect: { name: 'login' },
            makeRequest: false
        },

        refreshData: {
            url: '/refresh',
            method: 'POST',
            enabled: true,
            interval: 30
        },

        fetchData: {
            url: '/user',
            method: 'GET',
            enabled: false
        },

        authRedirect: { name: 'login' },
        forbiddenRedirect: { name: '403' },
        notFoundRedirect: { name: '404' }
    }
})

I tried by adding redirect: 'dashboard' to the login route which works as expected, if a user is already logged in it's redirected to the dashboard, but if the user is not logged in then I face a blank page.

I can't figure out what am I doing wrong

EDIT 1
I did the same question for a previous version, but I'm still facing the same issue: #493 (comment)

EDIT 2
I found that notFoundRedirect is triggered when a logged in user is trying to reach an auth: false route, but what's the correct way to define it? Can I specify a notFoundRedirect only for login route like

{
            name: 'login',
            path: '/',
            component: () => import('@/components/auth/LoginPage'),
            meta: {
                title: 'Acceso al Sistema',
                auth: false,
                notFoundRedirect: '/dashboard'
            }
        }
@websanova
Copy link
Owner

Just set meta.auth = false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants