React component only displays status code error message instead of custom ones - reactjs

During POST to register new user I check if that user already exists in mongoDB data base and if yes display this in react near the register form. I'm using express.js to create my API and axios to send requests from client side. This is how I check if user is registered.
const user = await Model.User.findOne({email: req.body.email});
if(user) return res.status(400).send('User already registered');
This is the function that is being called on Register button click:
handleSubmit= async (userData) =>{
await apis.registerNewUser(userData)
.then(res => console.log(res.body))
.catch(err => {
console.log(err);
this.setState({errorMessage: err.message});
})
}
The problem is that I can't display the custom error message that I add when sending response. Instead of getting "User already registered" I get "400 Bad request" showing near my register form. When I post through postman there is no problem, the response "User already registered" shows up in the response text window however that part doesnt exist when recieving error on react side. I can't send user an error that tells him nothing about the reason why he can't register. How can I get "User already registered" in react?

I have found the solution. In order to read the property of that custom text put inside send() when catching error I need to read from err.response.data - that's the right property to read "User already registered".I hope it helps someone one day.

Related

Amplify Cognito Auth, Can I verify the forgotten password code on its own page?

I am using Amplify and creating a forgotten password journey for my app. I am using this function:
import { Auth } from 'aws-amplify';
// Send confirmation code to user's email
Auth.forgotPassword(username)
.then(data => console.log(data))
.catch(err => console.log(err));
// Collect confirmation code and new password, then
Auth.forgotPasswordSubmit(username, code, new_password)
.then(data => console.log(data))
.catch(err => console.log(err));
As specified in the docs here
What I want to achieve is a 3 page journey.
enter email address
enter code (and have it validated before moving on to the next page)
enter new password
The issue that I am having is that the provided forgotPasswordSubmit function takes all 3 args, when at this point, I only want to send 2 (email and code).
My plan was to send the email, and the code, and an empty string for the password, and then check the returned errors to see if there was one relating to the code being incorrect however I only ever get an error for the password being invalid.
Is there a way that I can get amplify to send back all errors at once (in this case the password being invalid but ALSO the code not being correct), or is there a nicer way of doing this?
I have searched for ages for an answer before asking here so any help would be much appreciated.

Adding web-notification in React app similar to Alert but Not Alert

user story:
We need to alert the user when there a modification or a comment on the thing he has posted by another user.
We are new to react and we are lost in a loop about how to notify the user who is logged in on a different page (maybe his dashboard). The event is performed by another user2 where he is on a page where is commenting on the thing posted by user1.
How can we send an alert to that user1? who currently on a different page saying "there is a new comment on your post".
The issue we are thing about is: The event happens on the comment page. We can send the alert on that comment page. But how will we be able to send a notification to other users on a different page?
I know there is already present. You can take Jiira Board as an example.
Could anyone let us know how can we implement this?
One option is to use WebSockets. Whenever a user is on a page where you'd want them to be able to get a notification, open a websocket to your server:
const socket = new WebSocket('https://my-server.com/socket');
On your server, set up the socket endpoint. Whenever one user sends a message to another, on the server, for all sockets currently opened by the receiver, send a socket message informing them of the new message. Eg, on the server:
activeSockets
.filter(({ userId, socket }) => userId === receiverId)
.forEach(({ socket }) => {
socket.send('You have a new message');
});
And listen for those messages on the client:
socket.addEventListener('message', ({ data }) => {
if (data === 'You have a new message') {
alert(data);
}
});
This is, in broad terms, the industry standard for this sort of thing; it's what Stack Exchange does. It's how many websites allow the server to send data to the client without (inelegant) polling.

How to get a Discord bot to send a message to a specfic channel when created?

I'm making a bot which on a react to a certain will create a channel...
That all works perfectly expect I want a nessage to be posted when the cahnnel is created which has a specfic beginning.
client.on('channelCreate', (channel, message) => {
if(channel.name.startsWith('ticket-')){
message.channel.send('test');
});
I'm not getting any errors, just nothing...
You can't use the message variable in the channelCreate event. The only thing you're receiving is a channel object, so you need to use channel.send():
client.on('channelCreate', (channel, message) => {
if(channel.name.startsWith('ticket-')){
channel.send('test');
});

I am trying to add firebase email varification link varification to my app but getting error "can not read email propperty of null"

I am trying to add firebase email verification link verification to my app but getting an error cannot read email property of null
I tried to follow the firebase docs for the web but not able to find the complete solution exactly where and how to implement it
var actionCodeSettings = {
url: 'campatron.com' + firebase.auth().currentUser.email,
handleCodeInApp: true,
// When multiple custom dynamic link domains are defined, specify which
// one to use.
dynamicLinkDomain: "example.page.link"
};
firebase.auth().currentUser.sendEmailVerification(actionCodeSettings)
.then(function() {
// Verification email sent.
})
.catch(function(error) {
// Error occurred. Inspect error.code.
});
The currentUser property is not yet initialized, meaning that your user is not yet logged in when you reach this code. Where is this piece of code called in your app?

Showing error message on MEAN website

I am quite new to MEAN and I am learning a lot. At the moment I am trying to show an error message on my page when an user is not allowed into the website. The page contains a button which redirects you to the steam login. After you login the steam API sends your steamid which I will then check in the mongodb database:
app.get('/auth/steam/return',
passport.authenticate('steam', { failureRedirect: '/' }),https://stackoverflow.com/users/5333805/luud-van-keulen
function(req, res) {
UserModel.findOne({ steamid : req.user.id }, function (err, user) {
if(!user) {
console.log('does not exist');
//Probably have to set the error message here
} else {
req.session.userid = req.user.id; //Setting the session
}
});
res.redirect('/');
});
The only thing that I can't get working is how to show a message when the user is not allowed (he is not in the database). I want to use AngularJS for the HTML (so no Jade).
I do know that I have to set a variable somewhere in the response header and then with AngularJS I need to check if this variable exists or not. When It exist it should show the div which contains the error message.
The problem is that I can't use res.render because I need to redirect.
So in the block where user is not found, you should have something like:
res.status(401).send("Login failed.");
And then on the client side you can check the response status and display the mesage.
Edit: if you need help on the client side as well, please provide your client code.
I ended up using express-flash.

Resources