Welcome to the SafeInsights management app.
You likely want to visit the OpenStax study proposal page.
diff --git a/src/middleware.ts b/src/middleware.ts
index f1252c8..eed89a3 100644
--- a/src/middleware.ts
+++ b/src/middleware.ts
@@ -13,13 +13,6 @@ export default clerkMiddleware(async (auth, req: NextRequest) => {
try {
const { userId, orgId, orgRole, sessionClaims } = await auth()
- // TODO: Probably remove
- // Require organization selection and prevent personal account usage
- // if (userId && (!orgId || sessionClaims?.org_personal)) {
- // if (!req.nextUrl.pathname.startsWith('/org-selection')) {
- // return NextResponse.redirect(new URL('/org-selection', req.url))
- // }
- // }
// Check if user belongs to SafeInsights organization (admin - highest priority)
const isAdmin = orgId === SAFEINSIGHTS_ORG_ID
From 9b3b76dbf47390fb5bb2555a192dd13b0a30337f Mon Sep 17 00:00:00 2001
From: therealmarv <1050582+therealmarv@users.noreply.github.com>
Date: Wed, 13 Nov 2024 23:28:36 +0200
Subject: [PATCH 14/23] revert changes in main page
---
src/app/page.tsx | 19 ++-----------------
1 file changed, 2 insertions(+), 17 deletions(-)
diff --git a/src/app/page.tsx b/src/app/page.tsx
index e58a49d..92f2f77 100644
--- a/src/app/page.tsx
+++ b/src/app/page.tsx
@@ -1,25 +1,10 @@
-'use client'
-
-import { SignedIn, SignedOut, useUser } from '@clerk/nextjs'
+import { SignedIn, SignedOut } from '@clerk/nextjs'
import { SignIn } from '@/components/signin'
-import { useOrganization } from '@clerk/nextjs'
import { footerStyles, mainStyles, pageStyles } from './page.css'
import { Title, Flex } from '@mantine/core'
import { UserNav } from './user-nav'
-export default function Home() {
- const { user } = useUser()
- const { organization } = useOrganization()
-
- const OPENSTAX_ORG_ID = 'org_2ohzjhfpKp4QqubW86FfXzzDm2I'
-
- const isOrgMember = organization?.id === OPENSTAX_ORG_ID
- const isSiMember = isOrgMember && organization?.membership?.role === 'org:si_member'
- const isAdmin = isOrgMember && organization?.membership?.role === 'org:admin'
-
- console.log('Current organization:', organization)
- console.log(`Active in openstax org: ${isOrgMember ? 'yes' : 'no'}`)
- console.log(`Is si_member: ${isSiMember ? 'yes' : 'no'}`)
+export default async function Home() {
return (
From 92a2221223033bebab1c2623f9d08b3327781a2f Mon Sep 17 00:00:00 2001
From: therealmarv <1050582+therealmarv@users.noreply.github.com>
Date: Wed, 13 Nov 2024 23:36:21 +0200
Subject: [PATCH 15/23] use a simple logger for debug info of middleware
---
src/lib/logger.ts | 24 ++++++++++++++++++++++++
src/middleware.ts | 19 +++++++++++--------
2 files changed, 35 insertions(+), 8 deletions(-)
create mode 100644 src/lib/logger.ts
diff --git a/src/lib/logger.ts b/src/lib/logger.ts
new file mode 100644
index 0000000..87d5153
--- /dev/null
+++ b/src/lib/logger.ts
@@ -0,0 +1,24 @@
+type LogLevel = 'debug' | 'info' | 'warn' | 'error'
+
+const isDevelopment = process.env.NODE_ENV === 'development'
+
+const logger = {
+ debug: (...args: unknown[]) => {
+ if (isDevelopment) {
+ console.warn('[DEBUG]', ...args)
+ }
+ },
+ info: (...args: unknown[]) => {
+ if (isDevelopment) {
+ console.warn('[INFO]', ...args)
+ }
+ },
+ warn: (...args: unknown[]) => {
+ console.warn('[WARN]', ...args)
+ },
+ error: (...args: unknown[]) => {
+ console.error('[ERROR]', ...args)
+ },
+}
+
+export default logger
diff --git a/src/middleware.ts b/src/middleware.ts
index e397b09..ca28a91 100644
--- a/src/middleware.ts
+++ b/src/middleware.ts
@@ -1,5 +1,6 @@
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
import { NextRequest, NextResponse } from 'next/server'
+import logger from '@/lib/logger'
const isMemberRoute = createRouteMatcher(['/member(.*)'])
const isResearcherRoute = createRouteMatcher(['/researcher(.*)'])
@@ -23,11 +24,13 @@ export default clerkMiddleware(async (auth, req: NextRequest) => {
// Define researcher status (users not admin and not in OpenStax)
const isResearcher = !isAdmin && !isOrgMember
- console.log('[Middleware] Active Organization:', orgId)
- console.log('[Middleware] Current Role:', orgRole)
- console.log(`[Middleware] Current User is admin: ${isAdmin ? 'yes' : 'no'}`)
- console.log(`[Middleware] Current User is si_member: ${isMember ? 'yes' : 'no'}`)
- console.log(`[Middleware] Current User is si_researcher: ${isResearcher ? 'yes' : 'no'}`)
+ logger.info('Middleware:', {
+ organization: orgId,
+ role: orgRole,
+ isAdmin,
+ isMember,
+ isResearcher,
+ })
// Handle authentication redirects
if (req.nextUrl.pathname.startsWith('/reset-password') ||
@@ -51,7 +54,7 @@ export default clerkMiddleware(async (auth, req: NextRequest) => {
if (isMemberRoute(req)) {
// Only SI members and admins can access member routes
if (!isMember && !isAdmin) {
- console.log('[Middleware] Access denied: Member route requires SI member or admin access')
+ logger.warn('Access denied: Member route requires SI member or admin access')
return new NextResponse(null, { status: 403 })
}
}
@@ -60,13 +63,13 @@ export default clerkMiddleware(async (auth, req: NextRequest) => {
if (isResearcherRoute(req)) {
// Only researchers and admins can access researcher routes
if (!isResearcher && !isAdmin) {
- console.log('[Middleware] Access denied: Researcher route requires researcher or admin access')
+ logger.warn('Access denied: Researcher route requires researcher or admin access')
return new NextResponse(null, { status: 403 })
}
}
} catch (error) {
- console.error('Middleware error:', error)
+ logger.error('Middleware error:', error)
}
return NextResponse.next()
From 9294290a447fc8157dcec1ebcf2d9e5c1ba653c0 Mon Sep 17 00:00:00 2001
From: therealmarv <1050582+therealmarv@users.noreply.github.com>
Date: Wed, 13 Nov 2024 23:40:56 +0200
Subject: [PATCH 16/23] add Clerk auth() response example as TypeDoc comment
---
src/middleware.ts | 30 +++++++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
diff --git a/src/middleware.ts b/src/middleware.ts
index ca28a91..66748d0 100644
--- a/src/middleware.ts
+++ b/src/middleware.ts
@@ -2,6 +2,34 @@ import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
import { NextRequest, NextResponse } from 'next/server'
import logger from '@/lib/logger'
+/**
+ * Example Clerk auth() response structure:
+ * ```typescript
+ * {
+ * sessionClaims: {
+ * azp: "http://localhost:4000",
+ * exp: 1730995945,
+ * iat: 1730995885,
+ * iss: "https://example.clerk.accounts.dev",
+ * nbf: 1730995875,
+ * org_id: "org_xxxxxxxxxxxxxxxxxxxx",
+ * org_permissions: [],
+ * org_role: "org:admin",
+ * org_slug: "example-org",
+ * sid: "sess_xxxxxxxxxxxxxxxxxxxx",
+ * sub: "user_xxxxxxxxxxxxxxxxxxxx"
+ * },
+ * sessionId: "sess_xxxxxxxxxxxxxxxxxxxx",
+ * userId: "user_xxxxxxxxxxxxxxxxxxxx",
+ * orgId: "org_xxxxxxxxxxxxxxxxxxxx",
+ * orgRole: "org:admin",
+ * orgSlug: "example-org",
+ * orgPermissions: [],
+ * __experimental_factorVerificationAge: null
+ * }
+ * ```
+ */
+
const isMemberRoute = createRouteMatcher(['/member(.*)'])
const isResearcherRoute = createRouteMatcher(['/researcher(.*)'])
const OPENSTAX_ORG_ID = 'org_2ohzjhfpKp4QqubW86FfXzzDm2I'
@@ -12,7 +40,7 @@ const SAFEINSIGHTS_ORG_ID = 'org_2oUWxfZ5UDD2tZVwRmMF8BpD2rD'
export default clerkMiddleware(async (auth, req: NextRequest) => {
try {
- const { userId, orgId, orgRole, sessionClaims } = await auth()
+ const { userId, orgId, orgRole } = await auth()
// Check if user belongs to SafeInsights organization (admin - highest priority)
From ec2716af0778e091fb8868950061c89050f9551f Mon Sep 17 00:00:00 2001
From: therealmarv <1050582+therealmarv@users.noreply.github.com>
Date: Thu, 14 Nov 2024 00:01:52 +0200
Subject: [PATCH 17/23] make everything nice
---
src/middleware.ts | 74 +++++++++++++++++++++++------------------------
1 file changed, 37 insertions(+), 37 deletions(-)
diff --git a/src/middleware.ts b/src/middleware.ts
index 66748d0..def1ad9 100644
--- a/src/middleware.ts
+++ b/src/middleware.ts
@@ -1,4 +1,4 @@
-import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
+import { clerkMiddleware, createRouteMatcher, AuthObject } from '@clerk/nextjs/server'
import { NextRequest, NextResponse } from 'next/server'
import logger from '@/lib/logger'
@@ -38,26 +38,37 @@ const SAFEINSIGHTS_ORG_ID = 'org_2oUWxfZ5UDD2tZVwRmMF8BpD2rD'
// Clerk middleware reference
// https://clerk.com/docs/references/nextjs/clerk-middleware
-export default clerkMiddleware(async (auth, req: NextRequest) => {
+export default clerkMiddleware(async (auth: AuthObject, req: NextRequest) => {
try {
const { userId, orgId, orgRole } = await auth()
+ if (!userId) {
+ // Block unauthenticated access to protected routes
+ if (isMemberRoute(req) || isResearcherRoute(req)) {
+ logger.warn('Access denied: Authentication required')
+ return new NextResponse(null, { status: 403 })
+ }
+ // For non-protected routes, let Clerk handle the redirect
+ return NextResponse.next()
+ }
- // Check if user belongs to SafeInsights organization (admin - highest priority)
- const isAdmin = orgId === SAFEINSIGHTS_ORG_ID
- // Check if user belongs to OpenStax organization (if not admin)
- const isOrgMember = !isAdmin && orgId === OPENSTAX_ORG_ID
- // Check if user is a SafeInsights member (any OpenStax org member, if not admin)
- const isMember = isOrgMember
- // Define researcher status (users not admin and not in OpenStax)
- const isResearcher = !isAdmin && !isOrgMember
+
+ // Define user roles
+ const userRoles = {
+ isAdmin: orgId === SAFEINSIGHTS_ORG_ID,
+ isOpenStaxMember: orgId === OPENSTAX_ORG_ID,
+ get isSafeInsightsMember() {
+ return this.isOpenStaxMember && !this.isAdmin
+ },
+ get isResearcher() {
+ return !this.isAdmin && !this.isOpenStaxMember
+ }
+ }
logger.info('Middleware:', {
organization: orgId,
role: orgRole,
- isAdmin,
- isMember,
- isResearcher,
+ ...userRoles
})
// Handle authentication redirects
@@ -68,32 +79,21 @@ export default clerkMiddleware(async (auth, req: NextRequest) => {
}
}
- // TODO: Activate it for future usage if needed
- // Handle post-login redirects for members and researchers
- // if (userId && isOrgMember && req.nextUrl.pathname === '/') {
- // if (isMember) {
- // return NextResponse.redirect(new URL('/member/openstax/studies/review', req.url))
- // } else {
- // return NextResponse.redirect(new URL('/researcher/study/request/openstax', req.url))
- // }
- // }
-
- // Handle member route protection
- if (isMemberRoute(req)) {
- // Only SI members and admins can access member routes
- if (!isMember && !isAdmin) {
- logger.warn('Access denied: Member route requires SI member or admin access')
- return new NextResponse(null, { status: 403 })
- }
+
+ // Route protection
+ const routeProtection = {
+ member: isMemberRoute(req) && !userRoles.isSafeInsightsMember && !userRoles.isAdmin,
+ researcher: isResearcherRoute(req) && !userRoles.isResearcher && !userRoles.isAdmin
}
- // Handle researcher route protection
- if (isResearcherRoute(req)) {
- // Only researchers and admins can access researcher routes
- if (!isResearcher && !isAdmin) {
- logger.warn('Access denied: Researcher route requires researcher or admin access')
- return new NextResponse(null, { status: 403 })
- }
+ if (routeProtection.member) {
+ logger.warn('Access denied: Member route requires SI member or admin access')
+ return new NextResponse(null, { status: 403 })
+ }
+
+ if (routeProtection.researcher) {
+ logger.warn('Access denied: Researcher route requires researcher or admin access')
+ return new NextResponse(null, { status: 403 })
}
} catch (error) {
From df0095696601fbe397901735f1869c25c8c5f494 Mon Sep 17 00:00:00 2001
From: therealmarv <1050582+therealmarv@users.noreply.github.com>
Date: Thu, 14 Nov 2024 00:16:48 +0200
Subject: [PATCH 18/23] fix type definitons
---
src/lib/logger.ts | 2 --
src/middleware.ts | 4 ++--
2 files changed, 2 insertions(+), 4 deletions(-)
diff --git a/src/lib/logger.ts b/src/lib/logger.ts
index 87d5153..f8098a8 100644
--- a/src/lib/logger.ts
+++ b/src/lib/logger.ts
@@ -1,5 +1,3 @@
-type LogLevel = 'debug' | 'info' | 'warn' | 'error'
-
const isDevelopment = process.env.NODE_ENV === 'development'
const logger = {
diff --git a/src/middleware.ts b/src/middleware.ts
index def1ad9..bcdbdad 100644
--- a/src/middleware.ts
+++ b/src/middleware.ts
@@ -1,4 +1,4 @@
-import { clerkMiddleware, createRouteMatcher, AuthObject } from '@clerk/nextjs/server'
+import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
import { NextRequest, NextResponse } from 'next/server'
import logger from '@/lib/logger'
@@ -38,7 +38,7 @@ const SAFEINSIGHTS_ORG_ID = 'org_2oUWxfZ5UDD2tZVwRmMF8BpD2rD'
// Clerk middleware reference
// https://clerk.com/docs/references/nextjs/clerk-middleware
-export default clerkMiddleware(async (auth: AuthObject, req: NextRequest) => {
+export default clerkMiddleware(async (auth: any, req: NextRequest) => {
try {
const { userId, orgId, orgRole } = await auth()
From 526b80d5f7067138bc547808b85aa94dae30c1fb Mon Sep 17 00:00:00 2001
From: therealmarv <1050582+therealmarv@users.noreply.github.com>
Date: Thu, 14 Nov 2024 00:33:30 +0200
Subject: [PATCH 19/23] prettier
---
src/middleware.ts | 14 +++++---------
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/src/middleware.ts b/src/middleware.ts
index bcdbdad..7fc5957 100644
--- a/src/middleware.ts
+++ b/src/middleware.ts
@@ -52,7 +52,6 @@ export default clerkMiddleware(async (auth: any, req: NextRequest) => {
return NextResponse.next()
}
-
// Define user roles
const userRoles = {
isAdmin: orgId === SAFEINSIGHTS_ORG_ID,
@@ -62,28 +61,26 @@ export default clerkMiddleware(async (auth: any, req: NextRequest) => {
},
get isResearcher() {
return !this.isAdmin && !this.isOpenStaxMember
- }
+ },
}
logger.info('Middleware:', {
organization: orgId,
role: orgRole,
- ...userRoles
+ ...userRoles,
})
// Handle authentication redirects
- if (req.nextUrl.pathname.startsWith('/reset-password') ||
- req.nextUrl.pathname.startsWith('/signup')) {
+ if (req.nextUrl.pathname.startsWith('/reset-password') || req.nextUrl.pathname.startsWith('/signup')) {
if (userId) {
return NextResponse.redirect(new URL('/', req.url))
}
}
-
// Route protection
const routeProtection = {
member: isMemberRoute(req) && !userRoles.isSafeInsightsMember && !userRoles.isAdmin,
- researcher: isResearcherRoute(req) && !userRoles.isResearcher && !userRoles.isAdmin
+ researcher: isResearcherRoute(req) && !userRoles.isResearcher && !userRoles.isAdmin,
}
if (routeProtection.member) {
@@ -95,7 +92,6 @@ export default clerkMiddleware(async (auth: any, req: NextRequest) => {
logger.warn('Access denied: Researcher route requires researcher or admin access')
return new NextResponse(null, { status: 403 })
}
-
} catch (error) {
logger.error('Middleware error:', error)
}
@@ -110,6 +106,6 @@ export const config = {
// Always run for routes below
'/(dl|member|researcher)(.*)',
'/',
- '/(reset-password|signup)'
+ '/(reset-password|signup)',
],
}
From 838b5bd7dc63a21285b76cd603e4b8ad84eae71a Mon Sep 17 00:00:00 2001
From: therealmarv <1050582+therealmarv@users.noreply.github.com>
Date: Thu, 14 Nov 2024 01:02:36 +0200
Subject: [PATCH 20/23] tidy up naming
---
src/middleware.ts | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/middleware.ts b/src/middleware.ts
index 7fc5957..80e7a59 100644
--- a/src/middleware.ts
+++ b/src/middleware.ts
@@ -56,7 +56,7 @@ export default clerkMiddleware(async (auth: any, req: NextRequest) => {
const userRoles = {
isAdmin: orgId === SAFEINSIGHTS_ORG_ID,
isOpenStaxMember: orgId === OPENSTAX_ORG_ID,
- get isSafeInsightsMember() {
+ get isMember() {
return this.isOpenStaxMember && !this.isAdmin
},
get isResearcher() {
@@ -79,12 +79,12 @@ export default clerkMiddleware(async (auth: any, req: NextRequest) => {
// Route protection
const routeProtection = {
- member: isMemberRoute(req) && !userRoles.isSafeInsightsMember && !userRoles.isAdmin,
+ member: isMemberRoute(req) && !userRoles.isMember && !userRoles.isAdmin,
researcher: isResearcherRoute(req) && !userRoles.isResearcher && !userRoles.isAdmin,
}
if (routeProtection.member) {
- logger.warn('Access denied: Member route requires SI member or admin access')
+ logger.warn('Access denied: Member route requires member or admin access')
return new NextResponse(null, { status: 403 })
}
From 996545a129443135766305d66e0d0953e38a5bd8 Mon Sep 17 00:00:00 2001
From: therealmarv <1050582+therealmarv@users.noreply.github.com>
Date: Mon, 18 Nov 2024 23:18:20 +0200
Subject: [PATCH 21/23] use package debug for logging
---
package-lock.json | 19 +++++++++++++++++++
package.json | 2 ++
src/lib/logger.ts | 21 ++++++++++-----------
src/middleware.ts | 8 +++++++-
4 files changed, 38 insertions(+), 12 deletions(-)
diff --git a/package-lock.json b/package-lock.json
index 590dd10..10b0a71 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -34,6 +34,7 @@
"child_process": "^1.0.2",
"clsx": "^2.1.1",
"dayjs": "^1.11.13",
+ "debug": "^4.3.7",
"highlight.js": "^11.10.0",
"jsonwebtoken": "^9.0.2",
"kysely": "^0.27.4",
@@ -54,6 +55,7 @@
"@playwright/test": "^1.47",
"@testing-library/react": "^16.0",
"@testing-library/user-event": "^14.5.2",
+ "@types/debug": "^4.1.12",
"@types/pg": "^8.11.10",
"@types/react": "18.3",
"@typescript-eslint/eslint-plugin": "^8.13.0",
@@ -6356,6 +6358,16 @@
"@types/node": "*"
}
},
+ "node_modules/@types/debug": {
+ "version": "4.1.12",
+ "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.12.tgz",
+ "integrity": "sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@types/ms": "*"
+ }
+ },
"node_modules/@types/estree": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.6.tgz",
@@ -6383,6 +6395,13 @@
"@types/node": "*"
}
},
+ "node_modules/@types/ms": {
+ "version": "0.7.34",
+ "resolved": "https://registry.npmjs.org/@types/ms/-/ms-0.7.34.tgz",
+ "integrity": "sha512-nG96G3Wp6acyAgJqGasjODb+acrI7KltPiRxzHPXnP3NgI28bpQDRv53olbqGXbfcgF5aiiHmO3xpwEpS5Ld9g==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/@types/mysql": {
"version": "2.15.26",
"resolved": "https://registry.npmjs.org/@types/mysql/-/mysql-2.15.26.tgz",
diff --git a/package.json b/package.json
index c75544d..74c4734 100644
--- a/package.json
+++ b/package.json
@@ -29,6 +29,7 @@
"@playwright/test": "^1.47",
"@testing-library/react": "^16.0",
"@testing-library/user-event": "^14.5.2",
+ "@types/debug": "^4.1.12",
"@types/pg": "^8.11.10",
"@types/react": "18.3",
"@typescript-eslint/eslint-plugin": "^8.13.0",
@@ -77,6 +78,7 @@
"child_process": "^1.0.2",
"clsx": "^2.1.1",
"dayjs": "^1.11.13",
+ "debug": "^4.3.7",
"highlight.js": "^11.10.0",
"jsonwebtoken": "^9.0.2",
"kysely": "^0.27.4",
diff --git a/src/lib/logger.ts b/src/lib/logger.ts
index f8098a8..1f07b68 100644
--- a/src/lib/logger.ts
+++ b/src/lib/logger.ts
@@ -1,22 +1,21 @@
-const isDevelopment = process.env.NODE_ENV === 'development'
+import debug from 'debug'
const logger = {
- debug: (...args: unknown[]) => {
- if (isDevelopment) {
- console.warn('[DEBUG]', ...args)
- }
- },
- info: (...args: unknown[]) => {
- if (isDevelopment) {
- console.warn('[INFO]', ...args)
- }
- },
+ debug: debug('app:debug'),
+ info: debug('app:info'),
warn: (...args: unknown[]) => {
+ debug('app:warn')(...args)
console.warn('[WARN]', ...args)
},
error: (...args: unknown[]) => {
+ debug('app:error')(...args)
console.error('[ERROR]', ...args)
},
}
+// Enable debug output in development
+if (process.env.NODE_ENV === 'development') {
+ debug.enable('app:*')
+}
+
export default logger
diff --git a/src/middleware.ts b/src/middleware.ts
index 80e7a59..a9e676a 100644
--- a/src/middleware.ts
+++ b/src/middleware.ts
@@ -1,6 +1,9 @@
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
import { NextRequest, NextResponse } from 'next/server'
import logger from '@/lib/logger'
+import debug from 'debug'
+
+const middlewareDebug = debug('app:middleware')
/**
* Example Clerk auth() response structure:
@@ -46,6 +49,7 @@ export default clerkMiddleware(async (auth: any, req: NextRequest) => {
// Block unauthenticated access to protected routes
if (isMemberRoute(req) || isResearcherRoute(req)) {
logger.warn('Access denied: Authentication required')
+ middlewareDebug('Blocking unauthenticated access to protected route')
return new NextResponse(null, { status: 403 })
}
// For non-protected routes, let Clerk handle the redirect
@@ -64,7 +68,7 @@ export default clerkMiddleware(async (auth: any, req: NextRequest) => {
},
}
- logger.info('Middleware:', {
+ middlewareDebug('Auth check: %o', {
organization: orgId,
role: orgRole,
...userRoles,
@@ -85,11 +89,13 @@ export default clerkMiddleware(async (auth: any, req: NextRequest) => {
if (routeProtection.member) {
logger.warn('Access denied: Member route requires member or admin access')
+ middlewareDebug('Blocking unauthorized member route access: %o', { userId, orgId, userRoles })
return new NextResponse(null, { status: 403 })
}
if (routeProtection.researcher) {
logger.warn('Access denied: Researcher route requires researcher or admin access')
+ middlewareDebug('Blocking unauthorized researcher route access: %o', { userId, orgId, userRoles })
return new NextResponse(null, { status: 403 })
}
} catch (error) {
From 326ce348fdd2eb802af88ff1665436d99fa3887e Mon Sep 17 00:00:00 2001
From: therealmarv <1050582+therealmarv@users.noreply.github.com>
Date: Mon, 18 Nov 2024 23:21:47 +0200
Subject: [PATCH 22/23] replace org IDs with slugs
---
src/middleware.ts | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/middleware.ts b/src/middleware.ts
index a9e676a..76f73e8 100644
--- a/src/middleware.ts
+++ b/src/middleware.ts
@@ -35,15 +35,15 @@ const middlewareDebug = debug('app:middleware')
const isMemberRoute = createRouteMatcher(['/member(.*)'])
const isResearcherRoute = createRouteMatcher(['/researcher(.*)'])
-const OPENSTAX_ORG_ID = 'org_2ohzjhfpKp4QqubW86FfXzzDm2I'
-const SAFEINSIGHTS_ORG_ID = 'org_2oUWxfZ5UDD2tZVwRmMF8BpD2rD'
+const OPENSTAX_ORG_SLUG = 'openstax'
+const SAFEINSIGHTS_ORG_SLUG = 'safe-insights'
// Clerk middleware reference
// https://clerk.com/docs/references/nextjs/clerk-middleware
export default clerkMiddleware(async (auth: any, req: NextRequest) => {
try {
- const { userId, orgId, orgRole } = await auth()
+ const { userId, orgId, orgRole, orgSlug } = await auth()
if (!userId) {
// Block unauthenticated access to protected routes
@@ -58,8 +58,8 @@ export default clerkMiddleware(async (auth: any, req: NextRequest) => {
// Define user roles
const userRoles = {
- isAdmin: orgId === SAFEINSIGHTS_ORG_ID,
- isOpenStaxMember: orgId === OPENSTAX_ORG_ID,
+ isAdmin: orgSlug === SAFEINSIGHTS_ORG_SLUG,
+ isOpenStaxMember: orgSlug === OPENSTAX_ORG_SLUG,
get isMember() {
return this.isOpenStaxMember && !this.isAdmin
},
From 2dbc39f699c2da332b9985036a7f0b70b005c3a9 Mon Sep 17 00:00:00 2001
From: therealmarv <1050582+therealmarv@users.noreply.github.com>
Date: Mon, 18 Nov 2024 23:33:02 +0200
Subject: [PATCH 23/23] simplify logger
---
src/lib/logger.ts | 14 ++++++--------
1 file changed, 6 insertions(+), 8 deletions(-)
diff --git a/src/lib/logger.ts b/src/lib/logger.ts
index 1f07b68..4f99709 100644
--- a/src/lib/logger.ts
+++ b/src/lib/logger.ts
@@ -3,14 +3,8 @@ import debug from 'debug'
const logger = {
debug: debug('app:debug'),
info: debug('app:info'),
- warn: (...args: unknown[]) => {
- debug('app:warn')(...args)
- console.warn('[WARN]', ...args)
- },
- error: (...args: unknown[]) => {
- debug('app:error')(...args)
- console.error('[ERROR]', ...args)
- },
+ warn: debug('app:warn'),
+ error: debug('app:error'),
}
// Enable debug output in development
@@ -18,4 +12,8 @@ if (process.env.NODE_ENV === 'development') {
debug.enable('app:*')
}
+// Forward warnings and errors to console
+logger.warn.log = console.warn.bind(console)
+logger.error.log = console.error.bind(console)
+
export default logger