Next.js Discord

Discord Forum

Props in NextAuth (app router)

Answered
`icey tea dev` posted this in #help-forum
Open in Discord
I'm trying to authenticate using JWT's.

1. I have a serverside function that authenticates the user and sends the token to the client side.
2. On the clientside, if I receive a jwt, I route to the next page.

Next time the user does something on the clientside, and needs to be re-routed, or needs to call a serverside function, how do I authenticate them with the JWT.

I assume I need to pass the jwt down as a prop from when I authenticate them. If this is correct, how do I pass down props using the App Router?
If this is not the way to do it, how should I do it? Should I be setting a hearder or something?
  const onSubmit: SubmitHandler<Inputs> = async (data) => {
    const { email, password } = data;
    const request = new Request('/api/login', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ email, password }),
    });

    const response = await fetch(request);
    const responseJson : userData = await response.json();

    if (!response.ok) {
      console.log(response.statusText);
      setErrorMessage(true);
    }

    router.push('/dashboard');
    // route to dashboard
  };
Answered by Ray
It is better to set the token to cookie after the user signin, every time you need that token, you can grab it with cookies().get()
View full answer

21 Replies

Answer
@Ray It is better to set the token to cookie after the user signin, every time you need that token, you can grab it with `cookies().get()`
that means I should respond with NextResponse correct? (I see it lets me access a property called cookie)
@`icey tea dev` that means I should respond with NextResponse correct? (I see it lets me access a property called cookie)
it doesn't matter what you response. When you run cookies().set() it will append the header for you
oh I see. and using that, I can use cookies().get() on the client-side, send that to some middlewhere that checks if the token is valid, and then proceed with the next procedure?
ohhh lol that's right I can
is cookies like a nextjs thing?
not the concept ofcookies
but a function like that
yes
that's awesome
thanks for the help
btw, if you not set the cookies to httponly, you can still access it client side tho
but I would keep the token in httponly cookie
I assume that makes it inaccessible from the clientside. That makes sense since I don't rly need the cookie on the clientside
yes thats good
please mark solution if your issue has be solved😆