Skip to content

Commit 92513f6

Browse files
committed
Add user search and improve email lookup
Introduces a searchUsers function to find users by email or name with case-insensitive matching. Also updates getProfileByEmail to use ilike for case-insensitive email lookup and trims input whitespace.
1 parent b54257e commit 92513f6

1 file changed

Lines changed: 36 additions & 1 deletion

File tree

lib/database/connection.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,14 @@ export async function getProfileByEmail(email: string) {
300300
}
301301

302302
const supabase = createClient()
303+
304+
// Use ilike for case-insensitive search and trim whitespace
305+
const trimmedEmail = email.trim().toLowerCase()
306+
303307
const { data, error } = await supabase
304308
.from('profiles')
305309
.select('*')
306-
.eq('email', email)
310+
.ilike('email', trimmedEmail)
307311
.single()
308312

309313
if (error) {
@@ -317,6 +321,37 @@ export async function getProfileByEmail(email: string) {
317321
}
318322
}
319323

324+
// Search for users by email or name
325+
export async function searchUsers(query: string, limit = 10) {
326+
try {
327+
if (!process.env.NEXT_PUBLIC_SUPABASE_URL || !process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY) {
328+
console.warn('Supabase not configured')
329+
return []
330+
}
331+
332+
const supabase = createClient()
333+
const trimmedQuery = query.trim()
334+
335+
if (!trimmedQuery) return []
336+
337+
// Search by email or name (case-insensitive)
338+
const { data, error } = await supabase
339+
.from('profiles')
340+
.select('id, email, name, avatar')
341+
.or(`email.ilike.%${trimmedQuery}%,name.ilike.%${trimmedQuery}%`)
342+
.limit(limit)
343+
344+
if (error) {
345+
console.warn('Error searching users:', error)
346+
return []
347+
}
348+
return data || []
349+
} catch (err) {
350+
console.warn('Error searching users:', err)
351+
return []
352+
}
353+
}
354+
320355
// ============================================
321356
// FILES
322357
// ============================================

0 commit comments

Comments
 (0)