當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。