Check if profile.name exist before register post


#1

I’m trying to do a register, but i want it to check if the username is in the database or not.
because when i enter an existing username it will be accepted.

>  createUser : function (credentials) {
>     check(credential, Object)
>     // First we check if the User already exist and if he has PW
>     const USER = Accounts.findUserByEmail(credentials.email)
>     if (USER !== undefined && (USER.services === undefined || USER.services.password === undefined || USER.services.password.bcrypt === undefined)) {
>       Accounts.setPassword(USER._id, credentials.password)
>       result = 'newPassword'
>     } else {
>       // user account creation (via the app)
>       result = Accounts.createUser(credentials) 
>     
>    }
>     return result

May i add something like this ?

USER.profile.name === undefined 

credentials.profile = { name: credentials.username }

there is my schema

let Schemas = {}

Schemas.User = new SimpleSchema({
    createdAt: {
        type: Date
    },

    // Services
    services: {
        type: Object,
        optional: true
    },
    "services.password": {
        type: Object,
        optional: true
    },
    "services.password.bcrypt": {
        type: String,
        optional: true
    },
    "services.resume": {
        type: Object,
        blackbox: true,
        optional: true
    },

    // Emails
    emails: {
        type: Array,
        optional: true
    },
    "emails.$": {
        type: Object
    },
    "emails.$.address": {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    "emails.$.verified": {
        type: Boolean
    },

    // Name
    profile: {
        type: Object,
        optional: true
    },
    "profile.name": {
        // optional:true,
        type: String,
    },
    // Features (Activated or not)
    "profile.features": {
        type: Object,
        optional: true,
        blackbox: true
    },

    pushIds: {
        type: [String],
        optional: true
    },

    // Roles
    roles: {
        type: [String],
        optional: true,
        blackbox: true
    },


})

Meteor.users.attachSchema(Schemas.User)

#2

I’m not sure why all this is necessary, the Accounts package takes care of all this work automatically. Duplicate usernames/emails will not be allowed. If you perform these checks your self you are just running the same code redundantly. Usernames are stored in the username key of the user document if you give it one when you create the account so there’s no need to store it on the profile key as well.

//this can even be called directly from the client
Accounts.createUser({ username, email, password });

#3

i need to set the username on the profile.name and not in username.

That’s why i need to check if it’s unique or not.