简体   繁体   English

Meteor 账户资料

[英]Meteor Accounts Profile

I want my users to have a profile with a name and an avatar.我希望我的用户拥有一个带有姓名和头像的个人资料。 I am not able to get this working properly.我无法让它正常工作。 (in fact, I am inheriting some code). (实际上,我正在继承一些代码)。 I need someone to tell me how to modify the following code so that I can get my user profile name to be the same as the Username from the signup page.我需要有人告诉我如何修改以下代码,以便我可以让我的用户个人资料名称与注册页面中的用户名相同。

To be very clear, when a user is created, there is a option to fill in a profile which is a sub document of user.很清楚,当创建用户时,可以选择填写作为用户子文档的配置文件。 In my profile, I want a name and image.在我的个人资料中,我想要一个名称和图像。 Upon creating a user, we store things like a password and email and usernane.创建用户后,我们会存储密码和 email 和用户名之类的内容。 I also want to store the profile, and record the name in the profile as equal to the username.我还想存储配置文件,并将配置文件中的名称记录为与用户名相同。 This seems impossible, as you can see from the terrible code where we strip the profile name from the email.这似乎是不可能的,正如您从我们从 email 中删除配置文件名称的糟糕代码中看到的那样。 Please help me set the profile name upon user creation.请帮助我在创建用户时设置配置文件名称。

Here is the override of onCreateUser:这是 onCreateUser 的覆盖:

import { Accounts } from 'meteor/accounts-base';从 'meteor/accounts-base' 导入 { Accounts };

Accounts.onCreateUser((options, user) => {
  console.log('\nsign up attempt:', new Date());


  // Handle password signup
  if (user.services.password) {
    const email = user.emails[0].address;
    const name = email.split('@')[0];
    console.log(
      '\nservice --> password',
      '\nname:', name,
      '\nemail:', email,
    );

    // Extend user's profile by adding default name and avatar
    const profile = {
      name,
      avatar: 'user.png',
    };

    return { roles: [], ...user, profile };
  }

  // Handle facebook signup
  if (user.services.facebook) {
    const { id, name, gender, email } = user.services.facebook;
    console.log(
      '\nservice --> facebook',
      '\nname:', name,
      '\nid:', id,
      '\ngender:', gender,
      '\nemail:', email,
    );

    // Extend user's profile by adding facebook data
    const profile = {
      name,
      gender,
      avatar: `http://graph.facebook.com/${id}/picture/`,
    };

    return { roles: [], ...user, profile };
  }

  // Throw in case of a different service
  throw new Error(401, 'Sign up attempt with service different than facebook or password');
});

It is really bad.这真的很糟糕。 I don't want to take the user name from the email.我不想从 email 中获取用户名。 Instead, I want to use the username from the login page.相反,我想使用登录页面中的用户名。 Here is some create user code:这是一些创建用户代码:

import gql from 'graphql-tag';
import Auth from '/app/api/auth';
import { storeLoginToken } from './store';

async function createUser({ username, email, password, profile }, apollo) {
  const result = await apollo.mutate({
    mutation: gql`
      mutation createUser ($username: String, $email: String, $password: HashedPassword!, $profile: CreateUserProfileInput) {
        createUser (username: $username, email: $email, password: $password, profile: $profile) {
          id
          token
          tokenExpires
        }
      }
    `,
    variables: {
      username,
      email,
      password: Auth.hashPassword(password),
      profile,
    },
  });

  const { id, token, tokenExpires } = result.data.createUser;
  await storeLoginToken(id, token, new Date(tokenExpires));
  return id;
}

export default createUser;

Here is some graphql mutation with this weird thing "CreateUserProfileInput" which I don't understand:这是一些 graphql 突变与这个奇怪的东西“CreateUserProfileInput”,我不明白:

import gql from 'graphql-tag';

const types = gql`
  extend input CreateUserInput {
    profile: CreateUserProfileInput!
  }

  input CreateUserProfileInput {
    name: String!
  }


  type Mutation {
    # Create a new user.
    createUser (username: String, email: String, password: HashedPassword, plainPassword: String, profile: CreateUserProfileInput): LoginMethodResponse

Here is my collection snippet with the user profile:这是我的用户个人资料的收藏片段:

  'profile.name': {
    type: String,
    max: 150,
    optional: true,
  },

  'profile.gender': {
    type: String,
    max: 50,
    optional: true,
  },

  'profile.avatar': {
    type: String,
    max: 150,
    optional: true,
  },

Here is my signup page:这是我的注册页面:

import React from 'react';
import { Link } from 'react-router-dom';
import { compose, setDisplayName } from 'recompose';
import { FormattedMessage as T, injectIntl } from 'react-intl';
import { withRouteProps, withFormProps, withServiceProps, withSEO } from '/app/ui/hocs';
import AuthPageLayout from '/app/ui/layouts/auth-page';
import { PasswordAuthViews, FBAuthBtn } from '/app/ui/components/smart/auth';
import Feedback from '/app/ui/components/dumb/feedback';

const SignupPage = ({
  intl: { formatMessage: t },
  disabled,
  errorMsg,
  successMsg,
  handleBefore,
  handleClientError,
  handleServerError,
  service,
  setService,
  loginUrl,
}) => (
  <AuthPageLayout
    title={t({ id: 'signup' })}
    subtitle={t({ id: 'signupSubTitle' })}
    link={<Link to={loginUrl()}><T id="login" /></Link>}
  >
    <PasswordAuthViews
      view="signup"
      btnLabel={t({ id: 'signup' })}
      disabled={disabled}
      onBeforeHook={() => {
        // Keep track of the auth service being used
        setService('password');
        handleBefore();
      }}
      onClientErrorHook={handleClientError}
      onServerErrorHook={handleServerError}
    />
    {service === 'password' && (
      <Feedback
        loading={disabled}
        errorMsg={errorMsg}
        successMsg={successMsg}
      />
    )}
    {/* <div className="center">
      <T id="signupOrText" />
    </div>
    <FBAuthBtn
      btnLabel={t({ id: 'signupFBButton' })}
      disabled={disabled}
      onBeforeHook={() => {
        // Keep track of the auth service being used
        setService('facebook');
        handleBefore();
      }}
      onServerErrorHook={handleServerError}
    />
    {service === 'facebook' && (
      <Feedback
        loading={disabled}
        errorMsg={errorMsg}
        successMsg={successMsg}
      />
    )} */}
  </AuthPageLayout>
);

export default compose(
  injectIntl,
  withRouteProps,
  withFormProps,
  withServiceProps,
  withSEO({ title: 'signup' }),
  setDisplayName('SignupPage'),
)(SignupPage);

If all you want is to have the profile.name property to be the same as the username, you can extract the username property from user in onCreateUser hook and then pass it to the profile.如果您只想让profile.name属性与用户名相同,则可以在onCreateUser钩子中从user中提取username属性,然后将其传递给配置文件。

Accounts.onCreateUser((options, user) => {
  console.log('\nsign up attempt:', new Date());


  // Handle password signup
  if (user.services.password) {
    const email = user.emails[0].address;
    const name = user.username; 
    console.log(
      '\nservice --> password',
      '\nname:', name,
      '\nemail:', email,
    );

    // Extend user's profile by adding default name and avatar
    const profile = {
      name,
      avatar: 'user.png',
    };

    return { roles: [], ...user, profile };
  }
    ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM