Add alpha numeric regex to username on sign up

resolves #82
This commit is contained in:
Kurt Hutten
2020-11-10 20:49:19 +11:00
parent 14716d1fb0
commit 01f1a02837
4 changed files with 8 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
import { createUserInsecure } from 'src/services/users/users.js' import { createUserInsecure } from 'src/services/users/users.js'
import { db } from 'src/lib/db' import { db } from 'src/lib/db'
import { enforceAlphaNumeric } from 'src/services/helpers'
export const handler = async (req, _context) => { export const handler = async (req, _context) => {
const body = JSON.parse(req.body) const body = JSON.parse(req.body)
@@ -74,7 +75,7 @@ export const handler = async (req, _context) => {
const newSeed = count === 1 ? `${seed}_${count}` : seed.slice(0,-1) + count const newSeed = count === 1 ? `${seed}_${count}` : seed.slice(0,-1) + count
return generateUniqueUserName(newSeed, count) return generateUniqueUserName(newSeed, count)
} }
const userNameSeed = email.split('@')[0] const userNameSeed = enforceAlphaNumeric(email.split('@')[0])
const userName = await generateUniqueUserName(userNameSeed) // TODO maybe come up with a better default userName? const userName = await generateUniqueUserName(userNameSeed) // TODO maybe come up with a better default userName?
const input = { const input = {
email, email,

View File

@@ -11,3 +11,5 @@ export const foreignKeyReplacement = (input) => {
}) })
return output return output
} }
export const enforceAlphaNumeric = (string) => string.replace(/([^a-zA-Z\d_:])/g, '-')

View File

@@ -1,8 +1,7 @@
import { db } from 'src/lib/db' import { db } from 'src/lib/db'
import { foreignKeyReplacement } from 'src/services/helpers' import { foreignKeyReplacement, enforceAlphaNumeric } from 'src/services/helpers'
import { requireAuth } from 'src/lib/auth' import { requireAuth } from 'src/lib/auth'
import { requireOwnership } from 'src/lib/owner' import { requireOwnership } from 'src/lib/owner'
import { user } from 'src/services/users/users'
export const parts = () => { export const parts = () => {
return db.part.findMany() return db.part.findMany()
@@ -40,7 +39,7 @@ export const updatePart = async ({ id, input }) => {
requireAuth() requireAuth()
await requireOwnership({partId: id}) await requireOwnership({partId: id})
if(input.title) { if(input.title) {
input.title = input.title.replace(/([^a-zA-Z\d_:])/g, '-') input.title = enforceAlphaNumeric(input.title)
} }
return db.part.update({ return db.part.update({
data: foreignKeyReplacement(input), data: foreignKeyReplacement(input),

View File

@@ -2,6 +2,7 @@ import { db } from 'src/lib/db'
import { requireAuth } from 'src/lib/auth' import { requireAuth } from 'src/lib/auth'
import { requireOwnership } from 'src/lib/owner' import { requireOwnership } from 'src/lib/owner'
import { UserInputError } from '@redwoodjs/api' import { UserInputError } from '@redwoodjs/api'
import { enforceAlphaNumeric } from 'src/services/helpers'
export const users = () => { export const users = () => {
requireAuth({ role: 'admin' }) requireAuth({ role: 'admin' })
@@ -42,7 +43,7 @@ export const updateUserByUserName = async ({ userName, input }) => {
requireAuth() requireAuth()
await requireOwnership({userName}) await requireOwnership({userName})
if(input.userName) { if(input.userName) {
input.userName = input.userName.replace(/([^a-zA-Z\d_:])/g, '-') input.userName = enforceAlphaNumeric(input.userName)
} }
if(input.userName && ['new', 'edit', 'update'].includes(input.userName)) { //TODO complete this and use a regexp so that it's not case sensitive, don't want someone with the userName eDiT if(input.userName && ['new', 'edit', 'update'].includes(input.userName)) { //TODO complete this and use a regexp so that it's not case sensitive, don't want someone with the userName eDiT
throw new UserInputError(`You've tried to used a protected word as you userName, try something other than `) throw new UserInputError(`You've tried to used a protected word as you userName, try something other than `)