Code Monkey home page Code Monkey logo

Comments (7)

nickredmark avatar nickredmark commented on June 19, 2024

That is a good point. Your observation is correct. Looks like this would solve the issue: #49.

If you are in a hurry a workaround could be to create an user via http and then modify it via a command to mongodb. Both steps are ugly, I know 😁

from ooth.

gferreri avatar gferreri commented on June 19, 2024

In our scenario, we'd like to build an interface where an admin user can create other users, but we're running into a host of issues, primarily stemming from a lack of a programmatic interface. Also, calls to the local strategy's register endpoint can only be made by a user who is not already logged in, which kills the ability for us to use the register endpoint from the client-side at all.

from ooth.

aaronmgdr avatar aaronmgdr commented on June 19, 2024

I had this same issue. I ended up studying insides of ooth and creating something similar that is compatable. I have removed the application specific parts but here it is

const generator = require('generate-password');
const OothMongo = require('ooth-mongo');
const {ObjectId} = require('mongodb');
const { hashSync, genSaltSync } = require('bcrypt-nodejs');
const { randomBytes } = require('crypto');
const SALT_ROUNDS = 12;

const HOUR = 1000 * 60 * 60;


function randomToken() {
  return randomBytes(43).toString('hex');
}


function hash(pass) {
  return hashSync(pass, genSaltSync(SALT_ROUNDS));
}

class UserImporter {
  backend: {
    getUserByValue: Function,
    insertUser: Function
  }
  
  constructor(db) {
    this.backend = new OothMongo(db, ObjectId);
  }
   /// call this to create a user
  immmigrate({email, firstName, lastName, phone}) {
    if (typeof email !== 'string') {
      throw new Error(`Invalid email ${email}`);
    }
      
    return this.backend.getUserByValue(['local.email'], email).then(user => {
      if (user) {
        console.error(`This email ${email} is already registered.`);
        return
      }
      const password = generate.password()
      const verificationToken = randomToken();
    
      this.backend.insertUser({
        profile:  {
          firstName,
          lastName,
          phone
        },
        local: {
          email,
          password: hash(password),
          verificationToken: hash(verificationToken),
          verificationTokenExpiresAt: new Date(Date.now() + HOUR)
        },
        roles
      }).then(_id => {  console.info(`User ${email} imported successfully.`);
    });
  }
}

export default UserImporter

from ooth.

jonpacker avatar jonpacker commented on June 19, 2024

from ooth.

nickredmark avatar nickredmark commented on June 19, 2024

@jonpacker thanks for the feedback, what approach did you end up taking? Is there any library that was helpful to you?

from ooth.

nickredmark avatar nickredmark commented on June 19, 2024

For the specific case of ooth-local it is now possible to run

ooth.on('register', ({_id, email, verificationToken}) => ...))

await ooth.callMethod('local', 'register', { email, password })

The general idea is that every method registered with ooth.registerMethod can now be called like that.

from ooth.

fractefactos avatar fractefactos commented on June 19, 2024

For the specific case of ooth-local it is now possible to run

ooth.on('register', ({_id, email, verificationToken}) => ...))

await ooth.callMethod('local', 'register', { email, password })

The general idea is that every method registered with ooth.registerMethod can now be called like that.

What's the ooth.on event callback for "login", that exist?

from ooth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.