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:
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!