【发布时间】:2021-01-13 23:12:59
【问题描述】:
我有一个向我的后端发送 post 请求的函数,它包含 JWT 令牌,然后后端检查这个令牌是否正确。不幸的是,isAuthenticated() 函数仅在我返回 Promise 时才有效。但是我现在想在我的 ProtectedRoute.tsx 中检查令牌是否正确。这如何与 Promise 一起使用?
isAuthenticated():
import axios from "axios"
let token = localStorage.getItem("jwt-token");
export const isAuthenticated = async (): Promise<boolean> => {
if (!token) {
return false;
} else {
let tokenCheck = false;
await axios.post("/api/users/tokencheck", { token: token }).then((res) => {
if (res.data === "Valid Token" || res.status === 200) {
tokenCheck = true
} else {
tokenCheck = false
}
})
return tokenCheck
}
}
ProtectedRoute.tsx:
import React from 'react'
import { Redirect, Route } from 'react-router-dom'
import { isAuthenticated } from "./Auth"
interface Props {
component: React.FC;
path: string;
}
const ProtectedRoute: React.FC<Props> = (props: Props) => {
return (
<React.Fragment>
{isAuthenticated()
? <Route path={props.path} exact component={props.component} />
: <Redirect to="/" />
}
</React.Fragment>
)
}
export default ProtectedRoute
后端:
export const tokenCheck = (req: Request, res: Response) => {
let { token } = req.body
jwt.verify(token!, process.env.JWTSECRET!, (err: any, decoded: any) => {
if (err) {
res.status(401).json("Invalid token");
} else {
res.status(200).json("Valid Token")
}
})
}
【问题讨论】:
-
考虑返回
then中的值,然后返回await axios.post的输出? -
store isAuthenticated 到 redux 或者使用 Context, ProtectedRoute 观察结果
-
isAuthenticated() 被声明为返回一个 Promise,但它不返回一个 Promise:它返回 true 或 false。
标签: javascript reactjs typescript promise jwt