StarBlog Blog Development Notes: Frontend Development - Navigation Guards
The router object provides us with the beforeEach method, which allows us to perform certain processing before each route change, also known as navigation guards. Here, we will implement some auxiliary features similar to authentication using navigation guards. Modify the router/index.js file to add navigation guards that check if the user session has expired during each route change. If the login is valid and the user is navigating to the login page, redirect directly to the homepage; if it is a non-login page and the session has expired, redirect to the login page to require login. The code is as follows router.beforeEach
The router object provides us with the beforeEach method, which allows you to perform certain operations before each route change. Also known as navigation guards, we'll use them here to implement auxiliary features like authentication.
Modify the router/index.js file to add a navigation guard that checks whether the user's session has expired on every route change. If the login is valid and the user is navigating to the login page, redirect them directly to the homepage. If they're trying to access a non-login page but their session has expired, redirect them to the login page to authenticate.
The code is as follows:
router.beforeEach((to, from, next) => {
// After successful login on the login page, user information is saved in the session.
// Its lifetime is tied to the session, becoming invalid when the page is closed.
let userName = sessionStorage.getItem('user')
if (to.path === '/login') {
// If accessing the login page and user session info exists, meaning already logged in, redirect to the homepage
if (userName) next({path: '/'})
else next()
} else {
if (!userName) {
// If accessing a non-login page and user session info doesn't exist, meaning not logged in, redirect to the login page
next({path: '/login'})
}
}
})
_EOF_
StarBlog Blog Development Notes: Frontend Development - Navigation Guards
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
StarBlog博客开发笔记:前端开发(1)准备篇
环境准备 NodeJs 首先需要安装NodeJs 下载地址:http://nodejs.cn/download/ 安装前端工具链 国内使用NPM需要设置国内镜像才能正常安装,之前常用的淘宝镜像说是要停止解析了,可以用这个npmmirror 中国镜像,命令如下: npm config set registry https://registry.npmmirror.com npmmirror 中国镜像
2024-12-10 · StarBlog前端开发-Vue · 约 1 分钟
StarBlog博客开发笔记:前端开发之瀑布流图片列表
用到的组件:vue-waterfall 项目地址:https://github.com/MopTym/vue-waterfall 参考资料 Vue<瀑布流加载效果>:https://www.jianshu.com/p/97b89597ab5c
2023-07-14 · StarBlog前端开发-Vue · 约 1 分钟
StarBlog博客开发笔记:前端开发(6)之登录页面
前言 前端开发的准备工作在前面已经完成了,接下来就要开启写页面(Copy/Paste)操作了~ 用ElementUI写页面很简单,对照着官网文档代码一顿复制粘贴就完事了。 写代码 没啥好说的,直接代码 布局 template el-form :model=loginForm :rules=fieldRules ref=loginForm label-position=left label-wi
2023-07-14 · StarBlog前端开发-Vue · 约 1 分钟
StarBlog博客开发笔记:前端开发(4)之FontAwesome图标
在现代前端开发中,图标已成为构建用户友好界面的重要元素。Font Awesome 是全球最流行的图标库之一,提供了大量的矢量图标,支持多种平台和框架。无论是网站、应用程序,还是管理面板,Font Awesome 都能帮助开发者轻松地为界面增添直观且易于识别的视觉元素。
2025-02-13 · StarBlog前端开发-Vue · 约 1 分钟
讨论
共 0 条加载评论中…