Next.js Discord

Discord Forum

async in client component

Answered
Brouillard posted this in #help-forum
Open in Discord
I'm not sure how I am supposed to do here:
I want to check if the connected user has entered all of his infos, but I can't use async function in client component.

"use client"

import React from "react"
import MagicLinkGenerator from "./MagicLinkGenerator"
import { useSession } from "next-auth/react"
import { useEffect } from "react"
import prisma from "@/utils/prismaClient"
import { redirect } from "next/navigation"

const Login = () => {
  const session = useSession()
  useEffect(() => {
    const fetchData = async () => {
      if (session?.data?.user) {
        const me = await prisma.user.findUnique({
          where: {
            email: session.data.user.email!,
          },
        });
        if (!me?.pseudo || !me?.firstName || me?.name) {
          redirect("/informations");
        } else {
          redirect("/");
        }
      }
    };

    fetchData()
  }, [])
  return (
    <div className="max-w-md flex flex-col items-center gap-4">
      <div>Login</div>
      <MagicLinkGenerator />
    </div>
  )
}

export default Login


error :
Error: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding 'use client' to a module that was originally written for the server.

9 Replies

it probably doesn't help that you are trying to use prisma in client component
wait, is prismaClient a proxy? (if it is, that still sounds unsafe)
it is unsafe
don't allow direct access to database from client components
i changed it so now I have an api route for that, but still hopw am I supposed to call the api from a client component ?
well, is there a reason why you can't make it a server component and import client into it (and then still have the redirect on initial load)
because I need the session
Answer
yes that seem great thanks