input AddUserInput {
firstName: String!
lastName: String!
email: String!
password: String!
confirmPassword: String!
pronoun: String
genderIdentity: String!
birthYear: Int!
birthMonth: Int!
birthDay: Int!
}
type Mutation {
addUser(addUserInput: AddUserInput): Auth!
login(email: String!, password: String!): Auth!
}
resolver
module.exports = {
Mutation: {
// User registration
async addUser(
_,
{
addUserInput: {
firstName,
lastName,
email,
password,
confirmPassword,
pronoun,
genderIdentity,
birthYear,
birthMonth,
birthDay,
},
}
) {
// Validate user data
const { errors, valid } = validateAddUserInput(
firstName,
lastName,
email,
password,
confirmPassword,
pronoun,
genderIdentity,
birthYear,
birthMonth,
birthDay
);
if (!valid) {
console.log('bonk on user registration');
throw new UserInputError('Errors', { errors });
}
// Make sure user doesn't already exist
const user = await User.findOne({ email });
if (user) {
throw new UserInputError('Email address is taken', {
errors: {
email: 'This email address is taken',
},
});
}
// hash the password and create an auth token
const hashedPassword = await bcrypt.hash(password, 12);
// save birthday as ISO 8601 string
const birthday = dayjs(`${birthYear}-${birthMonth}-${birthDay}`).toISOString();
const newUser = new User({
createdAt: new Date().toISOString(),
firstName,
lastName,
email,
password: hashedPassword,
gender: {
pronoun,
genderIdentity,
},
birthday,
});
const res = await newUser.save();
const { _id } = res;
const token = signToken({
email,
firstName,
lastName,
_id,
});
return {
user: res,
id: _id,
token,
};
},
},
}
准代码
import { gql } from '@apollo/client';
export const REGISTER_USER = gql`
mutation AddUser($registerInput: RegisterInput) {
addUser(registerInput: $registerInput) {
token
user {
_id
firstName
lastName
}
}
}
`;
签名组件
// Mutation
const [addUser, { data, loading, error }] = useMutation(REGISTER_USER);
// Registration form submit function
const handleRegistrationFormSubmit = async (event) => {
event.preventDefault();
// Make a copy of the registration form inputs
const registrationFormInputs = { ...registrationForm };
// Format gender inputs for the API
if (registrationForm.customGender === '') {
delete registrationFormInputs.customGender;
} else {
registrationFormInputs.genderIdentity = registrationFormInputs.customGender;
delete registrationFormInputs.customGender;
}
try {
const { data } = await addUser({
variables: { ...registrationFormInputs },
});
// Get login token from response
const registrationToken = data.register.token;
Auth.login(registrationToken);
// Clear the register form
setRegistrationForm({
firstName: '',
lastName: '',
emailAddress: '',
password: '',
confirmPassword: '',
birthDay: dayCurrent,
birthMonth: monthCurrent,
birthYear: yearCurrent,
gender: '',
pronoun: '',
customGender: '',
});
return <Navigate to="/" />;
} catch (e) {
...
}
};
我尝试了什么
const [registrationForm, setRegistrationForm] = useState({
...
email: '',
...
});
const { data } = await addUser({
variables: {
addUserInput: { ...registrationFormInputs },
},
});
希望这对其他人有帮助。