fix: Auto-create users on profile access and make email nullable in types
- Update getUserProfile, getUserReports, getUserEvents, and getUserActivity to call findOrCreateUser for Clerk IDs - This ensures users are automatically created in database when accessing their profile for the first time - Update User type to have email as 'string | null' instead of 'string' - Fixes 404 errors when new users try to access their profile This resolves the issue where new users signing up without email would get 404 errors when accessing their profile page.
This commit is contained in:
0
IMPLEMENTATION_COMPLETE.md
Normal file
0
IMPLEMENTATION_COMPLETE.md
Normal file
@@ -66,6 +66,11 @@ export class UserService {
|
||||
// Check if this is a Clerk ID (starts with 'user_')
|
||||
const isClerkId = userId.startsWith('user_');
|
||||
|
||||
// If it's a Clerk ID, ensure the user exists in our database
|
||||
if (isClerkId) {
|
||||
await this.findOrCreateUser(userId);
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: isClerkId ? { clerkId: userId } : { id: userId },
|
||||
select: {
|
||||
@@ -174,14 +179,8 @@ export class UserService {
|
||||
let dbUserId = userId;
|
||||
|
||||
if (isClerkId) {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { clerkId: userId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!user) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
dbUserId = user.id;
|
||||
// Ensure user exists in database
|
||||
dbUserId = await this.findOrCreateUser(userId);
|
||||
}
|
||||
|
||||
const reports = await prisma.report.findMany({
|
||||
@@ -207,14 +206,8 @@ export class UserService {
|
||||
let dbUserId = userId;
|
||||
|
||||
if (isClerkId) {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { clerkId: userId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!user) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
dbUserId = user.id;
|
||||
// Ensure user exists in database
|
||||
dbUserId = await this.findOrCreateUser(userId);
|
||||
}
|
||||
|
||||
// Get events user has registered for
|
||||
@@ -315,14 +308,8 @@ export class UserService {
|
||||
let dbUserId = userId;
|
||||
|
||||
if (isClerkId) {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { clerkId: userId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!user) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
dbUserId = user.id;
|
||||
// Ensure user exists in database
|
||||
dbUserId = await this.findOrCreateUser(userId);
|
||||
}
|
||||
|
||||
// Get user's reports
|
||||
|
||||
@@ -18,7 +18,7 @@ export interface Coordinates {
|
||||
export interface User {
|
||||
id: string;
|
||||
clerkId: string;
|
||||
email: string;
|
||||
email: string | null;
|
||||
username: string;
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
|
||||
Reference in New Issue
Block a user