How to get response after http post

currently, I am writing a web page, front-end on React Js and Back on Golang, there are several input forms on the page, and when user enter data, I am making post request on this route (’/user/auth’) and validating these inputs with golang, so when password is wrong I want to show a message on my front-end, how can I do it? how to send some data from golang to react after checking the inputs

Are you making the post as an rest-api call or sending the browser to this page?

Rest api

I don’t get what’s the issue, you could simply respond to the request with some simple json:

{
  "status": "error",
  "error": "invalid username or password"
}

You could also respond with plain text, where an empty response means that everything went fine, and an error message is sent when something goes wrong. Or maybe just use http response codes to signal an error. Anyway, if you give an error message in the json form, then check at your frontend for the status and show the error when needed.

Take one thing in consideration though: never expose that a specific username or password is wrong, that’s just giving away clues for an attacker, always mention that the pair doesn’t match.

You also must set some cookie for your authenticated session or similar to know that the user is authenticated.

That depends on how you are authenticating/authorizing your users. You could very well go with JWT tokens and attach the token to the header on every request, or manage the session at your DB, or whatever else you can think of. I’m not saying one option is better than the other, only that there are plenty of options to manage user sessions an the poster should research them.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.