POST http:// localhost:3000 / api / user 400(错误请求)

时间:2020-06-22 14:55:48

标签: node.js reactjs authentication react-redux mern

我正在使用MERN堆栈制作身份验证系统,后端节点API提供了JWT令牌, 该API在邮递员中绝对可以正常工作,但是当我将它与redux一起使用时,会出现此错误, 我想在客户端package.json中使用“ proxy”:“ http:// localhost:5000” Error in browser console

[authAction.js] [2]

import { REGISTER_SUCCESS, REGISTER_FAILURE } from './types';
import Axios from 'axios';

export const register = (formData) => async (dispatch) => {
    const config = {
        headers: {
            'Content-Type': 'application/json',
        },
    };
    try {
        const res = await Axios.post('/api/user', formData, config);
        dispatch({ type: REGISTER_SUCCESS, payload: res.data });
    } catch (err) {
        dispatch({ type: REGISTER_FAILURE });
    }
};

[authReducer.js] [3]

import { REGISTER_SUCCESS, REGISTER_FAILURE } from '../actions/types';

const initialState = {
    token: localStorage.getItem('token'),
    isAuthenticated: false,
    user: null,
    loading: true,
    errors: null,
};

const authReducer = (state = initialState, action) => {
    switch (action.type) {
        case REGISTER_SUCCESS:
            localStorage.setItem('token', action.payload.token);
            return {
                ...state,
                ...action.payload,
                isAuthenticated: true,
                loading: false,
                errors: null,
            };
        case REGISTER_FAILURE:
            localStorage.removeItem('token');
            return {
                ...state,
                token: null,
                isAuthenticated: false,
                loading: false,
                user: null,
            };
        default:
            return state;
    }
};

export default authReducer;

0 个答案:

没有答案