-
Notifications
You must be signed in to change notification settings - Fork 141
feat: better-auth #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harshsbhat
wants to merge
4
commits into
upstash:main
Choose a base branch
from
harshsbhat:better-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
| import fs from "fs-extra" | ||
| import path from "path" | ||
| import { type Installer } from "@/installers/index.js" | ||
| import { PKG_ROOT } from "@/constants.js" | ||
| import { addPackageDependency } from "@/utils/add-package-dep.js" | ||
|
|
||
| export const betterAuthInstaller: Installer = ({ projectDir, databaseProvider }) => { | ||
| addPackageDependency({ | ||
| projectDir, | ||
| dependencies: ["better-auth"], | ||
| devDependencies: false, | ||
| }) | ||
|
|
||
| const extrasDir = path.join(PKG_ROOT, "template/extras") | ||
|
|
||
| // auth routes | ||
| const routerSrc = path.join(extrasDir, `src/app/api/auth/[...all]/route.ts`) | ||
| const routerDest = path.join(projectDir, `src/app/api/auth/[...all]/route.ts`) | ||
|
|
||
| // auth client | ||
| const clientSrc = path.join(extrasDir, `src/lib/auth-client.ts`) | ||
| const clientDest = path.join(projectDir, `src/lib/auth-client.ts`) | ||
|
|
||
| // auth lib | ||
| const libSrc = path.join(extrasDir, `src/lib/auth.ts`) | ||
| const libDest = path.join(projectDir, `src/lib/auth.ts`) | ||
|
|
||
| // auth schema | ||
| const schemaSrc = path.join(extrasDir, `src/server/db/schema/with-postgres-auth.ts`) | ||
| const schemaDest = path.join(projectDir, `src/server/db/schema.ts`) | ||
|
|
||
| // auth page | ||
| const pageSrc = path.join(extrasDir, `src/app/with-better-auth-page.tsx`) | ||
| const pageDest = path.join(projectDir, `src/app/page.tsx`) | ||
|
|
||
| // auth components | ||
| const componentsSrc = path.join(extrasDir, `src/app/with-better-auth-form.tsx`) | ||
| const componentsDest = path.join(projectDir, `src/app/components/signup.tsx`) | ||
|
|
||
| // env handling | ||
| const envSrc = path.join(extrasDir, `config/_env-drizzle-better-auth`) | ||
| const envDest = path.join(projectDir, ".env") | ||
|
|
||
| // copy all files | ||
| fs.copySync(routerSrc, routerDest) | ||
| fs.copySync(clientSrc, clientDest) | ||
| fs.copySync(libSrc, libDest) | ||
| fs.copySync(componentsSrc, componentsDest) | ||
| fs.copySync(pageSrc, pageDest) | ||
| fs.copySync(schemaSrc, schemaDest) | ||
|
|
||
| // append env vars instead of overwriting | ||
| const betterAuthEnv = fs.readFileSync(envSrc, "utf-8") | ||
| fs.appendFileSync(envDest, `\n\n# Better Auth\n${betterAuthEnv}`) | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
|
|
||
|
|
||
| BETTER_AUTH_SECRET= | ||
| BETTER_AUTH_URL=http://localhost:3000 | ||
| GOOGLE_CLIENT_ID= | ||
| GOOGLE_CLIENT_SECRET= | ||
| GOOGLE_REDIRECT_URI=http://localhost:3000/api/auth/callback/google |
This file contains hidden or 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,4 @@ | ||
| import { auth } from "@/lib/auth"; | ||
| import { toNextJsHandler } from "better-auth/next-js"; | ||
|
|
||
| export const { GET, POST } = toNextJsHandler(auth.handler); |
This file contains hidden or 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,72 @@ | ||
| "use client" | ||
|
|
||
| import { useState } from "react" | ||
| import { authClient } from "@/lib/auth-client" | ||
|
|
||
| export const AuthComponent = () => { | ||
| const { | ||
| data: session, | ||
| isPending, | ||
| } = authClient.useSession() | ||
|
|
||
| const [isLoading, setIsLoading] = useState(false) | ||
|
|
||
| const handleGoogleAuth = async () => { | ||
| setIsLoading(true) | ||
| try { | ||
| await authClient.signIn.social({ | ||
| provider: "google", | ||
| callbackURL: "/", | ||
| }) | ||
| } catch (error) { | ||
| console.error(error) | ||
| } finally { | ||
| setIsLoading(false) | ||
| } | ||
| } | ||
|
|
||
| const handleSignOut = async () => { | ||
| setIsLoading(true) | ||
| try { | ||
| await authClient.signOut() | ||
| } catch (error) { | ||
| console.error(error) | ||
| } finally { | ||
| setIsLoading(false) | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <div className="w-full max-w-sm backdrop-blur-lg bg-black/15 px-8 py-6 rounded-md text-zinc-100/75 space-y-4"> | ||
| {isPending ? ( | ||
| <p className="text-center">Checking session...</p> | ||
| ) : session?.user?.email ? ( | ||
| <div className="flex flex-col items-center gap-6 p-6 bg-zinc-900 rounded-xl shadow-lg"> | ||
| <div className="text-center"> | ||
| <p className="text-zinc-400 text-sm">Logged in as</p> | ||
| <p className="font-semibold text-lg text-white">{session.user.email}</p> | ||
| </div> | ||
|
|
||
| <button | ||
| onClick={handleSignOut} | ||
| disabled={isLoading} | ||
| className="cursor-pointer w-full max-w-xs flex items-center justify-center gap-3 rounded-md text-base font-medium ring-2 ring-offset-2 ring-offset-zinc-900 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white ring-transparent hover:ring-white h-12 px-6 py-3 bg-gradient-to-tr from-zinc-800 to-zinc-700 text-zinc-100 transition disabled:opacity-60" | ||
| > | ||
| {isLoading ? "Signing out..." : "Sign Out"} | ||
| </button> | ||
| </div> | ||
|
|
||
| ) : ( | ||
| <div className="flex justify-center"> | ||
| <button | ||
| onClick={handleGoogleAuth} | ||
| disabled={isLoading} | ||
| className="cursor-pointer w-full flex items-center justify-center gap-3 rounded-md text-base/6 ring-2 ring-offset-2 ring-offset-white focus-visible:outline-none focus-visible:ring-zinc-900 ring-transparent hover:ring-zinc-900 h-12 px-10 py-3 bg-zinc-800 text-zinc-100 font-medium bg-gradient-to-tl from-zinc-900 to-zinc-800 transition hover:bg-zinc-700 disabled:opacity-60 disabled:cursor-not-allowed" | ||
| > | ||
| {isLoading ? "Signing up..." : "Sign Up with Google"} | ||
| </button> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or 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,32 @@ | ||
| import { cn } from "@/lib/utils" | ||
| import { AuthComponent } from "./components/signup" | ||
| import { RecentPost } from "./components/post" | ||
|
|
||
| export default async function Home() { | ||
| return ( | ||
| <main className="flex min-h-screen bg-gradient-to-br from-zinc-950 via-zinc-900 to-zinc-950 flex-col items-center justify-center relative isolate"> | ||
| <div className="absolute inset-0 -z-10 opacity-50 mix-blend-soft-light bg-[url('/noise.svg')] [mask-image:radial-gradient(ellipse_at_center,black,transparent)]" /> | ||
| <div className="container flex flex-col items-center justify-center gap-6 px-4 py-16"> | ||
| <h1 | ||
| className={cn( | ||
| "inline-flex tracking-tight flex-col gap-1 transition text-center", | ||
| "font-display text-4xl sm:text-5xl md:text-6xl font-semibold leading-none lg:text-[4rem]", | ||
| "bg-gradient-to-r from-20% bg-clip-text text-transparent", | ||
| "from-white to-gray-50" | ||
| )} | ||
| > | ||
| <span>JStack</span> | ||
| </h1> | ||
|
|
||
| <p className="text-[#ececf399] text-lg/7 md:text-xl/8 text-pretty sm:text-wrap sm:text-center text-center mb-8"> | ||
| The stack for building seriously fast, lightweight and{" "} | ||
| <span className="inline sm:block"> | ||
| end-to-end typesafe Next.js apps. | ||
| </span> | ||
| </p> | ||
| <RecentPost /> | ||
| <AuthComponent /> | ||
| </div> | ||
| </main> | ||
| ) | ||
| } |
This file contains hidden or 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,5 @@ | ||
| import { createAuthClient } from "better-auth/react" | ||
|
|
||
| export const authClient = createAuthClient({ | ||
| baseURL: process.env.BETTER_AUTH_URL, | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be done via a jstack middleware as well. using
useThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! I realized this after reviewing the https://www.better-auth.com/docs/integrations/hono yesterday. Give me a day. I will do the fixes. Also, the
envones.Thanks for taking the time to review tho
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully this helps, I already implemented this for our company 2 months ago, publishing the code so it helps you. Good luck brother
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for sharing this. We'll implement this tomorrow after college
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eager to see your implementation! the jstack cli def need a proper better-auth setup on launch