Home  >  Article  >  Web Front-end  >  Koa2 implements an interceptor to verify the session before logging in

Koa2 implements an interceptor to verify the session before logging in

不言
不言Original
2018-07-07 10:35:354430browse

This article mainly introduces the koa2 implementation interceptor to verify the session before logging in. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

//定义允许直接访问的urlconst allowpage = ['/login','/api/login']//拦截function localFilter(ctx) {
    let url = ctx.originalUrl    if (allowpage.indexOf(url) > -1) {
        logger.info('当前地址可直接访问')
    }else {        if (ctx.isAuthenticated()) {            if(url==='/'){
                ctx.redirect('/projectList')
            }
            console.log('login status validate success')
        } else {
            console.log('login status validate fail')
            console.log(ctx.request.url)
            ctx.redirect('/login')
        }
    }
}//session拦截app.use(async (ctx, next) => {
    localFilter(ctx)
    await next()

})

When using koa After -passport and koa-session middleware, user login verification can be performed. With this interceptor, you can verify whether you are logged in before entering all pages, and write routes that do not need to be intercepted in the allowpage array.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

vue How to monitor whether the user has modified the information when switching pages after getting the page details

How about Angular Correct operation of DOM

The above is the detailed content of Koa2 implements an interceptor to verify the session before logging in. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn