Added random-bigint library for verificationCode, store EmailOptIn data in DB.

This commit is contained in:
elweyn 2021-11-04 12:57:28 +01:00
parent d097e5cd76
commit 8bfc538824
4 changed files with 31 additions and 10 deletions

View File

@ -5697,6 +5697,11 @@
"integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==",
"dev": true
},
"random-bigint": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/random-bigint/-/random-bigint-0.0.1.tgz",
"integrity": "sha512-X+NTsf5Hzl/tRNLiNTD3N1LRU0eKdIE0+plNlV1CmXLTlnAxj6HipcTnOhWvFRoSytCz6l1f4KYFf/iH8NNSLw=="
},
"range-parser": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",

View File

@ -28,6 +28,7 @@
"jsonwebtoken": "^8.5.1",
"module-alias": "^2.2.2",
"mysql2": "^2.3.0",
"random-bigint": "^0.0.1",
"reflect-metadata": "^0.1.13",
"sodium-native": "^3.3.0",
"ts-jest": "^27.0.5",

View File

@ -29,6 +29,7 @@ import { LoginUserRepository } from '../../typeorm/repository/LoginUser'
import { LoginUserBackupRepository } from '../../typeorm/repository/LoginUserBackup'
import { LoginUser } from '@entity/LoginUser'
import { LoginUserBackup } from '@entity/LoginUserBackup'
import { LoginEmailOptIn } from '@entity/LoginEmailOptIn'
// TODO apparently the types are cannot be loaded correctly? IDK whats wrong and we have to use require
// import {
@ -43,6 +44,8 @@ import { LoginUserBackup } from '@entity/LoginUserBackup'
// } from 'sodium-native'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const sodium = require('sodium-native')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const random = require('random-bigint')
// We will reuse this for changePassword
const isPassword = (password: string): boolean => {
@ -374,16 +377,23 @@ export class UserResolver {
throw new Error('error saving user')
})
// TODO: send EMail (EMAIL_OPT_IN_REGISTER)
// const emailType = 2
// auto emailOptIn = controller::EmailVerificationCode::create(userModel->getID(), model::table::EMAIL_OPT_IN_REGISTER);
// auto code = createEmailVerificationCode();
// auto db = new model::table::EmailOptIn(code, userModel->getID(), model::table::EMAIL_OPT_IN_REGISTER);
// auto emailOptInModel = emailOptIn->getModel();
// if (!emailOptInModel->insertIntoDB(false)) {
// emailOptInModel->sendErrorsAsEmail();
// return stateError("insert emailOptIn failed");
// }
// Store EmailOptIn in DB
const emailOptIn = new LoginEmailOptIn()
emailOptIn.userId = loginUserId
emailOptIn.verificationCode = random(64) // TODO generate verificationCode
emailOptIn.emailOptInTypeId = 2
await queryRunner.manager.save(emailOptIn).catch((error) => {
// TODO: Send error email instead of throw error
// if (!emailOptInModel->insertIntoDB(false)) {
// emailOptInModel->sendErrorsAsEmail();
// return stateError("insert emailOptIn failed");
// }
// eslint-disable-next-line no-console
console.log('Error while saving emailOptIn', error)
throw new Error('error saving email opt in')
})
// TODO: Send EmailOptIn to user.email
// emailOptIn->setBaseUrl(user->getGroupBaseUrl() + ServerConfig::g_frontend_checkEmailPath);
// em->addEmail(new model::Email(emailOptIn, user, model::Email::convertTypeFromInt(emailType)));
await queryRunner.commitTransaction()

View File

@ -4615,6 +4615,11 @@ queue-microtask@^1.2.2:
resolved "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz"
integrity sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==
random-bigint@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/random-bigint/-/random-bigint-0.0.1.tgz#684de0a93784ab7448a441393916f0e632c95df9"
integrity sha512-X+NTsf5Hzl/tRNLiNTD3N1LRU0eKdIE0+plNlV1CmXLTlnAxj6HipcTnOhWvFRoSytCz6l1f4KYFf/iH8NNSLw==
range-parser@~1.2.1:
version "1.2.1"
resolved "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz"