Back

Update MagicURL Failing ( Next JS V13 | Not Setting Session )

  • 0
  • Web
Vexoa
6 May, 2023, 17:03

Image 1: https://i.imgur.com/Mxr5aiU.png

Code:

TypeScript
import { NextResponse } from "next/server"
import client, { Account, ID } from "@/lib/appwrite/appwrite"

export async function GET(request: Request) {
  // Parse the URL and get the query parameters
  const url = new URL(request.url)
  const userId = url.searchParams.get("userId")
  const secret = url.searchParams.get("secret")

  if (!userId || !secret) {
    return NextResponse.redirect(process.env.NEXT_PUBLIC_APP_URL || "", 302)
  }

  try {
    const updateURL = Account.updateMagicURLSession(userId, secret)
    updateURL.then(
      function (response) {
        console.log(response)
        return NextResponse.redirect(
          process.env.NEXT_PUBLIC_APP_URL + "/dashboard",
          302
        )
      },
      function (error) {
        console.log(error)
        throw new Error(error.message)
      }
    )
    // return NextResponse.redirect(
    //   process.env.NEXT_PUBLIC_APP_URL + "/dashboard",
    //   302
    // )
  } catch (error) {
    // console.error(error)
    return NextResponse.redirect(process.env.NEXT_PUBLIC_APP_URL || "", 302)
  }

  // return NextResponse.redirect(
  //   process.env.NEXT_PUBLIC_APP_URL + "/dashboard" || "",
  //   302
  // )
}```
TL;DR
The user is experiencing an issue with updating MagicURL in NextJS v13. They are building their frontend with Next.js 13 and have set it up as static generated files. However, they are facing difficulties with server-side rendering and the persistence of the session cookie. They are unsure of how to implement Magic URLs. Another user suggests using an API route that is executed server-side. The user shares a code snippet for a GET request, but it is unclear if the code is being executed server-side. The error message includes a screenshot and the code for reference. Solution: - Disable server-side rendering if not necessary as it can comp
Drake
6 May, 2023, 17:29

What's the error you're getting? Is this code being executed server side?

Vexoa
6 May, 2023, 17:41

clientside

Drake
6 May, 2023, 17:43

Wait...I see request and next response...this looks like it's executed server side 🧐

Vexoa
6 May, 2023, 18:07

its a GET request, i redirect from the login button to the api route and it redirects them back to the dash

Drake
6 May, 2023, 18:12

API route...that's executed server side, isn't it?

Vexoa
6 May, 2023, 18:25

I'm not sure 😂

Drake
6 May, 2023, 18:26

This is the really confusing thing about using frameworks with server side rendering. with server side rendering you have:

Client (browser) <-> NextJS Server-side <-> Appwrite

If you're creating a session at NextJS Server-side, it doesn't get persisted or attached to the user and just gets lost

Vexoa
6 May, 2023, 18:27

So how would i do Magic URLs?

Vexoa
6 May, 2023, 18:28

I did the first part, i just dont get the confirmation side of things

Drake
6 May, 2023, 18:30

You would need to make sure the code is executed client side.

I highly suggest turning off all server-side rendering if you don't really need it. it complicates things a lot. with server-side rendering enabled magic url and oauth2 may not work because the cookie is set client side and the server-side nextjs code doesn't have access to it.

Vexoa
6 May, 2023, 18:31

Sure ill try that 😂

jSnake🐍🕊
9 May, 2023, 09:25

im using next.js 13 for my frontend

TypeScript
/** @type {import('next').NextConfig} */
const nextConfig = {
    reactStrictMode: true,
    output: 'export',
    distDir: 'dist',
    images: {
        unoptimized: true,
    },
}

module.exports = nextConfig

I'm building it as static generated files that any web server can serve That ensures things are client side, if you use the /api routes, then it is definitely server side

jSnake🐍🕊
9 May, 2023, 09:26

I haven't gotten deep into their new app directory stuff yet though, I think that is different

jSnake🐍🕊
9 May, 2023, 09:29

you could also pass through the headers to the appwrite api as well and still do things server side

Vexoa
13 May, 2023, 17:29

@jSnake🐍🕊 I appreciate that, still trying to figure out the new Next JS v13

Vexoa
13 May, 2023, 17:29

I want to create a template so i can copy paste modules into my projects

Vexoa
13 May, 2023, 17:29

and for instance

Vexoa
13 May, 2023, 17:29

instantly link login/register stuff

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more