ReactJS - how can I handle notifications to user based on server response? - reactjs

so I've been trying to figure out
how to handle some notifications/alerts to show users some information based on what they typed in for example login form.
const DoLogin = async (email, password) => {
const loginTeacher = await axios.post(
"http://localhost:3000/teachers/login",
{
email,
password
}
);
return loginTeacher;
};
class Login extends React.Component {
state = {
email: "",
password: "",
logged: false,
status: "",
errorMessage: ""
};
onEmailChange = e => {
this.setState({
email: e.target.value
});
};
onPassChange = e => {
this.setState({
password: e.target.value
});
};
onSubmit = e => {
e.preventDefault();
DoLogin(this.state.email, this.state.password)
.then(res => {
localStorage.setItem("mysecrettoken", res.data.token);
this.setState({ teacher: res.data, logged: true, status: res.status });
alert("Successfully logged in");
})
.catch(err => {
alert("Unable to login in, user not found");
});
};
loginForm() {
return (
<div className="Login form">
<form onSubmit={this.onSubmit}>
<label htmlFor="email">
Email:
<input
type="text"
name="email"
value={this.state.email}
onChange={this.onEmailChange}
/>
</label>
<br />
<label htmlFor="password">
Hasło:
<input
type="password"
name="password"
value={this.state.password}
onChange={this.onPassChange}
/>
</label>
<br />
<input type="submit" value="Zaloguj" />
<input type="button" value="Dodaj nauczyciela" />
</form>
</div>
);
}
}
Now, whenever a user is able to login it shows alert with the message, but I don't think that's a good way to show user information.
Could you please help me with that? Some articles/libraries would be great. I've tried to implement react toast but I failed to do that.

You can store the details in the state (like you already do) and then access in the render method for conditional rendering if the user has logged in.
const DoLogin = async (email, password) => {
const loginTeacher = await axios.post(
"http://localhost:3000/teachers/login",
{
email,
password
}
);
return loginTeacher;
};
class Login extends React.Component {
state = {
email: "",
password: "",
logged: false,
status: "",
errorMessage: ""
};
onEmailChange = e => {
this.setState({
email: e.target.value
});
};
onPassChange = e => {
this.setState({
password: e.target.value
});
};
onSubmit = e => {
e.preventDefault();
DoLogin(this.state.email, this.state.password)
.then(res => {
localStorage.setItem("mysecrettoken", res.data.token);
this.setState({ teacher: res.data, logged: true, status: res.status, showingMessage: true });
setTimeout(() => {
this.setState({ showingMessage: false })
}, 2000)
alert("Successfully logged in");
})
.catch(err => {
// update state with ERROR
this.setState({ error: err.message })
alert("Unable to login in, user not found");
});
};
loginForm() {
if (this.state.logged && this.state.showingMessage) {
return (<div>You've logged in as {this.state.teacher.name}</div>)
}
return (
<div className="Login form">
{/* display ERROR */}
{this.state.error && <span style="color:red">
There was an error during registration: {this.state.error}.
</span>}
<form onSubmit={this.onSubmit}>
<label htmlFor="email">
Email:
<input
type="text"
name="email"
value={this.state.email}
onChange={this.onEmailChange}
/>
</label>
<br />
<label htmlFor="password">
Hasło:
<input
type="password"
name="password"
value={this.state.password}
onChange={this.onPassChange}
/>
</label>
<br />
<input type="submit" value="Zaloguj" />
<input type="button" value="Dodaj nauczyciela" />
</form>
</div>
);
}
}
I don't know where your render method is, but basically just access the state. You can also set a timeout after you received the data, and add another property on the state, like showingMessage which will be true at first, and then in say 2s will be false, then your condition would be if (this.state.logged & this.state.showingMessage).

Related

Sending email using react and nodemailer

Been trying to make a contact form which uses nodemailer. I used Postman to check if my backend is working, and it is. I even get an email in my outlook. However, I am stuck at the front end bit. I just can't seem to get the actual contact form to send an email on submit. My code is below.
Backend
app.use(express.json());
app.use(bodyParser.json());
app.use(cors());
app.use(express.urlencoded({ extended: false }));
const transporter = nodemailer.createTransport({
service: "hotmail",
auth: {
user: ,
pass: ,
}
});
transporter.verify(function (error, success) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
}
});
app.post('/send', (req, res, next) => {
var name = req.body.name
var email = req.body.email
var subject = req.body.subject
var message = req.body.message
var mail = {
from: name,
to: ,
subject: subject,
text: message
}
transporter.sendMail(mail, (err, data) => {
if (err) {
res.json({
status: 'fail'
})
} else {
res.json({
status: 'success'
})
}
})
})
Frontend
import axios from "axios";
import React from 'react';
class ContactForm extends React.Component {
constructor(props) {
super(props);
this.state = {
name: '',
email: '',
subject: '',
message: ''
}
}
onNameChange(event) {
this.setState({ name: event.target.value })
}
onEmailChange(event) {
this.setState({ email: event.target.value })
}
onSubjectChange(event) {
this.setState({ subject: event.target.value })
}
onMsgChange(event) {
this.setState({ message: event.target.value })
}
submitEmail(e) {
e.preventDefault();
axios({
method: "POST",
url: "/send",
data: this.state
}).then((response) => {
if (response.data.status === 'success') {
alert("Message Sent.");
this.resetForm()
} else if (response.data.status === 'fail') {
alert("Message failed to send.")
}
})
}
resetForm() {
this.setState({ name: '', email: '', subject: '', message: '' })
}
render() {
return (
<div className="section">
<div className="container">
<div className="row">
<div className="col-md-12">
<div className="section-title">
<h2 className="title">Contact Us</h2>
<p>Let us know what you think! In order to provide better service,
please do not hesitate to give us your feedback. Thank you.</p><hr />
<form id="contact-form" onSubmit={this.submitEmail.bind(this)}
method="POST">
<div className="form-group">
<div className="row">
<div className="col-md-6">
<input placeholder="Name" id="name" type="text"
className="form-control" required value={this.state.name}
onChange={this.onNameChange.bind(this)} />
</div>
<div className="col-md-6">
<input placeholder="Email" id="email" type="email"
className="form-control" aria-describedby="emailHelp"
required value={this.state.email} onChange=
{this.onEmailChange.bind(this)} />
</div>
</div>
</div>
<div className="form-group">
<input placeholder="Subject" id="subject" type="text"
className="form-control" required value={this.state.subject}
onChange={this.onSubjectChange.bind(this)} />
</div>
<div className="form-group">
<textarea placeholder="Message" id="message"
className="form-control" rows="1"
required value={this.state.message}
onChange={this.onMsgChange.bind(this)} />
</div>
<button type="submit" className="primary-btn submit">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
);
}
}
export default ContactForm;

this.state not populating fields in editProfile.component

Newbie here. Basic question I know. I have made a 'newProfile' component using pretty much the same mechanics as this and it's working! Now I need an editProfile component that updates the Profile form with props from the database using params.id. The URL shows the .id piece is working when I click 'edit' on a profile in a profileList component that is also working. This code is not getting errors, but it is not showing state for each of the fields.
What am I missing?
`
export default class EditProfile extends Component {
constructor(props) {
super(props);
this.onChangeUsername = this.onChangeUsername.bind(this);
this.onChangeFirst = this.onChangeFirst.bind(this);
this.onChangeLast = this.onChangeLast.bind(this);
this.onChangeEmail = this.onChangeEmail.bind(this);
this.onChangePassword = this.onChangePassword.bind(this);
this.onChangeDob = this.onChangeDob.bind(this);
this.onChangeLocation = this.onChangeLocation.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.state = {
username: '',
first: '',
last: '',
email: '',
password:'',
dob:'',
location:'',
}
}
componentDidMount() {
axios.get('http://localhost:5000/profiles/'+this.props.match.params.id)
.then(response => {
this.setState({
username: response.data.username,
first: response.data.first,
last: response.data.last,
email: response.data.email,
password: response.data.password,
dob: response.data.dob,
location: response.data.location
})
})
.catch(function (error) {
console.log(error);
})
}
componentDidMount() {
axios.get('http://localhost:5000/users/')
.then(response => {
if (response.data.length > 0) {
this.setState({
users: response.data.map(user => user.username),
})
}
})
.catch((error) => {
console.log(error);
})
}
onChangeProfilePic(e) {
this.setState({
profilePic: e.target.value
});
}
onChangeUsername(e) {
this.setState({
username: e.target.value
});
}
onChangeFirst(e) {
this.setState({
first: e.target.value
});
}
onChangeLast(e) {
this.setState({
last: e.target.value
});
}
onChangeEmail(e) {
this.setState({
email: e.target.value
});
}
onChangePassword(e) {
this.setState({
password: e.target.value
});
}
onChangeDob(e) {
this.setState({
dob: e.target.value
});
} onChangeLocation(e) {
this.setState({
location: e.target.value
});
}
onSubmit(e) {
e.preventDefault();
const profile = {
username: this.state.username,
first: this.state.first,
last: this.state.last,
email: this.state.email,
password: this.state.password,
dob: this.state.dob,
location: this.state.location,
}
console.log(profile);
axios.post('http://localhost:5000/profiles/update'+this.props.match.params.id, profile)
.then(res => console.log(res.data));
window.location = '/';
}
render() {
return (
<div>
<h3>Edit Profile
</h3>
<form onSubmit={this.onSubmit}>
<div className="form-group">
<label>Username:
</label>
<input
type="text"
className="form-control"
value={this.state.username}
onChange={this.onChangeUsername}
/>
</div>
<div className="form-group">
<label>First Name:
</label>
<input
type="text"
className="form-control"
value={this.state.first}
onChange={this.onChangeFirst}
/>
</div>
<div className="form-group">
<label>Last Name:
</label>
<input
type="text"
className="form-control"
value={this.state.last}
onChange={this.onChangeLast}
/>
</div>
<div className="form-group">
<label>Email:
</label>
<input
type="text"
className="form-control"
value={this.state.email}
onChange={this.onChangeEmail}
/>
</div>
<div className="form-group">
<label>Password:
</label>
<input
type="text"
className="form-control"
value={this.state.password}
onChange={this.onChangePassword}
/>
</div>
<div className="form-group">
<input type="submit" value="Save" className="btn btn-primary" />
</div>
</form>
</div>
)}
}
`
Here is the error I'm getting in the console.
react-dom.development.js:86 Warning: A component is changing a controlled input to be uncontrolled. This is likely caused by the value changing from a defined to undefined, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://reactjs.org/link/controlled-components
at input
at div
at form
at div
at CreateProfile (http://localhost:3000/static/js/bundle.js:194:5)
at RenderedRoute (http://localhost:3000/static/js/bundle.js:44214:5)
at Routes (http://localhost:3000/static/js/bundle.js:44678:5)
at div
at Router (http://localhost:3000/static/js/bundle.js:44609:15)
at BrowserRouter (http://localhost:3000/static/js/bundle.js:42779:5)
at App

AWS Cognito 'Index.handler' Error in React-Redux app

I am trying to learn how to integrate AWS Cognito Auth into a custom React-Redux app. I am running into an issue during the sign-up event with the following error message from cognito:
"message: "CustomMessage failed with error index.handler is undefined or not exported."}"
This error is being generated from my 'signup' action. So I know that react is passing off the form data to redux accurately. The Cognito console also successfully shows a registered user, however the email confirmation the user is supposed to get after the signup process is not sent (I am using real email's to test).
Below are my action and reducers for the signup process. Any suggestions would be helpful.
--FYI I made sure that my Amplify configuration is in the root and that my src directory has an index.js file. I am also using email as the username.
ACTION
// SIGN UP USER
export const signup = ({
firstname,
lastname,
username,
password,
phonenumber,
}) => async (dispatch) => {
try {
const res = await Auth.signUp({
username,
password,
attributes: {
given_name: firstname,
family_name: lastname,
phone_number: phonenumber,
},
});
console.log(res);
dispatch({
type: SIGNUP_SUCCESS,
payload: res,
});
// get if user is signed in
dispatch(loadUser());
} catch (error) {
console.log(error);
dispatch({
type: SIGNUP_FAIL,
});
}
};
REDUCER
case SIGNUP_SUCCESS:
return {
...state,
...payload,
isAuthenticate: true,
};
case AUTH_ERROR:
case LOGOUT:
return {
...state,
isAuthenticated: false,
user: null,
};
REACT FORM
const SignUp = ({ signup, isAuthenticated }) => {
// collect data from form
const [formData, setFormData] = useState({
firstname: '',
lastname: '',
username: '',
password: '',
phonenumber: '',
});
//extract data into formData object
const { firstname, lastname, username, password, phonenumber } = formData;
// collect input data on click
const onChange = (event) =>
setFormData({ ...formData, [event.target.name]: event.target.value });
// call action on submit of form
const onSubmit = async (event) => {
event.preventDefault();
try {
// call signup action
signup({ firstname, lastname, username, password, phonenumber });
//this.props.history.push('/welcome');
return <Redirect to='/welcome' />;
} catch (error) {
console.log(error.message);
}
};
// if user is already signed up/in then return to dashboard
if (isAuthenticated) {
return <Redirect to='/dashboard' />;
}
return (
<Fragment>
<div className='row signin'>
<div className='col-md-3 col-sm-auto col-lg-3'></div>
<div className='col-md-6 col-sm-auto col-lg-6 text-center '>
{/* onSubmit={this.handleSubmit} */}
<form className='form-signin' onSubmit={onSubmit}>
<h1 className='h3 font-weight-normal'>Please sign in</h1>
<label for='inputFirstName' className='sr-only'>
First Name
</label>
<input
type='text'
id='inputFirstName'
name='firstName'
className='form-control'
placeholder='First Name'
required
onChange={onChange}
></input>
<label for='inputLastName' className='sr-only'>
Last Name
</label>
<input
type='text'
id='inputLastName'
name='lastname'
className='form-control'
placeholder='Last Name'
required
onChange={onChange}
></input>
<label for='inputEmail' className='sr-only'>
E-mail
</label>
<input
type='text'
name='username'
id='inputemail'
className='form-control'
placeholder='Email address'
required
autofocus
onChange={onChange}
></input>
<label for='inputPassword' className='sr-only'>
Password
</label>
<input
type='password'
id='inputPassword'
name='password'
className='form-control'
placeholder='Password'
required
onChange={onChange}
></input>
<label for='inputPhoneNumber' className='sr-only'>
Phone Number
</label>
<input
type='text'
id='inputPhoneNumber'
name='phonenumber'
className='form-control'
placeholder='Phone Number'
required
onChange={onChange}
></input>
<button className='btn btn-lg btn-primary btn-block' type='submit'>
Sign up
</button>
</form>
</div>
<div className='col-md-3 col-sm-auto col-lg-3'></div>
</div>
</Fragment>
);
};
SignUp.propTypes = {
signup: propTypes.func.isRequired,
isAuthenticated: propTypes.bool,
};
const mapStateToProps = (state) => ({
isAuthenticated: state.auth.isAuthenticated,
});
export default connect(mapStateToProps, { signup })(SignUp);
That error message sounds like a Lambda function is failing. Do you by chance have Cognito set to trigger a Lambda function with a custom signup message?
It sounds like you have customized the 'send email confirmation' behavior with your own Lambda function, and that function is not written/packaged/deployed correctly.

Redirection to dashboard after login in react

I have a login page, and I want to redirect users to dashboard after the details are filled.
I have tried using history.push and redirect components but I couldn't redirect.
Login Page
class Login extends React.Component {
state = {
email: '',
password: '',
errors: {},
redirect: false
}
validateForm = () => {
let errors = {};
let formIsValid = true;
if(!this.state.email) {
formIsValid = false;
errors['email'] = 'Please enter email to continue';
}
if(!this.state.password) {
formIsValid = false;
errors['password'] = 'Please enter password to continue';
}
this.setState({
errors: errors
})
return formIsValid;
}
handleChange = (event) => {
this.setState({
[event.target.id]: event.target.value
});
}
handleSubmit = (event) => {
event.preventDefault();
// console.log(this.state);
if(this.validateForm()) {
const loginData = {
email: this.state.email,
password: this.state.password
}
axios
.post('/users.json', loginData)
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
})
}
}
render() {
return (
<div className="container">
<form onSubmit={this.handleSubmit} className="white">
<h5 className="grey-text text-darken-3">Login</h5>
<div className="input-field">
<label htmlFor="email">Email</label>
<input type="email" id="email" onChange={this.handleChange} />
<p>{this.state.errors.email}</p>
</div>
<div className="input-field">
<label htmlFor="password">Password</label>
<input type="password" id="password" onChange={this.handleChange} />
<p>{this.state.errors.password}</p>
</div>
<div className="input-field">
<button onClick={this.redirectHandler} className="btn btn-primary">Login</button>
</div>
</form>
</div>
)
}
}
export default Login;
I want to redirect to other page once the form is submitted with the email and password.
I've been trying this for days but I couldn't find a solution.
import { withRouter } from 'react-router';
class Login extends React.Component {
state = {
email: '',
password: '',
errors: {},
redirect: false
}
validateForm = () => {
let errors = {};
let formIsValid = true;
if(!this.state.email) {
formIsValid = false;
errors['email'] = 'Please enter email to continue';
}
if(!this.state.password) {
formIsValid = false;
errors['password'] = 'Please enter password to continue';
}
this.setState({
errors: errors
})
return formIsValid;
}
handleChange = (event) => {
this.setState({
[event.target.id]: event.target.value
});
}
handleSubmit = (event) => {
event.preventDefault();
// console.log(this.state);
if(this.validateForm()) {
const loginData = {
email: this.state.email,
password: this.state.password
}
axios
.post('/users.json', loginData)
.then(response => {
this.props.history.push("/dashboard");
console.log(response.data);
})
.catch(error => {
console.log(error);
})
}
}
render() {
return (
<div className="container">
<form onSubmit={this.handleSubmit} className="white">
<h5 className="grey-text text-darken-3">Login</h5>
<div className="input-field">
<label htmlFor="email">Email</label>
<input type="email" id="email" onChange={this.handleChange} />
<p>{this.state.errors.email}</p>
</div>
<div className="input-field">
<label htmlFor="password">Password</label>
<input type="password" id="password" onChange={this.handleChange} />
<p>{this.state.errors.password}</p>
</div>
<div className="input-field">
<button onClick={this.redirectHandler} className="btn btn-primary">Login</button>
</div>
</form>
</div>
)
}
}
export default withRouter(Login);
have some complain about your code.
first: for form validation and handling you dont need to use state,
there is a library called Formik which will help you a lot with
this.
second: if you are using redux to check user is logged in or
not you need to create a private route for routes which cannot be
accessible for public like here dashboard component.
third: to use history you need to wrap your
component inside withRouter HOC which will pass route props to your
component so you can use history or if your are using functional component you can use useHistory() hook.

How to fix "XML Parsing Error: syntax error" in React JS?

I am trying to setup user login/auth in MERN stack. When I fill the register form and submit I get the following message on console.
XML Parsing Error: syntax error
Location: http://localhost:3000/api/users/register
Line Number 1, Column 1:
I have tested my api using Postman and everything seems to be working fine.
Below is my api for register requuest:
// API routes
// #routes POST api/users/register
// #access Public
// #route POST api/users/register
// #desc Register user
// #access Public
router.post("/register", (req, res) => {
// Form validation
const { errors, isValid } = validateRegisterInput(req.body);
// Check validation
if (!isValid) {
return res.status(400).json(errors);
}
User.findOne({ email: req.body.email }).then(user => {
if (user) {
return res.status(400).json({ email: "Email already exists" });
} else {
const newUser = new User({
name: req.body.name,
email: req.body.email,
password: req.body.password
});
// Hash password before saving in database
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) throw err;
newUser.password = hash;
newUser
.save()
.then(user => res.json(user))
.catch(err => console.log(err));
});
});
}
});
});
On the client side:
Register.jsx
import ...
class Register extends Component {
constructor() {
super();
this.state = {
name: "",
email: "",
password: "",
password2: "",
errors: {}
};
}
componentDidMount() {
// If logged in and user navigates to Register page, should redirect them to dashboard
if (this.props.auth.isAuthenticated) {
this.props.history.push("/dashboard");
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.errors) {
this.setState({
errors: nextProps.errors
});
}
}
onChange = e => {
this.setState({ [e.target.id]: e.target.value });
};
onSubmit = e => {
e.preventDefault();
const newUser = {
name: this.state.name,
email: this.state.email,
password: this.state.password,
password2: this.state.password2
};
this.props.registerUser(newUser, this.props.history);
};
render() {
const { errors } = this.state;
return (
<div className="container">
<div className="row">
<div className="col s8 offset-s2">
<Link to="/" className="btn-flat waves-effect">
<i className="material-icons left">keyboard_backspace</i> Back to
home
</Link>
<div className="col s12" style={{ paddingLeft: "11.250px" }}>
<h4>
<b>Register</b> below
</h4>
<p className="grey-text text-darken-1">
Already have an account? <Link to="/login">Log in</Link>
</p>
</div>
<form noValidate onSubmit={this.onSubmit}>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.name}
error={errors.name}
id="name"
type="text"
className={classnames("", {
invalid: errors.name
})}
/>
<label htmlFor="name">Name</label>
<span className="red-text">{errors.name}</span>
</div>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.email}
error={errors.email}
id="email"
type="email"
className={classnames("", {
invalid: errors.email
})}
/>
<label htmlFor="email">Email</label>
<span className="red-text">{errors.email}</span>
</div>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.password}
error={errors.password}
id="password"
type="password"
className={classnames("", {
invalid: errors.password
})}
/>
<label htmlFor="password">Password</label>
<span className="red-text">{errors.password}</span>
</div>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.password2}
error={errors.password2}
id="password2"
type="password"
className={classnames("", {
invalid: errors.password2
})}
/>
<label htmlFor="password2">Confirm Password</label>
<span className="red-text">{errors.password2}</span>
</div>
<div className="col s12" style={{ paddingLeft: "11.250px" }}>
<button
style={{
width: "150px",
borderRadius: "3px",
letterSpacing: "1.5px",
marginTop: "1rem"
}}
type="submit"
className="btn btn-large waves-effect waves-light hoverable blue accent-3"
>
Sign up
</button>
</div>
</form>
</div>
</div>
</div>
);
}
}
Register.propTypes = {
registerUser: PropTypes.func.isRequired,
auth: PropTypes.object.isRequired,
errors: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
auth: state.auth,
errors: state.errors
});
export default connect(
mapStateToProps,
{ registerUser }
)(withRouter(Register));
I cannot see what is wrong in the code. I have followed a tutorial and done the exact same thing.
My registerUser method is in the following file.
authActions.js
// Register User
export const registerUser = (userData, history) => dispatch => {
axios
.post("/api/users/register", userData)
.then(res => history.push("/login")) // re-direct to login on successful register
.catch(err =>
dispatch({
type: GET_ERRORS,
payload: err.response.data
})
);
};
// Login - get user token
export const loginUser = userData => dispatch => {
axios
.post("/api/users/login", userData)
.then(res => {
// Save to localStorage
// Set token to localStorage
const { token } = res.data;
localStorage.setItem("jwtToken", token);
// Set token to Auth header
setAuthToken(token);
// Decode token to get user data
const decoded = jwt_decode(token);
// Set current user
dispatch(setCurrentUser(decoded));
})
.catch(err =>
dispatch({
type: GET_ERRORS,
payload: err.response.data
})
);
};
// Set logged in user
export const setCurrentUser = decoded => {
return {
type: SET_CURRENT_USER,
payload: decoded
};
};
// User loading
export const setUserLoading = () => {
return {
type: USER_LOADING
};
};
// Log user out
export const logoutUser = () => dispatch => {
// Remove token from local storage
localStorage.removeItem("jwtToken");
// Remove auth header for future requests
setAuthToken(false);
// Set current user to empty object {} which will set isAuthenticated to false
dispatch(setCurrentUser({}));
};

Resources