Role based access, and lots of style changes, login/signup pages still look funky in dark mode
This commit is contained in:
@@ -113,13 +113,21 @@ export const handler = async (
|
||||
// If this returns anything else, it will be returned by the
|
||||
// `signUp()` function in the form of: `{ message: 'String here' }`.
|
||||
handler: ({ username, hashedPassword, salt, userAttributes }) => {
|
||||
const adminEmails: string[] = process.env.ADMIN_EMAILS.split(',')
|
||||
|
||||
let role = 'user'
|
||||
const email = username.toLowerCase()
|
||||
|
||||
if (adminEmails.includes(email)) role = 'admin'
|
||||
|
||||
return db.user.create({
|
||||
data: {
|
||||
email: username,
|
||||
email: email,
|
||||
hashedPassword: hashedPassword,
|
||||
salt: salt,
|
||||
firstName: userAttributes.firstName,
|
||||
lastName: userAttributes.lastName,
|
||||
roles: role,
|
||||
},
|
||||
})
|
||||
},
|
||||
|
||||
@@ -27,7 +27,7 @@ export const getCurrentUser = async (session: Decoded) => {
|
||||
|
||||
return await db.user.findUnique({
|
||||
where: { id: session.id },
|
||||
select: { id: true, firstName: true },
|
||||
select: { id: true, firstName: true, roles: true },
|
||||
})
|
||||
}
|
||||
|
||||
@@ -59,32 +59,27 @@ export const hasRole = (roles: AllowedRoles): boolean => {
|
||||
return false
|
||||
}
|
||||
|
||||
const currentUserRoles = context.currentUser?.roles
|
||||
|
||||
if (typeof roles === 'string') {
|
||||
if (typeof currentUserRoles === 'string') {
|
||||
// roles to check is a string, currentUser.roles is a string
|
||||
return currentUserRoles === roles
|
||||
} else if (Array.isArray(currentUserRoles)) {
|
||||
// roles to check is a string, currentUser.roles is an array
|
||||
return currentUserRoles?.some((allowedRole) => roles === allowedRole)
|
||||
// If your User model includes roles, uncomment the role checks on currentUser
|
||||
if (roles) {
|
||||
if (Array.isArray(roles)) {
|
||||
// the line below has changed
|
||||
if (context.currentUser.roles)
|
||||
return context.currentUser.roles
|
||||
.split(',')
|
||||
.some((role) => roles.includes(role))
|
||||
}
|
||||
|
||||
if (typeof roles === 'string') {
|
||||
// the line below has changed
|
||||
if (context.currentUser.roles)
|
||||
return context.currentUser.roles.split(',').includes(roles)
|
||||
}
|
||||
|
||||
// roles not found
|
||||
return false
|
||||
}
|
||||
|
||||
if (Array.isArray(roles)) {
|
||||
if (Array.isArray(currentUserRoles)) {
|
||||
// roles to check is an array, currentUser.roles is an array
|
||||
return currentUserRoles?.some((allowedRole) =>
|
||||
roles.includes(allowedRole)
|
||||
)
|
||||
} else if (typeof currentUserRoles === 'string') {
|
||||
// roles to check is an array, currentUser.roles is a string
|
||||
return roles.some((allowedRole) => currentUserRoles === allowedRole)
|
||||
}
|
||||
}
|
||||
|
||||
// roles not found
|
||||
return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { Prisma, Part } from '@prisma/client'
|
||||
|
||||
import type { ScenarioData } from '@redwoodjs/testing/api'
|
||||
|
||||
export const standard = defineScenario<Prisma.PartCreateArgs>({
|
||||
|
||||
@@ -29,18 +29,21 @@ export const updatePart: MutationResolvers['updatePart'] = ({ id, input }) => {
|
||||
export const deletePart: MutationResolvers['deletePart'] = async ({ id }) => {
|
||||
const client = Filestack.init(process.env.REDWOOD_ENV_FILESTACK_API_KEY)
|
||||
const part = await db.part.findUnique({ where: { id } })
|
||||
const handle = part.imageUrl.split('/').pop()
|
||||
|
||||
const security = Filestack.getSecurity(
|
||||
{
|
||||
expiry: new Date().getTime() + 5 * 60 * 1000,
|
||||
handle,
|
||||
call: ['remove'],
|
||||
},
|
||||
process.env.REDWOOD_ENV_FILESTACK_SECRET
|
||||
)
|
||||
if (!part.imageUrl.includes('no_image.png')) {
|
||||
const handle = part.imageUrl.split('/').pop()
|
||||
|
||||
await client.remove(handle, security)
|
||||
const security = Filestack.getSecurity(
|
||||
{
|
||||
expiry: new Date().getTime() + 5 * 60 * 1000,
|
||||
handle,
|
||||
call: ['remove'],
|
||||
},
|
||||
process.env.REDWOOD_ENV_FILESTACK_SECRET
|
||||
)
|
||||
|
||||
await client.remove(handle, security)
|
||||
}
|
||||
|
||||
return db.part.delete({
|
||||
where: { id },
|
||||
|
||||
Reference in New Issue
Block a user