-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): Added flexible validation checks for User entity email an…
…d password Adds an additional Prisma middleware for validation checks that the user can disable and/or extend Closes #88 and fixes #65
- Loading branch information
shayneczyzewski
committed
Dec 2, 2021
1 parent
bb3964a
commit 81000d5
Showing
7 changed files
with
92 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
waspc/data/Generator/templates/server/src/core/EntityValidationError.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class EntityValidationError extends Error { | ||
constructor (entityName, message, data, ...params) { | ||
super(message, ...params) | ||
|
||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, EntityValidationError) | ||
} | ||
|
||
this.name = this.constructor.name | ||
|
||
if (!entityName) { | ||
throw new Error('entityName has to be a non-empty string.') | ||
} | ||
this.entityName = entityName | ||
|
||
if (data) { | ||
this.data = data | ||
} | ||
} | ||
} | ||
|
||
export default EntityValidationError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 14 additions & 2 deletions
16
waspc/data/Generator/templates/server/src/routes/auth/signup.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
{{={= =}=}} | ||
import Prisma from '@prisma/client' | ||
import prisma from '../../dbClient.js' | ||
import { handleRejection } from '../../utils.js' | ||
import EntityValidationError from '../../core/EntityValidationError.js' | ||
import HttpError from '../../core/HttpError.js' | ||
|
||
export default handleRejection(async (req, res) => { | ||
const userFields = req.body || {} | ||
|
||
await prisma.{= userEntityLower =}.create({ data: userFields }) | ||
try { | ||
await prisma.{= userEntityLower =}.create({ data: userFields }) | ||
res.send() | ||
} catch(e) { | ||
if (e instanceof Prisma.PrismaClientKnownRequestError) { | ||
throw new HttpError(422, 'Save failed', { prisma_error_code: e.code }) | ||
} else if (e instanceof EntityValidationError) { | ||
throw new HttpError(422, 'Validation failed', { entity: e.entityName, message: e.message }) | ||
} | ||
throw new HttpError(500) | ||
} | ||
|
||
res.send() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters