forked from vercel/ai-chatbot
-
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.
- Loading branch information
1 parent
124efca
commit cb60f8b
Showing
139 changed files
with
8,935 additions
and
8,790 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 |
---|---|---|
@@ -1,12 +1,16 @@ | ||
# You must first activate a Billing Account here: https://platform.openai.com/account/billing/overview | ||
# Then get your OpenAI API Key here: https://platform.openai.com/account/api-keys | ||
OPENAI_API_KEY=XXXXXXXX | ||
# Get your OpenAI API Key here: https://platform.openai.com/account/api-keys | ||
OPENAI_API_KEY=**** | ||
|
||
# Generate a random secret: https://generate-secret.vercel.app/32 or `openssl rand -base64 32` | ||
AUTH_SECRET=XXXXXXXX | ||
AUTH_SECRET=**** | ||
|
||
# Instructions to create kv database here: https://vercel.com/docs/storage/vercel-kv/quickstart and | ||
KV_URL=XXXXXXXX | ||
KV_REST_API_URL=XXXXXXXX | ||
KV_REST_API_TOKEN=XXXXXXXX | ||
KV_REST_API_READ_ONLY_TOKEN=XXXXXXXX | ||
/* | ||
* The following keys below are automatically created and | ||
* added to your environment when you deploy on vercel | ||
*/ | ||
|
||
# Instructions to create kv database here: https://vercel.com/docs/storage/vercel-blob | ||
BLOB_READ_WRITE_TOKEN=**** | ||
|
||
# Instructions to create a database here: https://vercel.com/docs/storage/vercel-postgres/quickstart | ||
POSTGRES_URL=**** |
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,41 @@ | ||
{ | ||
"extends": [ | ||
"next/core-web-vitals", | ||
"plugin:import/recommended", | ||
"plugin:import/typescript", | ||
"prettier", | ||
"plugin:tailwindcss/recommended" | ||
], | ||
"plugins": ["import", "tailwindcss"], | ||
"rules": { | ||
"tailwindcss/no-custom-classname": "off", | ||
"tailwindcss/classnames-order": "off", | ||
"import/order": [ | ||
"error", | ||
{ | ||
"groups": [ | ||
"builtin", | ||
"external", | ||
"internal", | ||
["parent", "sibling"], | ||
"index", | ||
"object", | ||
"type" | ||
], | ||
"newlines-between": "always", | ||
"alphabetize": { | ||
"order": "asc", | ||
"caseInsensitive": true | ||
} | ||
} | ||
] | ||
}, | ||
"settings": { | ||
"import/resolver": { | ||
"typescript": { | ||
"alwaysTryTypes": true | ||
} | ||
} | ||
}, | ||
"ignorePatterns": ["**/components/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Experimental_LanguageModelV1Middleware } from "ai"; | ||
|
||
export const customMiddleware: Experimental_LanguageModelV1Middleware = {}; |
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,8 @@ | ||
import { openai } from "@ai-sdk/openai"; | ||
import { experimental_wrapLanguageModel as wrapLanguageModel } from "ai"; | ||
import { customMiddleware } from "./custom-middleware"; | ||
|
||
export const customModel = wrapLanguageModel({ | ||
model: openai("gpt-4o"), | ||
middleware: customMiddleware, | ||
}); |
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,85 @@ | ||
"use server"; | ||
|
||
import { z } from "zod"; | ||
|
||
import { createUser, getUser } from "@/db/queries"; | ||
|
||
import { signIn } from "./auth"; | ||
|
||
const authFormSchema = z.object({ | ||
email: z.string().email(), | ||
password: z.string().min(6), | ||
}); | ||
|
||
export interface LoginActionState { | ||
status: "idle" | "in_progress" | "success" | "failed" | "invalid_data"; | ||
} | ||
|
||
export const login = async ( | ||
_: LoginActionState, | ||
formData: FormData, | ||
): Promise<LoginActionState> => { | ||
try { | ||
const validatedData = authFormSchema.parse({ | ||
email: formData.get("email"), | ||
password: formData.get("password"), | ||
}); | ||
|
||
await signIn("credentials", { | ||
email: validatedData.email, | ||
password: validatedData.password, | ||
redirect: false, | ||
}); | ||
|
||
return { status: "success" }; | ||
} catch (error) { | ||
if (error instanceof z.ZodError) { | ||
return { status: "invalid_data" }; | ||
} | ||
|
||
return { status: "failed" }; | ||
} | ||
}; | ||
|
||
export interface RegisterActionState { | ||
status: | ||
| "idle" | ||
| "in_progress" | ||
| "success" | ||
| "failed" | ||
| "user_exists" | ||
| "invalid_data"; | ||
} | ||
|
||
export const register = async ( | ||
_: RegisterActionState, | ||
formData: FormData, | ||
): Promise<RegisterActionState> => { | ||
try { | ||
const validatedData = authFormSchema.parse({ | ||
email: formData.get("email"), | ||
password: formData.get("password"), | ||
}); | ||
|
||
let [user] = await getUser(validatedData.email); | ||
|
||
if (user) { | ||
return { status: "user_exists" } as RegisterActionState; | ||
} else { | ||
await createUser(validatedData.email, validatedData.password); | ||
await signIn("credentials", { | ||
email: validatedData.email, | ||
password: validatedData.password, | ||
redirect: false, | ||
}); | ||
|
||
return { status: "success" }; | ||
} | ||
} catch (error) { | ||
if (error instanceof z.ZodError) { | ||
return { status: "invalid_data" }; | ||
} | ||
|
||
return { status: "failed" }; | ||
} | ||
}; |
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 @@ | ||
export { GET, POST } from "@/app/(auth)/auth"; |
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,39 @@ | ||
import { NextAuthConfig } from "next-auth"; | ||
|
||
export const authConfig = { | ||
pages: { | ||
signIn: "/login", | ||
newUser: "/", | ||
}, | ||
providers: [ | ||
// added later in auth.ts since it requires bcrypt which is only compatible with Node.js | ||
// while this file is also used in non-Node.js environments | ||
], | ||
callbacks: { | ||
authorized({ auth, request: { nextUrl } }) { | ||
let isLoggedIn = !!auth?.user; | ||
let isOnChat = nextUrl.pathname.startsWith("/"); | ||
let isOnRegister = nextUrl.pathname.startsWith("/register"); | ||
let isOnLogin = nextUrl.pathname.startsWith("/login"); | ||
|
||
if (isLoggedIn && (isOnLogin || isOnRegister)) { | ||
return Response.redirect(new URL("/", nextUrl)); | ||
} | ||
|
||
if (isOnRegister || isOnLogin) { | ||
return true; // Always allow access to register and login pages | ||
} | ||
|
||
if (isOnChat) { | ||
if (isLoggedIn) return true; | ||
return false; // Redirect unauthenticated users to login page | ||
} | ||
|
||
if (isLoggedIn) { | ||
return Response.redirect(new URL("/", nextUrl)); | ||
} | ||
|
||
return true; | ||
}, | ||
}, | ||
} satisfies NextAuthConfig; |
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,53 @@ | ||
import { compare } from "bcrypt-ts"; | ||
import NextAuth, { User, Session } from "next-auth"; | ||
import Credentials from "next-auth/providers/credentials"; | ||
|
||
import { getUser } from "@/db/queries"; | ||
|
||
import { authConfig } from "./auth.config"; | ||
|
||
interface ExtendedSession extends Session { | ||
user: User; | ||
} | ||
|
||
export const { | ||
handlers: { GET, POST }, | ||
auth, | ||
signIn, | ||
signOut, | ||
} = NextAuth({ | ||
...authConfig, | ||
providers: [ | ||
Credentials({ | ||
credentials: {}, | ||
async authorize({ email, password }: any) { | ||
let users = await getUser(email); | ||
if (users.length === 0) return null; | ||
let passwordsMatch = await compare(password, users[0].password!); | ||
if (passwordsMatch) return users[0] as any; | ||
}, | ||
}), | ||
], | ||
callbacks: { | ||
async jwt({ token, user }) { | ||
if (user) { | ||
token.id = user.id; | ||
} | ||
|
||
return token; | ||
}, | ||
async session({ | ||
session, | ||
token, | ||
}: { | ||
session: ExtendedSession; | ||
token: any; | ||
}) { | ||
if (session.user) { | ||
session.user.id = token.id as string; | ||
} | ||
|
||
return session; | ||
}, | ||
}, | ||
}); |
Oops, something went wrong.