Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module error & authentication with email #7

Closed
nickcanarelli opened this issue Sep 15, 2020 · 6 comments
Closed

Module error & authentication with email #7

nickcanarelli opened this issue Sep 15, 2020 · 6 comments

Comments

@nickcanarelli
Copy link

nickcanarelli commented Sep 15, 2020

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

Screen Shot 2020-09-15 at 4 01 09 PM

Screen Shot 2020-09-15 at 4 04 03 PM

Maybe its not even related to MongoDB but that's my guess...?

Any ideas?

@flaviuse
Copy link
Owner

flaviuse commented Sep 16, 2020

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
Screen Shot 2020-09-15 at 4 01 09 PM Screen Shot 2020-09-15 at 4 04 03 PM

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.
image
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

@nickcanarelli
Copy link
Author

nickcanarelli commented Sep 16, 2020

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'

@flaviuse
Copy link
Owner

flaviuse commented Sep 16, 2020

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:
    image

  • 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 : Module error & authentication with email #7 (comment)
    image

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.

@nickcanarelli
Copy link
Author

nickcanarelli commented Sep 16, 2020

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?

@nickcanarelli
Copy link
Author

nickcanarelli commented Sep 16, 2020

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) => {
...

@flaviuse
Copy link
Owner

flaviuse commented Sep 16, 2020

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.

@flaviuse flaviuse changed the title Possible to run with MongoDB Atlas? Module error & authentication with email Sep 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants