From f9768f7e088916b03b4fe4f45055c6563c27663d Mon Sep 17 00:00:00 2001 From: Tyler Myracle Date: Sun, 21 Jan 2024 15:36:29 -0600 Subject: [PATCH 1/2] fix dev tools and add admin role --- apps/client/pages/api/auth/[...nextauth].ts | 22 +++++-- apps/client/pages/register.tsx | 28 ++++++++- apps/server/src/app/lib/ability.ts | 8 +-- apps/server/src/app/lib/endpoint.ts | 6 +- apps/server/src/app/routes/admin.router.ts | 5 +- .../src/app/routes/institutions.router.ts | 4 +- .../src/accounts-list/AccountDevTools.tsx | 23 +------ .../accounts-manager/AccountTypeSelector.tsx | 2 +- .../src/services/queue/in-memory-queue.ts | 1 + libs/server/shared/src/utils/auth-utils.ts | 60 ------------------- libs/server/shared/src/utils/index.ts | 1 - libs/shared/src/types/user-types.ts | 4 -- .../migration.sql | 5 ++ prisma/schema.prisma | 7 +++ 14 files changed, 67 insertions(+), 109 deletions(-) delete mode 100644 libs/server/shared/src/utils/auth-utils.ts create mode 100644 prisma/migrations/20240121204146_add_auth_user_role/migration.sql diff --git a/apps/client/pages/api/auth/[...nextauth].ts b/apps/client/pages/api/auth/[...nextauth].ts index a641f0ac..40348a75 100644 --- a/apps/client/pages/api/auth/[...nextauth].ts +++ b/apps/client/pages/api/auth/[...nextauth].ts @@ -2,7 +2,8 @@ import NextAuth from 'next-auth' import type { SessionStrategy, NextAuthOptions } from 'next-auth' import CredentialsProvider from 'next-auth/providers/credentials' import { z } from 'zod' -import { PrismaClient, type Prisma } from '@prisma/client' +import { PrismaClient, AuthUserRole } from '@prisma/client' +import type { Prisma } from '@prisma/client' import { PrismaAdapter } from '@auth/prisma-adapter' import type { SharedType } from '@maybe-finance/shared' import bcrypt from 'bcrypt' @@ -36,6 +37,7 @@ async function validateCredentials(credentials: any): Promise { - const { firstName, lastName, email, password } = credentials + const { firstName, lastName, email, password, isAdmin } = credentials if (!firstName || !lastName) { throw new Error('Both first name and last name are required.') } + const isDevelopment = process.env.NODE_ENV === 'development' const hashedPassword = await bcrypt.hash(password, 10) return createAuthUser({ firstName, @@ -65,6 +69,7 @@ async function createNewAuthUser(credentials: { name: `${firstName} ${lastName}`, email, password: hashedPassword, + role: isAdmin && isDevelopment ? AuthUserRole.admin : AuthUserRole.user, }) } @@ -94,10 +99,15 @@ export const authOptions = { lastName: { label: 'Last name', type: 'text', placeholder: 'Last name' }, email: { label: 'Email', type: 'email', placeholder: 'hello@maybe.co' }, password: { label: 'Password', type: 'password' }, + isAdmin: { label: 'Admin', type: 'checkbox' }, }, async authorize(credentials) { - const { firstName, lastName, email, password } = await validateCredentials( - credentials + // Take credentials and convert the isAdmin string to a boolean + const { firstName, lastName, email, password, isAdmin } = await validateCredentials( + { + ...credentials, + isAdmin: Boolean(credentials?.isAdmin), + } ) const existingUser = await getAuthUserByEmail(email) @@ -114,7 +124,7 @@ export const authOptions = { throw new Error('Invalid credentials provided.') } - return createNewAuthUser({ firstName, lastName, email, password }) + return createNewAuthUser({ firstName, lastName, email, password, isAdmin }) }, }), ], @@ -126,6 +136,7 @@ export const authOptions = { token.firstName = authUser.firstName token.lastName = authUser.lastName token.name = authUser.name + token.role = authUser.role } return token }, @@ -136,6 +147,7 @@ export const authOptions = { session.firstName = token.firstName session.lastName = token.lastName session.name = token.name + session.role = token.role return session }, }, diff --git a/apps/client/pages/register.tsx b/apps/client/pages/register.tsx index 2552a3dd..6a0a64f5 100644 --- a/apps/client/pages/register.tsx +++ b/apps/client/pages/register.tsx @@ -1,5 +1,5 @@ import { useState, type ReactElement } from 'react' -import { Input, InputPassword, Button } from '@maybe-finance/design-system' +import { Input, InputPassword, Button, Checkbox } from '@maybe-finance/design-system' import { FullPageLayout } from '@maybe-finance/client/features' import { signIn, useSession } from 'next-auth/react' import { useRouter } from 'next/router' @@ -15,6 +15,7 @@ export default function RegisterPage() { const [isValid, setIsValid] = useState(false) const [errorMessage, setErrorMessage] = useState(null) const [isLoading, setIsLoading] = useState(false) + const [isAdmin, setIsAdmin] = useState(false) const { data: session } = useSession() const router = useRouter() @@ -38,6 +39,7 @@ export default function RegisterPage() { password, firstName, lastName, + isAdmin, redirect: false, }) @@ -108,6 +110,8 @@ export default function RegisterPage() { ) : null} + +