Cookies不是通过 发送到服务器的getServerSideProps
,下面是前端的代码:
export async function getServerSideProps() {
const res = await axios.get("http://localhost:5000/api/auth", {withCredentials: true});
const data = await res.data;
return { props: { data } }
}
Run Code Online (Sandbox Code Playgroud)
在服务器上,我有一个检查访问 JWT 令牌的策略。
export class JwtStrategy extends PassportStrategy(Strategy, "jwt") {
constructor() {
super({
ignoreExpiration: false,
secretOrKey: "secret",
jwtFromRequest: ExtractJwt.fromExtractors([
(request: Request) => {
console.log(request.cookies) // [Object: null prototype] {}
let data = request.cookies['access'];
return data;
}
]),
});
}
async validate(payload: any){
return payload;
}
}
Run Code Online (Sandbox Code Playgroud)
也就是说,当我通过getServerSideProps
cookie 发送请求时,不会到达服务器,尽管如果我发送,例如通过useEffect …
这是 _app.tsx 的样子:
function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
Run Code Online (Sandbox Code Playgroud)
我在构建项目时收到此错误:
Type error: 'Component' cannot be used as a JSX component.
Its element type 'ReactElement<any, any> | Component<{}, any, any> | null' is not a valid JSX element.
Type 'Component<{}, any, any>' is not assignable to type 'Element | ElementClass | null'.
Type 'Component<{}, any, any>' is not assignable to type 'ElementClass'.
The types returned by 'render()' are incompatible between these types.
Type 'React.ReactNode' is …
Run Code Online (Sandbox Code Playgroud) 我有 jwt 策略:
export class JwtStrategy extends PassportStrategy(Strategy, "jwt") {
constructor() {
super({
ignoreExpiration: false,
secretOrKey: "secret",
jwtFromRequest: ExtractJwt.fromExtractors([
(request: Request) => {
let data = request.cookies['access'];
return data;
}
]),
});
}
async validate(payload: any){
return payload;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
export class AuthController {
constructor(private authService: AuthService) {}
@UseGuards(AuthGuard("jwt"))
@Get()
getPayload() {
//here I need to get the payload that was returned in jwt strategy
}
}
Run Code Online (Sandbox Code Playgroud)
那么如何获取 jwt 策略中返回的控制器中的有效负载呢?