thejessewinton
thejessewinton
Explore posts from servers
TTCTheo's Typesafe Cult
Created by Steven-sensei on 8/6/2023 in #questions
Why keyof Record wont actually give the keys of the record ?
It's because of type C = Record<string,object>. When you're using Record<string, object>, the key is essentially being read as "any string", and it doesn't know how to evaluate it beyond that, since it doesn't know the shape of the object in your validate function. Someone else might have a more sophisticated solution, but here's an easy way around it:
const objectToHaveKeysFrom = {
test: {},
}

type C = Record<keyof typeof objectToHaveKeysFrom, object>

type GlobalStuff = {
description : C,
validate : (descriptionKeys: Record<keyof C, | string> | undefined) => Boolean
}

const test: GlobalStuff = {
description : {
...objectToHaveKeysFrom
},
validate : (descriptionKeys) => {
//no auto completed
descriptionKeys?.test
return true
}
}
const objectToHaveKeysFrom = {
test: {},
}

type C = Record<keyof typeof objectToHaveKeysFrom, object>

type GlobalStuff = {
description : C,
validate : (descriptionKeys: Record<keyof C, | string> | undefined) => Boolean
}

const test: GlobalStuff = {
description : {
...objectToHaveKeysFrom
},
validate : (descriptionKeys) => {
//no auto completed
descriptionKeys?.test
return true
}
}
You define the object explicitly outside of test, which is that objectToHaveKeysFrom, then you extract those keys into type C, and pass the object into test.
17 replies
TTCTheo's Typesafe Cult
Created by Jim on 3/19/2023 in #questions
How do headless-ui expose context variable for child components?
@.3819 I'm running into a similar error here, did you figure anything out?
20 replies
TTCTheo's Typesafe Cult
Created by Scotty on 4/11/2023 in #questions
trpc call that doesn't use prisma
Same core concept; create your endpoint, do the api call, return your data!
getOpenAI: publicProcedure
.input(z.object({/* your schema */}))
.query(async ({ ctx, input }) => {
const res = await fetch(/* OpenAI Endpoint */)
return await data.json()
}),
getOpenAI: publicProcedure
.input(z.object({/* your schema */}))
.query(async ({ ctx, input }) => {
const res = await fetch(/* OpenAI Endpoint */)
return await data.json()
}),
For more type-safety, if you know the shape of the data you’re getting you should run it through zod.safeParse and return that instead. Typing from my phone, but this should get you started.
2 replies
TTCTheo's Typesafe Cult
Created by Yatochka on 2/2/2023 in #questions
Next-Auth default middleware redirect to signin page even when logged in.
For others that might come through the thread, this would seem like an easy, effective solution too, based off of this SO thread. https://stackoverflow.com/questions/70325619/how-to-create-hoc-for-auth-in-next-js Create a function called withAuth or something similar that looks like:
export const withAuth = async (gssp: GetServerSideProps) => {
return async (ctx: GetServerSidePropsContext) => {
const session = await getServerAuthSession(ctx);

if (!session) {
return {
redirect: {
destination: "/sign-in",
permanent: false,
},
};
}

const gsspData = await gssp(ctx);

return {
props: {
...gsspData,
session,
},
};
};
};
export const withAuth = async (gssp: GetServerSideProps) => {
return async (ctx: GetServerSidePropsContext) => {
const session = await getServerAuthSession(ctx);

if (!session) {
return {
redirect: {
destination: "/sign-in",
permanent: false,
},
};
}

const gsspData = await gssp(ctx);

return {
props: {
...gsspData,
session,
},
};
};
};
Then, to use it, just use it in your getServerSideProps functions across pages:
export const getServerSideProps = withAuth(context => {
return { props: {} };
});
export const getServerSideProps = withAuth(context => {
return { props: {} };
});
19 replies
TTCTheo's Typesafe Cult
Created by Yatochka on 2/2/2023 in #questions
Next-Auth default middleware redirect to signin page even when logged in.
Yeah, that's true.
19 replies
TTCTheo's Typesafe Cult
Created by Yatochka on 2/2/2023 in #questions
Next-Auth default middleware redirect to signin page even when logged in.
But this all assumes that you're using SSR across your app.
19 replies
TTCTheo's Typesafe Cult
Created by Yatochka on 2/2/2023 in #questions
Next-Auth default middleware redirect to signin page even when logged in.
Yeah, the client API is great, it just can't be used in middleware. If you're wanting to use database sessions, then I've found it really useful to just export a gssp from each page.
import type { GetServerSidePropsContext } from "next";
import { getServerAuthSession } from "server/auth/get-server-auth-session";

export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
const session = await getServerAuthSession(ctx);

if (!session) {
return {
redirect: {
destination: "/sign-in",
permanent: false,
},
};
}

return {
props: {
session,
},
};
};
import type { GetServerSidePropsContext } from "next";
import { getServerAuthSession } from "server/auth/get-server-auth-session";

export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
const session = await getServerAuthSession(ctx);

if (!session) {
return {
redirect: {
destination: "/sign-in",
permanent: false,
},
};
}

return {
props: {
session,
},
};
};
then in your pages, just add export { getServerSideProps } from "utils/gssr.ts" or something similar; you could even add other arguments to allow you to pass any sort of server-side function and return it as props.
19 replies
TTCTheo's Typesafe Cult
Created by Yatochka on 2/2/2023 in #questions
Next-Auth default middleware redirect to signin page even when logged in.
What session strategy are you using? NextAuth only supports middleware with jwt sessions, so if you’re using database it won’t find a valid session. @Yiannis one thing to consider with you’re solution; it doesn’t validate the cookie, simply looks for a cookie with that key. If someone were to manually add a cookie with that key and any value, it seems to me that they’d be able to bypass the middleware and access the app.
19 replies
TTCTheo's Typesafe Cult
Created by thejessewinton on 12/6/2022 in #questions
Multiple DB Connections and Prisma clients
@Mihai Andrei I haven't, I will give it a look though. Thanks!
4 replies
TTCTheo's Typesafe Cult
Created by armand.salle on 11/12/2022 in #questions
Next Auth not working in production only
Yes, does it have any other messages? Try hitting the errors tab.
14 replies
TTCTheo's Typesafe Cult
Created by armand.salle on 11/12/2022 in #questions
Next Auth not working in production only
Do you have any logs when you try to log in? Even just the function logs in Vercel should do; might highlight a more specific error.
14 replies
TTCTheo's Typesafe Cult
Created by Grey on 9/28/2022 in #questions
React Icon situation
I've been a big fan of these ones: https://icons.radix-ui.com/
11 replies