Guest User

Untitled

a guest
Oct 26th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // @ts-nocheck
  2. // 'PostgresError: relation "session" does not exist
  3. //yeah ok, but the session will be null ? it shouldnt but gives this error either when we tried on the home page, session is null
  4. // 0) Learn next-auth or clerk
  5. // 1) If none of them works, try Clerk (so i'll rebuild my whole system) no actually its working like nextauth, only for auth part.
  6. //okay, i'll look at that, thanks for all sorry, but at least we fixed all errors yeah :D, good night and thanks :) bye then :)
  7. import { DrizzleAdapter } from "@auth/drizzle-adapter"
  8. import {
  9.     type DefaultSession,
  10.     getServerSession,
  11.     type NextAuthOptions,
  12. } from "next-auth";
  13. import DiscordProvider from "next-auth/providers/discord";
  14.  
  15. import { db } from "@/server/db/";
  16. import { users } from "@/server/db/schema";
  17. import { eq } from "drizzle-orm";
  18. import GoogleProvider from "next-auth/providers/google";
  19. import { env } from "@/env.mjs";
  20. import CredentialsProvider from "next-auth/providers/credentials";
  21. import { signInSchema } from "@/schema/authSchema";
  22. import { verify } from "argon2";
  23.  
  24.  
  25. //it can be there n
  26. declare module "next-auth" {
  27.     interface Session extends DefaultSession {
  28.         user: {
  29.             id: string;
  30.             isStaff: boolean
  31.         } & DefaultSession["user"];
  32.     }
  33.  
  34.     interface User {
  35.         // ...other properties
  36.         isStaff: boolean
  37.     }
  38. }
  39. // ! Are you seeing that a lot errors, or just i am
  40. //I'm seeing them, but on webstorm it's not like that, I think the compilator is not the same
  41. // use vscode :D,
  42. // in fact webstorm offer more options
  43. // in errors like user[0]... is because I removed checks, yep. i ll look at that, thats why i said remove checks okay :D
  44. // First, are you using latest version of nextauth  yeah,
  45. // latest version ; 4.24.3
  46. export const authOptions: NextAuthOptions = {
  47.     callbacks: {
  48.         jwt({ token, user }) {
  49.             // user &&
  50.  
  51.             // console.log({ "user": user, "token": token });
  52.             // are you sure about here
  53.             //it worked before lol, we can try to remove token = and see what happen, rabbit holes :D
  54.             // I would say your auth declarations are totally wrong
  55.             //Maybe, I don't use nextauth usually (idk if its english), I'm on AdonisJS, but I tried nextauth lol
  56.             /**
  57.              * Type '({ session, token }: { session: Session; token: JWT; user: AdapterUser; } & { newSession: any; trigger: "update"; }) => Promise<{ name: string | null; surname: string | null; id: string; email: string; delivery: { ...; }; }>' is not assignable to type '(params: { session: Session; token: JWT; user: AdapterUser; } & { newSession: any; trigger: "update"; }) => Awaitable<DefaultSession | Session>'.
  58.              */
  59.             return token;
  60.         },
  61.         // dont think you need a session
  62.         //I think I need, because TRPC needs too with the context (ctx.session.user.id......) wait a sec
  63.         // async session() {
  64.  
  65.         //     const user = await db.select().from(users).where(eq(users.email, token.email))
  66.  
  67.         //     console.log("user2", user)
  68.  
  69.  
  70.         //     // if (!user ) {
  71.         //     //     return null;
  72.         //     // }
  73.  
  74.         //     return {
  75.         //         name: user[0].name,
  76.         //         surname: user[0].surname,
  77.         //         id: user[0].id,
  78.         //         email: user[0].email,
  79.         //         delivery: {
  80.         //             city: user[0].city,
  81.         //             address: user[0].address,
  82.         //             postal_code: user[0].postal_code
  83.         //         },
  84.         //     }
  85.         // },
  86.         async session({ session, token }) {
  87.             const user = await db.select().from(users).where(eq(users.email, token.email!))
  88.             console.log("user2", user)
  89.             session.user.id = token.id as string
  90.  
  91.             //i'll go to sleep (check discord) look at line 1, then you can go for sure
  92.  
  93.             return session
  94.         }
  95.     },
  96.  
  97.     pages: {
  98.         //signOut: '/api/auth/signout',
  99.         newUser: "/api/auth/register"
  100.  
  101.     },
  102.  
  103.     //this code if very like 'if it works, don't touch it' lol (the whole nextauth part)
  104.     // If you use an `adapter` however, we default it to `"database"` instead.
  105.     // relation "session" does not exist PostgresError: relation "session" does not exist
  106.     /**
  107.      * now run app again while studio working, before wait for me.
  108.      * "scripts": {
  109.     "build": "next build",
  110.     "db:push": "dotenv drizzle-kit push:pg", [2]
  111.     "db:generate": "dotenv drizzle-kit generate:pg", [1]
  112.     "db:studio": "dotenv drizzle-kit studio", [3]
  113.     "dev": "next dev", [4]
  114.     "lint": "next lint",
  115.     "start": "next start"
  116.     I should drop the table I think ,do you have seed, seed ? when you run app will it fill DB, no, but the registration process is fast, ok then its best, first kill all terminals
  117.   },
  118.      */
  119.     jwt: {
  120.         // maxAge: 60 * 60 * 24 * 30 * 6,
  121.         maxAge: 60 * 60 * 24 * 30,
  122.     },
  123.     secret: env.NEXTAUTH_SECRET,
  124.  
  125.     // Here gives error ! (I'm following you, yea but gives error :D , but why? even it says docs as is)
  126.     // adapter: DrizzleAdapter(db, pgTable),
  127.     // import { db } from "./schema", but in your schema and at docs, there is nothing like db. Db is in
  128.     /**
  129.      * ?says that: yeah I saw, @[email protected], [email protected] [If you can ignore this error at runtime, its okay.]
  130.      * Like with a ts-ignore ? (yeah ok)
  131.      * Why did you move db to schema ?, at example it was saying, no matter you can revert it tho.
  132.      * Yeah because every component use db, so import are fucked up now ^^
  133.      * Type 'import("/Users/alexisdumain/development/web/shoptongout.fr/node_modules/.pnpm/@[email protected]/node_modules/@auth/core/adapters").Adapter' is not assignable to type 'import("/Users/alexisdumain/development/web/shoptongout.fr/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next-auth/adapters").Adapter'.
  134.         Types of property 'createUser' are incompatible.
  135.         You want me to try ? [fingers crossed , at least there is no error now]
  136.         Do you see console errors ?, can you clear and rerun again. Do you see my terminal? yea
  137.  
  138.         Can we try that ?
  139.         pnpm add drizzle-orm @auth/drizzle-adapter
  140.         pnpm add drizzle-kit --save-dev, havent you Idk, I think so, but it'll update.? I yep pnpm is yours
  141.         f1 > restart TS server, you know what, probably two libs conflicting. Maybe, but how can we know ? remove modules. Okay i'll try
  142.         `rm -rf node_modules, remove pnpm cache, how ? wait i'll search pnpm store prune? I'll try it ?, wait.
  143.         `remove .lock file, remove node_modules, remove cache`I think it's good?, its reading that .lock to install pkgs mhh? I removed it (pnpm-lock) okay above ok ? yeah, now wait.
  144.         we can try b
  145.         y using npm? wait . so ? :D. We have the error, but the cookie is well written in the app
  146.         // could it related with jwt expire
  147.         But, the error indicate that relation "session" does not exist PostgresError: relation "session" does not exist, it's maybe related to schema ? either schema or db, do you know dealing with migrations. A little bit lol
  148. ALTER TABLE "shoptongout_product" ALTER COLUMN "remaining_quantity" DROP DEFAULT;
  149.         can you undo latest migration which makes this  yeah, but I should just remove the drizzle folder, drop the tables, and try again, if you dont have a data to lose
  150.  
  151.         "db:push": "dotenv drizzle-kit push:pg", [2]
  152.     "db:generate": "dotenv drizzle-kit generate:pg", [1] <<
  153.     "db:studio": "dotenv drizzle-kit studio", [3]
  154.     "dev": "next dev", [4]
  155.  
  156.     DONE
  157.  
  158.     mhh, we have the user in console, but when I want to access to my privates page, i get unauthorized, s
  159.     ession token is not stored too
  160.     have you made 1,2,3,4 as follows yeep, can you see your data at studio yeah so DB works, yeah the registration process works, even in the console we can see that it returns the good data, and
  161.     email: '[email protected]',
  162.     password: '$argon2id$v=1'
  163.     its because the session is not stored in database, but the user yes. Do i really need a session ? lol
  164.     ah yes, cuz you provided it in here yeah ok, but the session seems to work ? we get the good data
  165.     SESSION:  null
  166.      */
  167.  
  168.     adapter: DrizzleAdapter(db),
  169.     providers: [
  170.         DiscordProvider({
  171.             clientId: env.DISCORD_CLIENT_ID,
  172.             clientSecret: env.DISCORD_CLIENT_SECRET,
  173.         }),
  174.  
  175.         GoogleProvider({
  176.             clientId: env.GOOGLE_CLIENT_ID,
  177.             clientSecret: env.GOOGLE_CLIENT_SECRET
  178.         }),
  179.         CredentialsProvider({
  180.             name: "credentials",
  181.             credentials: {
  182.                 username: { label: "Username", type: "text", placeholder: "jsmith" },
  183.                 password: { label: "Password", type: "password" }
  184.             },
  185.  
  186.             authorize: async (credentials) => {
  187.                 const creds = await signInSchema.parseAsync(credentials);
  188.  
  189.                 const user = await db.select().from(users).where(eq(users.email, creds.email));
  190.  
  191.  
  192.                 // console.log("user3: ", user[0])
  193.  
  194.  
  195.                 if (user[0]) {
  196.  
  197.                     const isValidPassword = await verify(user[0].password!, creds.password);
  198.                     if (!isValidPassword) {
  199.                         return null;
  200.                     }
  201.  
  202.                     return user[0]
  203.  
  204.                     // return {
  205.                     //     user: {
  206.                     //         name: user[0].name,
  207.                     //         surname: user[0].surname,
  208.                     //         id: user[0].id,
  209.                     //         email: user[0].email,
  210.                     //         isStaff: user[0].isStaff,
  211.  
  212.                     //         delivery: {
  213.                     //             city: user[0].city,
  214.                     //             address: user[0].address,
  215.                     //             postal_code: user[0].postal_code
  216.                     //         },
  217.                     //     }
  218.                     // }
  219.                 }
  220.  
  221.                 return null;
  222.  
  223.  
  224.  
  225.  
  226.             },
  227.             type: "credentials"
  228.         }),
  229.  
  230.     ],
  231. };
  232.  
  233. export const getServerAuthSession = () => getServerSession(authOptions);
  234.  
Add Comment
Please, Sign In to add comment