javascript 如何修复jsonwebtoken无法解析'buffer'?

ehxuflar  于 2023-05-05  发布在  Java
关注(0)|答案(1)|浏览(191)

我仍然是ReactJS和MERN堆栈的初学者。下面的Activate.js代码基本上意味着当useEffect()钩子运行时,我们将使用{match} props从route parameter/url获取jwt token。我们解码令牌以获得name。最后,将使用nametoken将其保存回状态-useState({})

激活.js

import React, { useState, useEffect } from 'react';
import { Link, Redirect, useParams } from 'react-router-dom';
import Layout from '../core/Layout';
import axios from 'axios';
import jwt from 'jsonwebtoken';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.min.css';

const Activate = ({match}) => {
  const [values, setValues] = useState({
    name: '',
    token: '',
    show: true
  }); // values is an object while setValues is a function

  let paramsToken = useParams(); // useParams replaced match.params.id on react-router-dom 6

  useEffect(() => {
    // let token = match.params.token; // obsolete
    let token = paramsToken;
    let { name } = jwt.decode(token); // destructure to get the name,

    if (token) {
      setValues({...values, name, token});
    }

  }, []);

  const { name, token, show } = values; // destructure

  const clickSubmit = event => {
    // code snippet
  };

  const activationLink = () => (
    <div>
      <h1 className="p-5 text-center">Hey {name}, Ready to activate your account?</h1>
      <button className="btn btn-outline-primary" onClick={clickSubmit}>Activate Account</button>
    </div>
  ); // used parenthesis so we don't use the return keyword

  return (
    <Layout>
      <div className="col-md-6 offset-md-3">
        <ToastContainer />
        {/* {JSON.stringify({name, email, password})}*/} {/* We can use this to know the value in the state */}
        {activationLink()}
      </div>
    </Layout>
  );
};

export default Activate;

包.json

{
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "axios": "^1.3.6",
    "jsonwebtoken": "^9.0.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.10.0",
    "react-scripts": "5.0.1",
    "react-toastify": "^9.1.2",
    "web-vitals": "^2.1.4"
  }
}

**问题:**我目前在使用jsonwebtoken软件包时遇到的问题是,系统提示我2-3个错误,我在下面指出了这些错误。请注意,我正在学习一个教程,教程的代码是3年前的,我的最新node.js版本和其他包是在2023年3月安装的。

未找到模块:错误:无法解析“C:\Users\john\Documents\node-projects\react\mern-ultimate-authentication\mern-auth-client\node_modules\buffer-equal-constant-time”中的“buffer”
未找到模块:错误:无法解析“C:\Users\john\Documents\node-projects\react\mern-ultimate-authentication\mern-auth-client\node_modules\jsonwebtoken”中的“crypto”
未找到模块:错误:无法解析“C:\Users\john\Documents\node-projects\react\mern-ultimate-authentication\mern-auth-client\node_modules\jsonwebtoken”中的“crypto”
重大变化:webpack〈5用于默认包含node.js核心模块的polyfills。
你知道如何解决上面的问题吗?任何帮助都非常感谢。谢谢

wmomyfyw

wmomyfyw1#

如果你想使用jwt.decode(),我认为你应该导入这个包:

import * as jwt_decode from 'jwt-decode';

或者如果你想像这样使用jwt.verify()

jwt.verify(yourToken,yourJWTKey,callbackfunction(error,userData){
  // your code here
})

相关问题