当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Accounts.onCreateUser方法代码示例

本文整理汇总了TypeScript中meteor/accounts-base.Accounts.onCreateUser方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Accounts.onCreateUser方法的具体用法?TypeScript Accounts.onCreateUser怎么用?TypeScript Accounts.onCreateUser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在meteor/accounts-base.Accounts的用法示例。


在下文中一共展示了Accounts.onCreateUser方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: if

Accounts.onCreateUser((options, user) => {
  // Grab the default profile object if it exists.
  const profile = (options.profile !== undefined) ? options.profile : user.profile;
  let service;
  let address;

  if (user.services) {
    for (const serviceName in user.services) {
      if (Object.hasOwnProperty.call(user.services, serviceName)) {
        service = user.services[serviceName];
        address = (service.email) ? service.email : false;
        if (!address) {
          const emails = (serviceName === 'password') ? user.emails : service.emails;
          for (let i = 0; i < emails.length; i++) {
            const email = emails[i];
            if (email.primary) {
              address = email.address;
            }
          }
        }

        if (service.username && !profile.name) {
          profile.name = service.username;
        }
        // Create or merge the profiles if needed.
        if (!user.profile && profile) {
          user.profile = profile;
        } else if (profile) {
          user.profile = Object.assign(profile, user.profile);
        }

        if (address) {
          const _emails = user.emails || [];
          _emails.push({ address });
          user.emails = _emails;
        } else {
          return user;
        }
      }
    }

    // BEWARE!!! Hackery below
    // See if any existing user has this email address, otherwise create new
    // From http://meteorpedia.com/read/Merging_OAuth_accounts
    const existingUser = Meteor.users.findOne({ 'emails.address': address });
    if (!existingUser) {
      return user;
    }

    // Precaution: these will exist from accounts-password if used
    if (!existingUser.services) {
      existingUser.services = {
        resume: {
          loginTokens: [],
        },
      };
    }
    if (!existingUser.services.resume) {
      existingUser.services.resume = {
        loginTokens: [],
      };
    }

    // Copy accross new service info
    existingUser.services[service] = user.services[service];
    if (user.services.resume) {
      if (user.services.resume.loginTokens) {
        existingUser.services.resume.loginTokens.push(
          user.services.resume.loginTokens[0]
        );
      }
    }

    // Create emails if needed.
    if (!existingUser.emails && user.emails) {
      existingUser.emails = user.emails;
    }

    // Create or merge the profiles if needed.
    if (!existingUser.profile && user.profile) {
      existingUser.profile = user.profile;
    } else if (user.profile) {
      existingUser.profile = Object.assign(user.profile, existingUser.profile);
    }

    // Even worse hackery
    Meteor.users.remove({ _id: existingUser._id }); // Remove the existing record
    return existingUser;              // Record is re-inserted
  }
  return user;
});
开发者ID:andyp22,项目名称:my_resume,代码行数:91,代码来源:accounts.ts

示例2: function

    if (user.username && user.username.length >= 3)
        return true;
    throw new Meteor.Error("403", "Username must have at least 3 characters");
});
// Validate username, without a specific error message.
Accounts.validateNewUser(function (user: { username: string }) {
    return user.username !== "root";
});

/**
 * From Accounts, Accounts.onCreateUser section
 */
Accounts.onCreateUser(function (options: { profile: any }, user: { profile: any, dexterity: number }) {
    var d6 = function () { return Math.floor(Math.random() * 6) + 1; };
    user.dexterity = d6() + d6() + d6();
    // We still want the default hook's 'profile' behavior.
    if (options.profile)
        user.profile = options.profile;
    return user;
});

/**
 * From Passwords, Accounts.emailTemplates section
 */
Accounts.emailTemplates.siteName = "AwesomeSite";
Accounts.emailTemplates.from = "AwesomeSite Admin <accounts@example.com>";
Accounts.emailTemplates.enrollAccount.subject = function (user: { profile: { name: string } }) {
    return "Welcome to Awesome Town, " + user.profile.name;
};
Accounts.emailTemplates.enrollAccount.text = function (user: any, url: string) {
    return "You have been selected to participate in building a better future!"
        + " To activate your account, simply click the link below:\n\n"
开发者ID:Jeremy-F,项目名称:DefinitelyTyped,代码行数:32,代码来源:meteor-tests.ts

示例3: extend

Accounts.onCreateUser((options, user) => {
    let currentService, loginEmail;

    user.profile = extend({}, user.profile, options.profile);

    if (user.services) {
        currentService = first(keys(user.services));
        loginEmail = user.services[currentService].email;
    }

    const originalUser = Meteor.users.findOne({'verified_emails': loginEmail});

    if (!originalUser) {
        return user;
    }

    // remove old account
    try {
        Meteor.users.remove(originalUser._id);
    } catch (e) {
        throw new Meteor.Error(500, e.toString());
    }


    // add the services to the new account
    for (let service of REGISTERED_SERVICES) {
        if (originalUser.services[service]) {
            user.services[service] = originalUser.services[service];
        }
    }

    // update posts
    Posts.update({ author: originalUser._id }, { $set: { author: user._id }}, { multi: true });
    user.profile = extend({}, originalUser.profile, user.profile, options.profile);

    return user;
});
开发者ID:kucharskimaciej,项目名称:bullet-journal,代码行数:37,代码来源:on_create_user.ts


注:本文中的meteor/accounts-base.Accounts.onCreateUser方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。