Closed nickcanarelli closed 4 years ago
I am trying to get this up and running by using MongoDB atlas but I keep running into errors when running
docker-compose up --build
..I assume by adding the following to MONGO_URI in the docker-compose.yml files it would still work, no?
mongodb+srv://admin:<password>!@something.hdb0n.mongodb.net/<dbname>?retryWrites=true&w=majority
Maybe its not even related to MongoDB but that's my guess...?
Any ideas?
Hi, I think I know why you have this error. There is a misspelling in an import on the server side of the mongo sanitize package.
I did fixed it on my local version but forgot to push the fix. I pushed it, run a git pull
command to update your local repository. It should fix it, your error is about not finding this module.
For MongoDB, I do not think it will be problematic, changing the env variable should work perfectly. If it does not work, it is possible that docker is blocking the connection with Atlas. If you encounter a problem I'll check it, it interest me as well.
I will add a Github action to check if the build is correct to avoid this sort of mistakes I made.
Have a nice day
Everything works as expected now, just curious.. how would you go about changing login from username to email?
I've tried the following:
Changing validateLoginInput
in users.js
:
function validateLoginInput(input) {
const schema = Joi.object({
email: Joi.string().min(3).max(255).required().email(),
password: Joi.string().min(5).max(255).required(),
});
return schema.validate(input);
}
changing LoginForm.jsx
:
state = {
data: { email '', password: '' },
errors: {},
};
schema = {
email: Joi.string().label('Email').required(),
password: Joi.string().label('Password').required(),
};
doSubmit = async () => {
const { data } = this.state;
await this.props.attemptLogin(data).catch((error) => {
if (error.response && error.response.status === 400) {
const errors = { ...this.state.errors };
errors.email = error.response.data.message;
this.setState({ errors });
}
});
};
<form onSubmit={this.handleSubmit}>
{this.renderInput('email', 'Email Address', 'text')}
{this.renderInput('password', 'Password', 'password')}
<div>
<Link to="/login/forgot">Forgot your password?</Link>
</div>
{this.renderButton('Login')}
</form>
But keep getting 'Missing Credentials'
Just in case: in the state of your component you have a typo ":" is missing after "email".
Now, if you want to go for email authentication you need a few more changes in addition to what you did already:
On the API endpoint of login (routes/auth.js in the server) you want to lowercase the email address because that is how emails are stored in the DB and what will be passed to passport to find the user:
After that you will need to change how the authentication is done, going from username to email with passportjs by modifying the configuration in the passport-setup.js file: Edit: add this modification as well : https://github.com/flaviuse/mern-authentication/issues/7#issuecomment-693631939
After these 2 3 changes you should be set, I tested it and it worked for me. It was not supposed to
If you want to completely remove usernames, you will have to change more files because it is used both in the client redux store and API routes if I remember correctly. Do a global search of "username" and check the user redux store it should give you an idea.
I'm still getting missing credentials after doing all that you had said to do. I even tried clearing the DB and starting fresh by registering a new user, verifying, then trying to login with email... Not sure what could be holding it up.
The console states: Failed to load resource: the server responded with a status of 400 (Bad Request)
So is it that maybe it's passing data in the incorrect format?
Got it!!! After some troubleshooting, the following worked:
module.exports = function () {
passport.use(
new LocalStrategy(
{ usernameField: 'email', passwordField: 'password' },
(email, password, done) => {
User.findOne({ email }, (err, user) => {
...
Great! I am closing the issue. If you have any question open a new one. I am going to test my version again because it should not have worked considering this : https://stackoverflow.com/a/18142000. Edit: I tested again and got your error, thanks for the fix.
I am trying to get this up and running by using MongoDB atlas but I keep running into errors when running
docker-compose up --build
..I assume by adding the following to MONGO_URI in the docker-compose.yml files it would still work, no?
Maybe its not even related to MongoDB but that's my guess...?
Any ideas?