I have the below code on my dashboard page.jsx
'use client'
import { useSession } from 'next-auth/react'
import { redirect } from 'next/navigation'
import { getUserByEmail } from '@/utils/user'
export default async function DashboardPage() {
const { data: session } = useSession()
if (!(await getUserByEmail(session.user.email))) {
redirect('/onboarding')
} else {
return <div>Welcome to Dashboard</div>
}
}
getUserByEmail.js
code is below.
export async function getUserByEmail(email) {
try {
if ((await getUsers()) === null) {
console.log('No users in the database.')
return null
}
await connectDB()
const user = await User.findOne({ email })
if (!user) {
console.log('User not found by that email.')
return null
}
console.log('User found by that email.')
return user
} catch (error) {
console.log('Failed to fetch user:', error.message)
return null
}
}
The above code doesn’t run. I get a long error when compiling the code saying cannot find various modules. When I remove the 'use client'
, the error goes away. But I have to use 'use client'
because of the useSession
hook.
What would be the best way to do this?
Thank you.