Next.js Discord

Discord Forum

Next-Auth Invalid URL

Answered
Yellowstripe scad posted this in #help-forum
Open in Discord
Yellowstripe scadOP
const onSubmit = async (values: z.infer<typeof FormSchema>) => {
    const signInData = await signIn("credentials", {
        email: values.email,
        password: values.password,
    });
  };


this is my sign in form module submit. I've narrowed down this error:
⨯ TypeError: Invalid URL
    at parseUrl (C:\Users\rapid\Documents\GitHub\keylib\.next\server\chunks\node_modules_95e03e._.js:596:18)
    at Object.<anonymous> (C:\Users\rapid\Documents\GitHub\keylib\.next\server\chunks\node_modules_95e03e._.js:1436:36)
    at instantiateModule (C:\Users\rapid\Documents\GitHub\keylib\.next\server\chunks\[turbopack]_runtime.js:490:23)
    at getOrInstantiateModuleFromParent (C:\Users\rapid\Documents\GitHub\keylib\.next\server\chunks\[turbopack]_runtime.js:541:12)
    at esmImport (C:\Users\rapid\Documents\GitHub\keylib\.next\server\chunks\[turbopack]_runtime.js:113:20)
    at C:\Users\rapid\Documents\GitHub\keylib\.next\server\chunks\_d2dea6._.js:306:150
    at [project]/components/form/SignInForm.tsx [app-ssr] (ecmascript) (C:\Users\rapid\Documents\GitHub\keylib\.next\server\chunks\_d2dea6._.js:490:3)
    at instantiateModule (C:\Users\rapid\Documents\GitHub\keylib\.next\server\chunks\[turbopack]_runtime.js:490:23)
    at getOrInstantiateModuleFromParent (C:\Users\rapid\Documents\GitHub\keylib\.next\server\chunks\[turbopack]_runtime.js:541:12)
    at commonJsRequire (C:\Users\rapid\Documents\GitHub\keylib\.next\server\chunks\[turbopack]_runtime.js:127:20)


to the signIn() function from next-auth. don't know what the problem is or how to fix it. this is my credentials:
import { NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import { db } from "./db";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { compare } from "bcrypt";

export const authOptions: NextAuthOptions = {
    adapter: PrismaAdapter(db),
    session: {
        strategy: "jwt"
    },
    pages: {
        signIn: "/sign-in",
    },
    providers: [
        CredentialsProvider({
          name: "Credentials",
          credentials: {
            email: { label: "Email", type: "email", placeholder: "john@mail.com" },
            password: { label: "Password", type: "password" }
          },
          async authorize(credentials, req) {
            if(!credentials?.email || !credentials?.password) {
                return null
            }

            const existingUser = await db.user.findUnique({
                where: { email: credentials?.email }
            });
            if(!existingUser) {
                return null
            }
            const passwordMath = await compare(credentials.password, existingUser.password);
            if (!passwordMath) {
                return null
            }

            return {
                id: existingUser.id + "",
                username: existingUser.username,
                email: existingUser.email,
            }
          }
        })
      ]
}
Answered by Yellowstripe scad
answer: try on different device
View full answer

33 Replies

Yellowstripe scadOP
i've been searching for a fix for hours, any help is much appreciated!
Yellowstripe scadOP
@Giant panda
Giant panda
what's your middleware
@Giant panda what's your middleware
Yellowstripe scadOP
i’m not sure what middleware exactly means, i’m new to coding. i’m using prisma and postgres
Giant panda
I mean, did you set up the middleware.ts file?
@Giant panda I mean, did you set up the middleware.ts file?
Yellowstripe scadOP
no
i don’t have one
Giant panda
Can you send a screenshot of your error logs?
@Giant panda Can you send a screenshot of your error logs?
Yellowstripe scadOP
every time i reload localhost/sign-in

i get this error
```⨯ TypeError: Invalid URL
at parseUrl (C:\Users\rapid\Documents\GitHub\keylib.next\server\chunks\node_modules95e03e..js:596:18)
at Object.<anonymous> (C:\Users\rapid\Documents\GitHub\keylib.next\server\chunks\node_modules95e03e..js:1436:36)
at instantiateModule (C:\Users\rapid\Documents\GitHub\keylib.next\server\chunks[turbopack]_runtime.js:490:23)
at getOrInstantiateModuleFromParent (C:\Users\rapid\Documents\GitHub\keylib.next\server\chunks[turbopack]_runtime.js:541:12)
at esmImport (C:\Users\rapid\Documents\GitHub\keylib.next\server\chunks[turbopack]runtime.js:113:20)
at C:\Users\rapid\Documents\GitHub\keylib.next\server\chunks_d2dea6.
.js:306:150
at [project]/components/form/SignInForm.tsx [app-ssr] (ecmascript) (C:\Users\rapid\Documents\GitHub\keylib.next\server\chunks_d2dea6._.js:490:3)
at instantiateModule
i narrowed it down the the signIn() function of next-auth/react
i’m not at my computer rn, sorry
Giant panda
that error log is not giving any proper hint
@Giant panda that error log is not giving any proper hint
Yellowstripe scadOP
one sec opening up vscode
do you want me to live share or add you to the repo
or just screenshots
Yellowstripe scadOP
this is my dir
hmm, I'm running it on a diff computer and I dont seem to be getting the error. I need to add my .env tho
Giant panda
import { NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import { db } from "./db";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { compare } from "bcrypt";

export const authOptions: NextAuthOptions = {
    adapter: PrismaAdapter(db),
    session: {
        strategy: "jwt"
    },
    pages: {
        signIn: "/sign-in",
    },
    providers: [
        CredentialsProvider({
          name: "Credentials",
          credentials: {
            email: { label: "Email", type: "email", placeholder: "john@mail.com" },
            password: { label: "Password", type: "password" }
          },
          async authorize(credentials, req) {
            if(!credentials?.email || !credentials?.password) {
                return null
            }

            const existingUser = await db.user.findUnique({
                where: { email: credentials?.email }
            });
            if(!existingUser) {
                return null
            }
            const passwordMath = await compare(credentials.password, existingUser.password);
            if (!passwordMath) {
                return null
            }

            return {
                id: existingUser.id + "",
                username: existingUser.username,
                email: existingUser.email,
            }
          }
        })
      ]
}
try doing signIn without custom signIn page
@Giant panda so you forgot to add the env variables ?
Yellowstripe scadOP
so on my pc, I have them, but then when I go to /sign-in it says error invalid url in the console
'use client';

import { useForm } from 'react-hook-form';
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from '../ui/form';
import * as z from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { Input } from '../ui/input';
import { Button } from '../ui/button';
import Link from 'next/link';
import { signIn, signOut, useSession } from "next-auth/react"

const FormSchema = z.object({
  email: z.string().min(1, 'Email is required').email('Invalid email'),
  password: z
    .string()
    .min(1, 'Password is required')
    .min(8, 'Password must have than 8 characters'),
});

const SignInForm = () => {
  const form = useForm<z.infer<typeof FormSchema>>({
    resolver: zodResolver(FormSchema),
    defaultValues: {
      email: '',
      password: '',
    },
  });

  const onSubmit = async (values: z.infer<typeof FormSchema>) => {
    console.log()
    const signInData = await signIn("credentials", {
        email: values.email,
        password: values.password,
    });
    console.log(signInData);
  };

  return (
    <Form {...form}>
      <form onSubmit={form.handleSubmit(onSubmit)} className='w-full'>
        <div className='space-y-2'>
          <FormField
            control={form.control}
            name='email'
            render={({ field }) => (
              <FormItem>
                <FormLabel>Email</FormLabel>
                <FormControl>
                  <Input placeholder='mail@example.com' {...field} />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name='password'
            render={({ field }) => (
              <FormItem>
                <FormLabel>Password</FormLabel>
                <FormControl>
                  <Input
                    type='password'
                    placeholder='Enter your password'
                    {...field}
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
        </div>
        <Button className='w-full mt-6' type='submit'>
          Sign in
        </Button>
      </form>
      <div className='mx-auto my-4 flex w-full items-center justify-evenly before:mr-4 before:block before:h-px before:flex-grow before:bg-stone-400 after:ml-4 after:block after:h-px after:flex-grow after:bg-stone-400'>
        or
      </div>
      <p className='text-center text-sm text-gray-600 mt-2'>
        If you don&apos;t have an account, please&nbsp;
        <Link className='text-blue-500 hover:underline' href='/sign-up'>
          Sign up
        </Link>
      </p>
    </Form>
  );
};

export default SignInForm;


thats my signinform
so whenenver I add signIn() to it, like I do in the onsubmit I get the problem
@Giant panda so you forgot to add the env variables ?
Yellowstripe scadOP
I just ran it on a diff computer and it doesnt seem to be getting the problem at all, I think a fix is to uninstall next-auth lib and reinstall
thanks for trying to help!
Giant panda
sorry for the late response. I've just come across something that might help resolve your issue
@Giant panda sorry for the late response. I've just come across something that might help resolve your issue
Yellowstripe scadOP
hey, this is the exact video i watched and followed to build my whole thing, lol.
i figured it out; my pc just had a bad package or something, it all worked out on my laptop and it worked on vercel too
ty for the help
* trying to help
Yellowstripe scadOP
answer: try on different device
Answer