繁体   English   中英

Email 使用 React 和 Flask 进行验证

[英]Email Verification with React and Flask

我在我的 ReactJS 应用程序中设置了 Email 验证 Function,后端使用 Z319C3206A7F10C17C3B9116。 但由于某种原因,它发出了一些错误。 错误,出现 500 - http://localhost:5000/send-verify-email内部错误。

PFA 相关文件。 如果我错过了任何文件或信息,请告诉我。

有人请告诉我这个问题 -

这是验证按钮的事件处理程序 -

    const handleClick = ( user ) => {
        if(user.email) {
            // Dispatch send email link
            dispatch(sendEmailVerifyLink(user.email))
        }
    }

auth.js 文件

// Action generator for sending verify email link`enter code here`
export const sendEmailVerifyLink = (recipientEmail) => (dispatch) => {

  // Dispatch Start Process
  dispatch({
    type: EMAIL_VERIFY_SEND_LINK
  })

  // create data body object
  const data = {
    email: recipientEmail
  }

  // send request to server
  axios.post('/send-verify-email', data)
    .then((res) => {
      // Dispatch success action
      dispatch({
        type: EMAIL_VERIFY_SEND_LINK_SUCCESSFULLY
      })

      // set Alert
      dispatch(setAlert(res.data.message, 'success'))
    })
    .catch((err) => {

      // Create response from error object
      const res = err.response

      // Dispatch fail action
      dispatch({
        type: EMAIL_VERIFY_SEND_LINK_FAILED
      })

      if(res && (res.status === 404 || res.status === 422 || res.status === 500))
      {
        // Set ALert for the above status codes
        dispatch(setAlert(res.data.message))
        return
      }

      // Set error alert
      dispatch(setAlert('Something went wrong.'))
    })
}

auth.py 文件

@auth.route('/send-verify-email', methods=["POST"])
def send_verify_email():
    """
    Route to handle and send verify email message.
    """

    # Get Recipient email address
    try:
        req = request.get_json()
        recipientEmail = req["email"]

    except:
        response = {
            "success": False,
            "message": "Please Provide Recipient Email Address."
        }
        return make_response(jsonify(response)), 422

    # Check if user exists or not
    user = ( Admin.query.filter_by(email=recipientEmail).first() or
             Extractor.query.filter_by(email=recipientEmail).first() or
             Management.query.filter_by(email=recipientEmail).first() )

    # return if user not exist
    if(not user):
        response = {
            "success": False,
            "message": "User Not Found, Please a Registered Email ID."
        }
        return make_response(jsonify(response)), 404

    # Check if user is verified or not
    if(user.verified):
        response = {
            "success": False,
            "message": "User is already verified."
        }
        return make_response(jsonify(response)), 422

    # create access token
    token = user.encode_access_token()

    # Verify Email Link
    verify_link = f"http://localhost:3000/verify-email/{token}"


    # Send reset link meail message
    try:
        send_verify_link_mail(recipient=recipientEmail, verify_link=verify_link)
        response = {
            "success": True,
            "message": f"Email Verification link has been sent to {recipientEmail}."
        }
        return make_response(jsonify(response)), 201

    except Exception as e:
        response = {
            "success": False,
            "message": 'Something went wrong.',
        }
        return make_response(jsonify(response)), 500

也许您的问题出在 CORS 上。 默认情况下,服务器不接收前端请求,但您可以轻松修复安装 flask-cors(pip install flask-cors)的问题。 所以试着在你的代码中写这个:

from flask_cors import CORS

app = Flask(__name__)
CORS(app)

来源: https://flask-cors.readthedocs.io/en/latest/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM