0

Столкнулся с ошибкой "Request failed with status code 404" пытаясь отправить запрос из формы. Подскажите в чем может быть проблема.

const bcrypt = require('bcrypt')
const jwt = require('jsonwebtoken')

const User = require('../models/User')
const keys = require('../config/keys')

module.exports.login = async (req, res) => {
  const candidate = 
    await User.findOne({
      login: req.body.login
    })

  if (!candidate)
    return res.status(404).json({
      message: `Неверный логин или пароль`
    })

  const passwordResult = bcrypt.compareSync(req.body.password, candidate.password)

  if (!passwordResult)
    return res.status(404).json({
      message: `Неверный логин или пароль`
    })

  console.log(candidate)

  const token = jwt.sign({
    login: candidate.login,
    id: candidate._id  
  }, keys.JWT, {
    expiresIn: 60 * 60 * 24
  }) // Генерируем токен

  return res.status(200).json({
    token: `Bearer ${token}`
  })
}

module.exports.register = async (req, res) => {
  const candidate = 
    await User.findOne({
      login: req.body.login
    })

  if (candidate) 
    return res.status(409).json({
      message: `Пользователь с такий логином уже существует 
      попробуйте другой`
    })

  const salt = bcrypt.genSaltSync(10)
  
  const password = req.body.password

  const user = new User({
    login: req.body.login,
    password: bcrypt.hashSync(password, salt)
  })

  try {
    await user.save() 

    return res.status(201).json(user)
  } catch(e) {
    console.log(`При добавлении пользователя произошла ошибка`)
  }
}

import React, { Component } from 'react'
import axios from 'axios'

class Auth extends Component {

  state = {
    name: '',
    password: '369369'
  }

  handleChange = (e) => {
    this.setState({
      name: e.target.value
    })

    console.log(this.state.name)
  }

  handleSubmit = (e) => {
    e.preventDefault()

    const user = {
      login: "test@test",
      password: "369369"
    }

    axios.get('/api/auth/login', { params: user }) //Использую прокси
      .then(res => console.log(res))
        .catch(err => console.log(err))
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <input type="text" name="login" onChange={this.handleChange} placeholder="Логин"/>
        <input type="password" name="password" placeholder="Пароль"/>
        <button type="submit">Отправить</button>
      </form>
    )
  }
}

export default Auth

  • 1
    а как module.exports.login и module.exports.register подключены к роутингу? – nörbörnën 1 час назад
  • router.get('/login', controller.login) router.post('/register', controller.register) – Егор Гришин 1 час назад

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Посмотрите другие вопросы с метками или задайте свой вопрос.