c
c
TTCTheo's Typesafe Cult
Created by c on 5/25/2024 in #questions
Using table prefixes in drizzle multi-project schema with NextAuth.
totally my bad, it's covered in https://authjs.dev/getting-started/adapters/drizzle if anyone has this issue in the future. just needed to pass the tables object with the correct assignments.
4 replies
TTCTheo's Typesafe Cult
Created by Del on 2/25/2023 in #questions
Caching Data for a trpc endpoint
react query
4 replies
TTCTheo's Typesafe Cult
Created by T on 1/25/2023 in #questions
Protected routes in Nextjs?
if you send me a code example I can take a look
55 replies
TTCTheo's Typesafe Cult
Created by Çağlar on 1/27/2023 in #questions
Best practice for Admin dashboard?
hard to help much without code to look at. i actually had trouble doing this just based off of docs but you can try doing what i did and just reading through all the nextauth/trpc example reps and searching through past issues for other code examples.
36 replies
TTCTheo's Typesafe Cult
Created by Çağlar on 1/27/2023 in #questions
Best practice for Admin dashboard?
not sure that this is best practice, but it's a middleware implementation w/ jwt: https://discord.com/channels/966627436387266600/1067624837507457034/1068639470896824320
36 replies
TTCTheo's Typesafe Cult
Created by T on 1/25/2023 in #questions
Protected routes in Nextjs?
(it's obv not the best implementation for actually assigning roles, but finally have protected routes working with middleware and jwt.)
55 replies
TTCTheo's Typesafe Cult
Created by T on 1/25/2023 in #questions
Protected routes in Nextjs?
figured this out using middleware with roles if anyone else has a similar issue:
// middleware.ts

import { withAuth } from "next-auth/middleware";

// More on how NextAuth.js middleware works: https://next-auth.js.org/configuration/nextjs#middleware
export default withAuth({
callbacks: {
authorized({ req, token }) {
// `/admin` requires admin role
if (
req.nextUrl.pathname === "/dash" ||
req.nextUrl.pathname === "/dash/templates"
) {
return token?.userRole === "admin";
}
// `/me` only requires the user to be logged in
return !!token;
},
},
});

export const config = { matcher: ["/dash", "/dash/templates"] };
// middleware.ts

import { withAuth } from "next-auth/middleware";

// More on how NextAuth.js middleware works: https://next-auth.js.org/configuration/nextjs#middleware
export default withAuth({
callbacks: {
authorized({ req, token }) {
// `/admin` requires admin role
if (
req.nextUrl.pathname === "/dash" ||
req.nextUrl.pathname === "/dash/templates"
) {
return token?.userRole === "admin";
}
// `/me` only requires the user to be logged in
return !!token;
},
},
});

export const config = { matcher: ["/dash", "/dash/templates"] };
and
// [...nextauth].ts

import NextAuth, { type NextAuthOptions } from "next-auth";
import DiscordProvider from "next-auth/providers/discord";
// Prisma adapter for NextAuth, optional and can be removed
import { PrismaAdapter } from "@next-auth/prisma-adapter";

import { env } from "../../../env/server.mjs";
import { prisma } from "../../../server/db";

export const authOptions: NextAuthOptions = {
// Include user.id on session
callbacks: {
async jwt({ token }) {
console.log(token);
if (token.email === "[email protected]") {
token.userRole = "admin";
}
console.log(token.userRole);
return token;
},
},
// Configure one or more authentication providers
adapter: PrismaAdapter(prisma),
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
],
session: {
strategy: "jwt",
},
};

export default NextAuth(authOptions);
// [...nextauth].ts

import NextAuth, { type NextAuthOptions } from "next-auth";
import DiscordProvider from "next-auth/providers/discord";
// Prisma adapter for NextAuth, optional and can be removed
import { PrismaAdapter } from "@next-auth/prisma-adapter";

import { env } from "../../../env/server.mjs";
import { prisma } from "../../../server/db";

export const authOptions: NextAuthOptions = {
// Include user.id on session
callbacks: {
async jwt({ token }) {
console.log(token);
if (token.email === "[email protected]") {
token.userRole = "admin";
}
console.log(token.userRole);
return token;
},
},
// Configure one or more authentication providers
adapter: PrismaAdapter(prisma),
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
],
session: {
strategy: "jwt",
},
};

export default NextAuth(authOptions);
55 replies
TTCTheo's Typesafe Cult
Created by T on 1/25/2023 in #questions
Protected routes in Nextjs?
also having this issue - was wondering if anyone has a middleware code example that I could look at for protecting routes.
55 replies
TTCTheo's Typesafe Cult
Created by c on 1/26/2023 in #questions
Example files missing, referenced in folder structure docs.
(thanks for putting all of this together, it's been v helpful so far!)
11 replies