Skip to content

Removing whip-data #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/whip-accounts/checkers/accountChecker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
type,
optional,
trimmed,
lowercased,
email,
string,
password
} from '@generates/whip-check'

export default type({
email: optional(trimmed(lowercased(email()))),
username: optional(trimmed(lowercased(string()))),
firstName: optional(trimmed(string())),
lastName: optional(trimmed(string())),
password: optional(password())
})
10 changes: 10 additions & 0 deletions packages/whip-accounts/checkers/emailChecker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {
type,
trimmed,
lowercased,
email
} from '@generates/whip-check'

export default type({
email: trimmed(lowercased(email()))
})
16 changes: 16 additions & 0 deletions packages/whip-accounts/checkers/resetPasswordChecker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
type,
optional,
trimmed,
lowercased,
email,
string,
password
} from '@generates/whip-check'

export default type({
email: trimmed(lowercased(email())),
token: trimmed(string()),
password: password(),
passwordMatch: optional(password())
})
15 changes: 15 additions & 0 deletions packages/whip-accounts/checkers/signInChecker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
type,
optional,
trimmed,
lowercased,
email,
string,
password
} from '@generates/whip-check'

export default type({
email: optional(trimmed(lowercased(email()))),
username: optional(trimmed(lowercased(string()))),
password: password()
})
17 changes: 17 additions & 0 deletions packages/whip-accounts/checkers/signUpChecker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
type,
optional,
trimmed,
lowercased,
email,
string,
password
} from '@generates/whip-check'

export default type({
email: trimmed(lowercased(email())),
username: optional(trimmed(lowercased(string()))),
firstName: optional(trimmed(string())),
lastName: optional(trimmed(string())),
password: password()
})
12 changes: 12 additions & 0 deletions packages/whip-accounts/checkers/verifyEmailChecker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {
type,
trimmed,
lowercased,
email,
string,
} from '@generates/whip-check'

export default type({
email: trimmed(lowercased(email())),
token: trimmed(string())
})
43 changes: 21 additions & 22 deletions packages/whip-accounts/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { merge } from '@generates/merger'
import bcrypt from 'bcrypt'
import { addToResponse } from '@generates/whip'
import { validate } from '@generates/whip-data'
import { check } from '@generates/whip-check'
import prisma from '@generates/whip-prisma'
import email from '@generates/whip-email'
import sessions from '@generates/whip-sessions'
import signUpValidator from './validators/signUpValidator.js'
import accountValidator from './validators/accountValidator.js'
import signInValidator from './validators/signInValidator.js'
import verifyEmailValidator from './validators/verifyEmailValidator.js'
import emailValidator from './validators/emailValidator.js'
import resetPasswordValidator from './validators/resetPasswordValidator.js'
import signUpChecker from './checkers/signUpChecker.js'
import accountChecker from './checkers/accountChecker.js'
import signInChecker from './checkers/signInChecker.js'
import verifyEmailChecker from './checkers/verifyEmailChecker.js'
import emailChecker from './checkers/emailChecker.js'
import resetPasswordChecker from './checkers/resetPasswordChecker.js'
import createToken from './middleware/token/createToken.js'
import insertToken from './middleware/token/insertToken.js'
import createEmailVerificationEmail from './middleware/email/createEmailVerificationEmail.js'
Expand Down Expand Up @@ -56,13 +56,13 @@ export default function accountsPlugin (app, opts = {}) {
app.opts.accounts = merge({}, defaults, opts)

//
app.opts.validators = merge({}, app.opts.validators, {
signUpValidator,
verifyEmailValidator,
signInValidator,
emailValidator,
accountValidator,
resetPasswordValidator
app.opts.checkers = merge({}, app.opts.checkers, {
signUpChecker,
verifyEmailChecker,
signInChecker,
emailChecker,
accountChecker,
resetPasswordChecker
})

//
Expand All @@ -84,15 +84,15 @@ accountsPlugin.sendVerifyEmail = [
]

accountsPlugin.signUp = [
validate({ validator: 'signUpValidator' }),
check('signUpChecker'),
hashPassword,
createAccount,
...accountsPlugin.sendVerifyEmail,
addToResponse
]

accountsPlugin.verifyEmail = [
validate({ validator: 'verifyEmailValidator' }),
check('verifyEmailChecker'),
getToken,
verifyToken,
setEmail,
Expand All @@ -103,8 +103,7 @@ accountsPlugin.verifyEmail = [
]

accountsPlugin.signIn = [
validate({ validator: 'signInValidator' }),
getAccount,
check('signInChecker'),
comparePasswords,
createSession,
reduceAccount,
Expand All @@ -117,7 +116,7 @@ accountsPlugin.signOut = [
]

accountsPlugin.forgotPassword = [
validate({ validator: 'emailValidator' }),
check('emailChecker'),
createToken,
getAccount,
insertToken({ type: 'password' }),
Expand All @@ -127,7 +126,7 @@ accountsPlugin.forgotPassword = [
]

accountsPlugin.resetPassword = [
validate({ validator: 'resetPasswordValidator' }),
check('resetPasswordChecker'),
getToken,
verifyToken,
hashPassword,
Expand All @@ -140,7 +139,7 @@ accountsPlugin.resetPassword = [
accountsPlugin.checkSession = checkSession
accountsPlugin.saveAccount = [
checkSession,
validate({ validator: 'accountValidator' }),
check('accountChecker'),
getAccount,
comparePasswords,
hashPassword,
Expand All @@ -150,7 +149,7 @@ accountsPlugin.saveAccount = [
]

accountsPlugin.resendVerifyEmail = [
validate({ validator: 'emailValidator' }),
check('emailChecker'),
getAccount,
...accountsPlugin.sendVerifyEmail,
addToResponse
Expand Down
2 changes: 1 addition & 1 deletion packages/whip-accounts/middleware/account/createAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { nanoid } from 'nanoid'
export default async function createAccount (req, res, next) {
const logger = req.logger.ns('whip.accounts.account')
const password = req.state.hashedPassword
const data = merge({}, req.state.validation.data, { id: nanoid(), password })
const data = merge({}, req.state.input, { id: nanoid(), password })
logger.debug('signUp.createAccount', data)

try {
Expand Down
6 changes: 3 additions & 3 deletions packages/whip-accounts/middleware/account/getAccount.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default async function getAccount (req, res, next) {
const validation = req.state.validation
const username = req.session.account?.username || validation?.data?.username
const email = req.session.account?.email || validation?.data?.email
const input = req.state.input
const username = req.session.account?.username || input?.username
const email = req.session.account?.email || input?.email
if (username || email) {
const where = {
OR: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const defaults = { path: '/verify-email' }

function handleEmailVerificationEmail (req, res, next, options) {
const url = createUrl(req.opts.baseUrl, options.path)
const input = req.state.validation.data
const input = req.state.input
url.search = { email: input.email, token: req.state.token }
const { verifyEmail } = req.opts.accounts.email
const data = { action: { href: url.href } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const defaults = { path: '/reset-password' }

function handleResetPasswordEmail (req, res, next, options) {
const url = createUrl(req.opts.baseUrl, options.path)
const input = req.state.validation.data
const input = req.state.input
url.search = { email: input.email, token: req.state.token }
const { resetPassword } = req.opts.accounts.email
const data = { action: { href: url.href } }
Expand Down
2 changes: 1 addition & 1 deletion packages/whip-accounts/middleware/email/setEmail.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default async function setEmail (req, res, next) {
const logger = req.logger.ns('whip.accounts.email')
const data = { email: req.state.validation.data.email, emailVerified: true }
const data = { email: req.state.input.email, emailVerified: true }
logger.info('setEmail', { email: data.email })

// Update the email and emailVerified values in the database and session. It's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { BadRequestError } from '@generates/whip'

export default async function comparePasswords (req, res, next) {
const logger = req.logger.ns('whip.accounts.password')
const payload = req.state.validation?.data
if (payload?.password) {
const input = req.state.input
if (input?.password) {
// Determine the password to compare against.
const password = req.state.account
? req.state.account.password
: req.opts.accounts.dummyPassword

// Compare the supplied password with the password hash saved in the
// database to determine if they match.
const passwordsMatch = await bcrypt.compare(payload.password, password)
const passwordsMatch = await bcrypt.compare(input.password, password)

// Log the password and whether the passwords match for debugging purposes.
const debug = { payload, password, passwordsMatch }
const debug = { input, password, passwordsMatch }
logger.debug('password.comparePasswords', debug)

if (!passwordsMatch && req.session?.account) {
Expand Down
4 changes: 2 additions & 2 deletions packages/whip-accounts/middleware/password/hashPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import bcrypt from 'bcrypt'

export default async function hashPassword (req, res, next) {
// Hash the user's password using bcrypt.
const data = req.state.validation?.data
const password = data?.newPassword || data?.password
const input = req.state.input
const password = input?.newPassword || input?.password
if (password) {
const logger = req.logger.ns('whip.accounts.password')
logger.debug('password.hashPassword', { password })
Expand Down
2 changes: 1 addition & 1 deletion packages/whip-accounts/middleware/session/createSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default async function createSession (req, res, next) {
// If the rememberMe functionality is enabled and the user has selected
// rememberMe, set the session cookie maxAge to null so that it won't have
// a set expiry.
if (req.opts.accounts.rememberMe && req.state.validation.data.rememberMe) {
if (req.opts.accounts.rememberMe && req.state.input.rememberMe) {
logger.info('session.createSession • Setting cookie maxAge to null')
req.session.cookie.maxAge = null
}
Expand Down
2 changes: 1 addition & 1 deletion packages/whip-accounts/middleware/token/getToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default async function getToken (req, res, next) {

req.state.account = await req.prisma.account
.findFirst({
where: { email: req.state.validation.data.email },
where: { email: req.state.input.email },
orderBy: { createdAt: 'desc' },
include: { tokens: true }
})
Expand Down
12 changes: 6 additions & 6 deletions packages/whip-accounts/middleware/token/insertToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function insertToken (opts) {
req.state.body.message = `${t} request submitted successfully`
}

const data = req.state.validation.data
const input = req.state.input
const logger = req.logger.ns('nrg.accounts.token')
if (isEmail && !req.state.emailChanged && account && account.emailVerified) {
// Log a warning that someone is trying to create a email verification
Expand All @@ -29,28 +29,28 @@ export default function insertToken (opts) {
logger.warn(
'token.handleInsertToken •',
'Email token request that has already been verified',
data
input
)
} else if (isEmail && !account) {
// Log a warning that someone is trying to create a email verification
// token for an account that doesn't exist.
logger.warn(
'token.handleInsertToken •',
'Email token request that does not match an enabled account',
{ data, accountId: account?.id }
{ input, accountId: account?.id }
)
} else if (opts.type === 'password' && !account) {
// Log a warning that someone is trying to reset a password for an account
// that doesn't exist.
logger.warn(
'token.handleInsertToken •',
'Password token request that does not match an enabled account',
{ data, accountId: account?.id }
{ input, accountId: account?.id }
)
} else {
const debug = {
opts,
data,
input,
accountId: account.id,
hashedToken: req.state.hashedToken
}
Expand All @@ -64,7 +64,7 @@ export default function insertToken (opts) {
id: nanoid(),
value: req.state.hashedToken,
type: opts.type,
email: data.email,
email: input.email,
accountId: account.id,
expiresAt: addDays(new Date(), 1).toISOString()
}
Expand Down
6 changes: 3 additions & 3 deletions packages/whip-accounts/middleware/token/verifyToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export default async function verifyToken (req, res, next) {

// Compare the supplied token value with the returned hashed token
// value.
const payload = req.state.validation.data
const tokensMatch = await bcrypt.compare(payload.token, token.value)
const input = req.state.input
const tokensMatch = await bcrypt.compare(input.token, token.value)

// Determine that the supplied token is valid if the token was found, the
// token values match, and the token is not expired.
Expand All @@ -38,7 +38,7 @@ export default async function verifyToken (req, res, next) {
logger.warn('token.verifyToken • Invalid token', info)
logger.debug(
'token.verifyToken • Tokens',
{ storedToken: token, ...payload }
{ storedToken: token, ...input }
)

// Return a 400 Bad Request if the token is invalid. The user cannot be told
Expand Down
2 changes: 1 addition & 1 deletion packages/whip-accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"dependencies": {
"@generates/extractor": "^1.3.2",
"@generates/merger": "^0.1.3",
"@generates/whip-data": "0.0.3",
"@generates/whip-check": "0.0.0",
"@generates/whip-email": "0.0.2",
"@generates/whip-prisma": "0.0.2",
"@generates/whip-sessions": "0.0.2",
Expand Down
Loading