繁体   English   中英

Meteor 账户资料

[英]Meteor Accounts Profile

我希望我的用户拥有一个带有姓名和头像的个人资料。 我无法让它正常工作。 (实际上,我正在继承一些代码)。 我需要有人告诉我如何修改以下代码,以便我可以让我的用户个人资料名称与注册页面中的用户名相同。

很清楚,当创建用户时,可以选择填写作为用户子文档的配置文件。 在我的个人资料中,我想要一个名称和图像。 创建用户后,我们会存储密码和 email 和用户名之类的内容。 我还想存储配置文件,并将配置文件中的名称记录为与用户名相同。 这似乎是不可能的,正如您从我们从 email 中删除配置文件名称的糟糕代码中看到的那样。 请帮助我在创建用户时设置配置文件名称。

这是 onCreateUser 的覆盖:

从 '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');
});

这真的很糟糕。 我不想从 email 中获取用户名。 相反,我想使用登录页面中的用户名。 这是一些创建用户代码:

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;

这是一些 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

这是我的用户个人资料的收藏片段:

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

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

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

这是我的注册页面:

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);

如果您只想让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