Skip to content

Commit add1d14

Browse files
committed
refactor: simplify tenantId handling in authentication routes
- Streamlined tenantId assignment in the login route by directly using the session value, ensuring it is a string or defaulting to an empty string. - Enhanced tenantId validation in the set-tenant route to ensure it is a valid GUID, improving data integrity. - Introduced a new utility function, isGuid, to validate GUID format, promoting code reuse and clarity.
1 parent d897266 commit add1d14

3 files changed

Lines changed: 19 additions & 19 deletions

File tree

src/src/app/auth/login/route.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,8 @@ export async function GET() {
1616
let code_verifier = client.randomPKCECodeVerifier()
1717
let code_challenge = await client.calculatePKCECodeChallenge(code_verifier)
1818
const openIdClientConfig = await getClientConfig()
19-
let tenantId = session.tenantId
20-
21-
// Ensure tenantId is always a string and handle edge cases
22-
if (!tenantId ||
23-
tenantId === 'default' ||
24-
(typeof tenantId === 'object' && Object.keys(tenantId).length === 0) ||
25-
typeof tenantId !== 'string') {
26-
tenantId = ''
27-
} else {
28-
tenantId = String(tenantId)
29-
}
19+
// Just use the saved tenantId (assumed sanitized at the time of saving)
20+
const tenantId = typeof session.tenantId === 'string' ? session.tenantId : ''
3021

3122
let parameters: Record<string, string> = {
3223
redirect_uri: clientConfig.redirect_uri,

src/src/app/auth/set-tenant/route.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getSession } from '@/lib/actions'
33
import { setUpLayoutConfig } from '@/lib/auth'
44
import { headers } from 'next/headers'
55
import { redirect } from 'next/navigation'
6+
import { isGuid } from '@/lib/session-utils'
67

78
/**
89
* Handles the GET request to set the tenant for the current session.
@@ -27,8 +28,9 @@ export async function GET() {
2728
try {
2829
const { data } = await tenantGetTenantGuid({ query: { host: host! } })
2930
console.log('Fetched tenant GUID:', data)
30-
// Ensure tenantId is always a string
31-
session.tenantId = data ? String(data) : ''
31+
const raw = data ? (typeof data === 'string' ? data : String(data)) : ''
32+
const candidate = raw.trim()
33+
session.tenantId = candidate && candidate !== 'default' && isGuid(candidate) ? candidate : ''
3234
} catch (error) {
3335
console.error('Failed to fetch tenant GUID:', error)
3436
session.tenantId = ''

src/src/lib/session-utils.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ export async function getClientConfig() {
4747
return await client.discovery(new URL(clientConfig.url!), clientConfig.client_id!)
4848
}
4949

50+
/**
51+
* Validates whether a string is a GUID (UUID v1-5).
52+
*/
53+
export function isGuid(value: string): boolean {
54+
return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/.test(
55+
value
56+
)
57+
}
58+
5059
/**
5160
* Sets the tenant ID in the session based on the provided host.
5261
* This function updates the session with the tenant ID associated with the given host.
@@ -71,11 +80,9 @@ export async function setTenantWithHost(host: string) {
7180
isObject: typeof data === 'object'
7281
})
7382

74-
// Ensure we store a string, not an object
75-
if (data) {
76-
session.tenantId = typeof data === 'string' ? data : String(data)
77-
} else {
78-
session.tenantId = ''
79-
}
83+
// Ensure we store a GUID or empty string
84+
const raw = data ? (typeof data === 'string' ? data : String(data)) : ''
85+
const candidate = raw.trim()
86+
session.tenantId = candidate && candidate !== 'default' && isGuid(candidate) ? candidate : ''
8087
await session.save()
8188
}

0 commit comments

Comments
 (0)