-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: trpc setup and auth connected to db
- Loading branch information
Showing
27 changed files
with
465 additions
and
7,942 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Config } from "drizzle-kit"; | ||
import { env } from "@/env"; | ||
|
||
export default { | ||
schema: "./src/server/db/schema.ts", | ||
dialect: "postgresql", | ||
dbCredentials: { | ||
url: env.POSTGRES_URL, | ||
}, | ||
} satisfies Config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { GET, POST } from "@/lib/auth"; | ||
export { GET, POST } from "@/server/auth"; | ||
|
||
export const runtime = "edge"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { NextRequest } from "next/server"; | ||
import { env } from "@/env"; | ||
import { appRouter } from "@/server/api/root"; | ||
import { createTRPCContext } from "@/server/api/trpc"; | ||
import { fetchRequestHandler } from "@trpc/server/adapters/fetch"; | ||
|
||
/** | ||
* This wraps the `createTRPCContext` helper and provides the required context for the tRPC API when | ||
* handling a HTTP request (e.g. when you make requests from Client Components). | ||
*/ | ||
const createContext = async (req: NextRequest) => { | ||
return createTRPCContext({ | ||
headers: req.headers, | ||
}); | ||
}; | ||
|
||
const handler = (req: NextRequest) => | ||
fetchRequestHandler({ | ||
endpoint: "/api/trpc", | ||
req, | ||
router: appRouter, | ||
createContext: () => createContext(req), | ||
onError: | ||
env.NODE_ENV === "development" | ||
? ({ path, error }) => { | ||
console.error( | ||
`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`, | ||
); | ||
} | ||
: undefined, | ||
}); | ||
|
||
export { handler as GET, handler as POST }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { auth } from "@/lib/auth"; | ||
import { auth } from "@/server/auth"; | ||
|
||
import { cn } from "@tanya.in/ui"; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"use server"; | ||
|
||
import { signIn as attempt } from "@/server/auth"; | ||
import { AuthError } from "next-auth"; | ||
|
||
export async function signIn(email: string, password: string) { | ||
try { | ||
await attempt("credentials", { | ||
email: email, | ||
password: password, | ||
redirectTo: "/", | ||
}); | ||
} catch (error) { | ||
if (error instanceof AuthError) { | ||
return { | ||
error: error.cause?.err?.message ?? "Oops! Something went wrong!", | ||
}; | ||
} | ||
throw error; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { authRouter } from "@/server/api/routers/auth/auth.procedure"; | ||
|
||
import { createCallerFactory, createTRPCRouter } from "./trpc"; | ||
|
||
/** | ||
* This is the primary router for your server. | ||
* | ||
* All routers added in /api/routers should be manually added here. | ||
*/ | ||
export const appRouter = createTRPCRouter({ | ||
auth: authRouter, | ||
}); | ||
|
||
// export type definition of API | ||
export type AppRouter = typeof appRouter; | ||
|
||
/** | ||
* Create a server-side caller for the tRPC API. | ||
* @example | ||
* const trpc = createCaller(createContext); | ||
* const res = await trpc.post.all(); | ||
* ^? Post[] | ||
*/ | ||
export const createCaller = createCallerFactory(appRouter); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { users } from "@/server/db/schema"; | ||
import { createInsertSchema } from "drizzle-zod"; | ||
|
||
export const authSchema = createInsertSchema(users).pick({ | ||
email: true, | ||
password: true, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { createTRPCRouter, publicProcedure } from "@/server/api/trpc"; | ||
import { signIn } from "@/server/auth"; | ||
import { TRPCError } from "@trpc/server"; | ||
import { AuthError } from "next-auth"; | ||
|
||
import { authSchema } from "./auth.input"; | ||
|
||
export const authRouter = createTRPCRouter({ | ||
login: publicProcedure.input(authSchema).mutation(async ({ input }) => { | ||
try { | ||
await signIn("credentials", { | ||
email: input.email, | ||
password: input.password, | ||
redirectTo: "/", | ||
}); | ||
} catch (error) { | ||
if (error instanceof AuthError) | ||
throw new TRPCError({ | ||
code: "BAD_REQUEST", | ||
message: error.cause?.err?.message ?? "Oops! Something went wrong!", | ||
}); | ||
} | ||
}), | ||
}); |
Oops, something went wrong.