小编Ово*_*чоы的帖子

为什么 cookie 不通过 Next.js 中的 getServerSideProps 发送到服务器?

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)

也就是说,当我通过getServerSidePropscookie 发送请求时,不会到达服务器,尽管如果我发送,例如通过useEffect …

node.js jwt reactjs next.js nestjs

23
推荐指数
1
解决办法
3万
查看次数

“组件”不能用作 JSX 组件。下一个

这是 _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)

javascript typescript reactjs next.js

7
推荐指数
2
解决办法
7941
查看次数

如何从jwt策略中获取有效负载?

我有 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 策略中返回的控制器中的有效负载呢?

node.js jwt nestjs nestjs-jwt

1
推荐指数
1
解决办法
1587
查看次数

标签 统计

jwt ×2

nestjs ×2

next.js ×2

node.js ×2

reactjs ×2

javascript ×1

nestjs-jwt ×1

typescript ×1