mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Since we decided to open Human Connection for public registration, we don't need to create artificial shortage of signups. The original intention here was too: 1. Grow the network slowly and under control 2. Create an incentive for users to invite their friends Point 2 is now obsolete, everyone can signup herself. Point 1 is based on the (seemingly) wrong assumption that there will be a "run" on the network if we open it for public registration. Our growth was fairly stable so far.
63 lines
2.3 KiB
JavaScript
63 lines
2.3 KiB
JavaScript
import { UserInputError } from 'apollo-server'
|
|
import { neode } from '../../bootstrap/neo4j'
|
|
import fileUpload from './fileUpload'
|
|
import encryptPassword from '../../helpers/encryptPassword'
|
|
import generateNonce from './helpers/generateNonce'
|
|
import existingEmailAddress from './helpers/existingEmailAddress'
|
|
import { normalizeEmail } from 'validator'
|
|
|
|
const instance = neode()
|
|
|
|
export default {
|
|
Mutation: {
|
|
Signup: async (_parent, args, context) => {
|
|
args.nonce = generateNonce()
|
|
args.email = normalizeEmail(args.email)
|
|
let emailAddress = await existingEmailAddress({ args, context })
|
|
if (emailAddress) return emailAddress
|
|
try {
|
|
emailAddress = await instance.create('EmailAddress', args)
|
|
return emailAddress.toJson()
|
|
} catch (e) {
|
|
throw new UserInputError(e.message)
|
|
}
|
|
},
|
|
SignupVerification: async (_parent, args) => {
|
|
const { termsAndConditionsAgreedVersion } = args
|
|
const regEx = new RegExp(/^[0-9]+\.[0-9]+\.[0-9]+$/g)
|
|
if (!regEx.test(termsAndConditionsAgreedVersion)) {
|
|
throw new UserInputError('Invalid version format!')
|
|
}
|
|
args.termsAndConditionsAgreedAt = new Date().toISOString()
|
|
|
|
let { nonce, email } = args
|
|
email = normalizeEmail(email)
|
|
const result = await instance.cypher(
|
|
`
|
|
MATCH(email:EmailAddress {nonce: {nonce}, email: {email}})
|
|
WHERE NOT (email)-[:BELONGS_TO]->()
|
|
RETURN email
|
|
`,
|
|
{ nonce, email },
|
|
)
|
|
const emailAddress = await instance.hydrateFirst(result, 'email', instance.model('Email'))
|
|
if (!emailAddress) throw new UserInputError('Invalid email or nonce')
|
|
args = await fileUpload(args, { file: 'avatarUpload', url: 'avatar' })
|
|
args = await encryptPassword(args)
|
|
try {
|
|
const user = await instance.create('User', args)
|
|
await Promise.all([
|
|
user.relateTo(emailAddress, 'primaryEmail'),
|
|
emailAddress.relateTo(user, 'belongsTo'),
|
|
emailAddress.update({ verifiedAt: new Date().toISOString() }),
|
|
])
|
|
return user.toJson()
|
|
} catch (e) {
|
|
if (e.code === 'Neo.ClientError.Schema.ConstraintValidationFailed')
|
|
throw new UserInputError('User with this slug already exists!')
|
|
throw new UserInputError(e.message)
|
|
}
|
|
},
|
|
},
|
|
}
|