美文网首页
react 拦截非登录用户对页面的查看

react 拦截非登录用户对页面的查看

作者: Veycn | 来源:发表于2019-03-17 18:16 被阅读0次

当用户没有登录的时候, 有些页面不能允许其访问.
具体的做法是: 重新自定义一个路由, 然后依据用户时候登录, 执行跳转逻辑


比如, 用户未登录, Topic 页面就不允许他访问, 转而跳转至登录界面, 这类似与Vue的导航守卫. 但是react没有提供这类API
PrivateRoute.js

import React from 'react'
import { Route, Redirect } from 'react-router-dom'
// 这个组件将根据登录的情况, 返回一个路由
const PrivateRoute = ({component: Component, ...props}) => {
    // 解构赋值 将 props 里面的 component 赋值给 Component
    return <Route {...props} render={(p) => {
        const login = document.cookie.includes('login=true')
        if (login){ // 如果登录了, 返回正确的路由
            return <Component />
        } else { // 没有登录就重定向至登录页面
            alert("你还没有登录哦, 确认将跳转登录界面进行登录!")
            return <Redirect to={{
                pathname: '/login',
                state: {
                    from: p.location.pathname
                }
            }}/>
        }
    }}/>
}
export default PrivateRoute

Login.js

import React from 'react'
import './login.css'

export default class Login extends React.Component{
    state = {
        isLogin: document.cookie.includes('login=true')
    }
    handleClick = () => {
        let login = !this.state.isLogin
        this.setState({
            isLogin: login
        })
        if(login){
            // 设置cookie之后跳转回来时的页面
            this.setCookie('login', true, 15)
            this.jumpBack()
        } else {
            // 设置时间为负数, 取消设置的 cookie
            this.setCookie('login', '', -1)
        }

    }
    setCookie = (key, value, day) => {
        let expires = day * 86400 * 1000  // 时间转化成 ms
        let date = new Date( + new Date() + expires) // 当前时间加上要存储的时间
        document.cookie = `${key}=${value};expires=${date.toUTCString()}`
    }
    jumpBack = () => {
        // 打哪儿来回哪去
        const { location } = this.props
        const from = location.state && location.state.from
       //  const article = location.state && location.state.article
        this.props.history.push({
            pathname: from,
            state: {
                // article
            }
        })
    }
    render() {
        return (
            <div className={'login'}>
                <button className={'login-btn'} onClick={this.handleClick}>{ this.state.isLogin ? '退出' : '登录' }</button>
            </div>
        );
    }
}

通过设置浏览器cookie 模拟了用户登录或者未登录.
当匹配到这个路由的时候, 首先就会去判断这个用户是否登录了, 如果是,就返回应该给用户看到的组件Component, 如果不是, 就提醒用户去登录, 并且跳转到登录页面.RedirectLogin页面.
如果用户在登录了之后, 希望直接跳转回Topic页面, 而不用手动, 那么就将当前路径pathname传递过去, 在Login页面确认用户登录之后, 再根据这个传递过去的pathname直接Redirect回到Topic页面.

相关文章

  • react 拦截非登录用户对页面的查看

    当用户没有登录的时候, 有些页面不能允许其访问.具体的做法是: 重新自定义一个路由, 然后依据用户时候登录, 执行...

  • SpringBoot通过拦截器拦截非登录用户

    使用springmvc中的intercepter进行页面的拦截,拦截除了登录界面的所有页面,当用户登录过后产生保存...

  • Node.js + Express 登录拦截

    原文链接 利用 Express 中间件功能实现登录拦截。如果用户请求的路径需要登录后才能访问,将用户重定向到登录页...

  • 自己理解的Spring Security流程

    用户发起对rest服务的请求,因为非登录请求,会通过前面的过滤器链(在配置中指定的过滤器会生效),到达最后一道拦截...

  • springboot拦截器的多重重定向问题

    使用springboot 拦截器,作为登录拦截,如果没有登录的用户不能访问主页,去到登录页面登录, 1.在声明拦截...

  • 在Vue 项目中你可能会遇到的问题

    一、项目的登录拦截及用户权限访问控制问题。 一个很常见的需求就是对未登录的用户进行路由拦截和用户的权限访问,如果你...

  • 用户登录拦截

    有些网页如果不拦截的话直接就能登录上去了,我用了WebMvcConfigurer。用的是springboot2.x...

  • day10

    查看命令位置:which 查看当前用户:whoami 查看登录用户:who 退出登录账户:exit ...

  • 7.springboot中的filter 和 listener

    拦截器filter(拦截请求,对请求进行预处理): 例如读取session判断用户是否登录,判断访问的请求URL是...

  • linux基本命令指南8:用户登录查看

    用户登录查看命令查看登录用户信息:w 用户名who 用户名 查询当前登录和过去登录的用户信息last#默认读取/v...

网友评论

      本文标题:react 拦截非登录用户对页面的查看

      本文链接:https://www.haomeiwen.com/subject/glnumqtx.html