NeonN
Neon7mo ago
7 replies
precious-lavender

RLS Authorized isn't working with custom JWT Token

I have built an app that using neon as a database along with Drizzle as the orm.

My app used to work fine before but now I get a TLS error:

 [Error: 58550000:error:0A00041A:SSL routines:ssl3_read_bytes:tlsv1 alert decode error:openssl\ssl\record\rec_layer_s3.c:1605:SSL alert number 50
    ]


I have tried multiple approaches to find a clue:

- I ensured drizzle wasn't the issue atleast without JWT. I can use neonclient with drizzle and it works with DATABASE_URL but when I add the token with the client it then gives the TLS error.

This is my code below for reference:

"use server";

import { neon } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
import { auth } from "@/auth";

export interface SessionUserType {
  access_token?: string;
  id: string;
  name: string;
  email: string;
}

export async function db() {
  const session = await auth();

  let authToken: string;
  if (!session) {
    console.warn("No session found; using fallback token");
    authToken = process.env.TEST_ACCESS_TOKEN!;
  } else {
    const user = session.user as SessionUserType;
    authToken = user?.access_token ? `${user.access_token}` : "";
  }

  console.log("Retrieved token:", authToken);
  if (!authToken) {
    throw new Error("No token available");
  }

  const neonClient = neon(process.env.DATABASE_AUTHENTICATED_URL!, {
    authToken: authToken});

  return drizzle(neonClient);
}


- I downgraded Postgres from 17 to 15 to check if that was the issue. but it didn't fix it.

- I then read in the document about the serverless driver from 1.0.0 was changing so I downgraded to 0.10.4 along with drizzle-orm from 0.44.1 to 0.39.1 but to no avail.

- My app is hosted on vercel and thought maybe that was the issue. So I spun up a deno app in cloudflare which didn't work too.

- Finally, I created a new neon project to test out if authenticated irl with token would work and still the issue presists.
Was this page helpful?