Please I need a set of expert eyes on these real quick :
what would happen if the error did not come up
The login page would collect the user’s gmail and password , send it to the login url and get an object containing the authorization token would be returned. Works perfectly on postman.
Inside vscode :
This is the function handle the authorization:
const [loginInput, setLoginInput] = React.useState({
email: '',
password: ''
})
const handleSubmit = async (e) =>{
e.preventDefault();
try{
const response = await axios.post('https://adscamp.thevootblog.com/api/v1/users/login',
JSON.stringify(loginInput.username, loginInput.password),
{
headers: { 'Content-Type' : 'application/json',
'Authorization': `Bearer ${response?.token}`
}
}
);
console.log(response)
}catch(err){
console.log(err)
}
}
I’m not getting the object
"cannot access ‘response’ before initialization" ,which means response is empty.
extra information :
I imported axios into login.js file from another file :
api(folder) => api.js
import axios from "axios";
export default axios.create({
baseURL: 'http://localhost:3000'
})
2
Answers
First of all you can only
JSON.stringify
JSON objectschange
to
And the error you are seeing is because you’re trying to access the response inside the headers before even the http request initalized.
the purpose of this HTTP request is to get the
Authorization Token
so remove the Authorization header.and remove the line
This is node.js code
Install dependency
Result
Curl commend
Result
Now you needs to hide your password.