当前位置:   article > 正文

react登录页面的判断token是否存在_react + ts 判断cookie里有没有token

react + ts 判断cookie里有没有token

1.创建一个高阶函数来判断token是否存在

  1. import { Navigate } from 'react-router-dom'
  2. import { getToken } from '../utile/index'
  3. function AuthComponent({ children }) {
  4. const isToken = getToken()
  5. if (isToken) {
  6. return <>{children}</>
  7. } else {
  8. return <Navigate to='/login' replace />
  9. }
  10. }
  11. export { AuthComponent }

2.封装本地存储token

  1. / 封装locaStrot存取token
  2. // 设置key属性
  3. const key = 'pc-key'
  4. // 存token
  5. const setToken = (token) => {
  6. return window.localStorage.setItem(key, token)
  7. }
  8. // 取token
  9. const getToken = () => {
  10. return window.localStorage.getItem(key)
  11. }
  12. // 清除token
  13. const removeToken = () => {
  14. return window.localStorage.removeItem(key)
  15. }
  16. export { setToken, getToken, removeToken }

3.在封装的axios请求拦截器中添加token,

  1. 请求拦截器
  2. http.interceptors.request.use((config) => {
  3. // 请求拦截器注入token
  4. const token = getToken()
  5. if (token) {
  6. config.headers.Authorization = `Bearer ${token}`
  7. }
  8. return config
  9. }, (error) => {
  10. return Promise.reject(error)
  11. })

4.创建mobx 文件:

  1. import LoginStore from './login.Store.js'
  2. import React from 'react'
  3. class RootStore {
  4. constructor() {
  5. this.loginStore = new LoginStore()
  6. }
  7. }
  8. const rootStore = new RootStore()
  9. const context = React.createContext(rootStore)
  10. const useStore = () => React.useContext(context)
  11. export { useStore }

 

5.然后再mobx中获取后台数据并赋值变量和存储本地token

  1. import { http } from '../utile/index'
  2. import { makeAutoObservable } from 'mobx'
  3. import { setToken } from '../utile/index.jsx'
  4. class LoginStore {//刷新页面token不会丢失
  5. token = 'a35cc861-2822-4a9b-a35f-b27fbc1651d2'
  6. constructor() {
  7. makeAutoObservable(this)
  8. }
  9. setToken = async ({ mobile, code }) => {
  10. // 调用登录接口
  11. const res = await http.post('http://geek.itheima.net/v1_0/authorizations', { mobile, code })
  12. // 获取的后台数据赋值给token
  13. this.token = res.data.data
  14. // 把token存到本地存储并传入参数
  15. setToken(this.token)
  16. }
  17. }
  18. export default LoginStore

 6.在app.js中导入高阶函索判断首页中是否有token,如果有就正常跳转页面,如果没有就跳转到登录页login

  1. import './App.css';
  2. import { BrowserRouter, Routes, Route } from 'react-router-dom'
  3. import Layout from './pages/Layout'
  4. import Login from './pages/Login'
  5. // 导入token判断的高阶函数
  6. import { AuthComponent } from './component/AuthComponent'
  7. function App() {
  8. return (
  9. <div className="App">
  10. <BrowserRouter>
  11. <Routes>
  12. {/* 这个是需要判断token是否存在 */}
  13. <Route path='/' element={<AuthComponent><Layout /></AuthComponent>}></Route>
  14. <Route path='/login' element={<Login />}></Route>
  15. </Routes>
  16. </BrowserRouter>
  17. </div>
  18. );
  19. }
  20. export default App;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/513642
推荐阅读
相关标签
  

闽ICP备14008679号