Skip to content

Commit caafe21

Browse files
committed
fix auth
1 parent e65c8bd commit caafe21

File tree

8 files changed

+47
-90
lines changed

8 files changed

+47
-90
lines changed

convex/http.ts

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,11 @@
11
import { ossStats } from './stats'
22
import { httpRouter } from 'convex/server'
3-
43
import { betterAuthComponent } from './auth'
5-
6-
import { convexAdapter } from '@convex-dev/better-auth'
7-
import { convex } from '@convex-dev/better-auth/plugins'
8-
import { betterAuth } from 'better-auth'
9-
import { reactStartHelpers } from '@convex-dev/better-auth/react-start'
10-
import { GenericCtx } from './_generated/server'
11-
12-
// You'll want to replace this with an environment variable
13-
const siteUrl = process.env.URL
4+
import { createAuth } from '../src/server/auth'
145

156
const http = httpRouter()
167

17-
const createAuth = (ctx: GenericCtx) =>
18-
betterAuth({
19-
// All auth requests will be proxied through your TanStack Start server
20-
baseURL: siteUrl,
21-
database: convexAdapter(ctx, betterAuthComponent),
22-
23-
// Simple non-verified email/password to get started
24-
socialProviders: {
25-
github: {
26-
clientId: process.env.GITHUB_OAUTH_CLIENT_ID as string,
27-
clientSecret: process.env.GITHUB_OAUTH_CLIENT_SECRET as string,
28-
},
29-
google: {
30-
clientId: process.env.GOOGLE_OAUTH_CLIENT_ID as string,
31-
clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET as string,
32-
},
33-
},
34-
plugins: [
35-
// The Convex plugin is required
36-
convex(),
37-
],
38-
})
39-
408
ossStats.registerRoutes(http)
419
betterAuthComponent.registerRoutes(http, createAuth)
4210

43-
export const { fetchSession, reactStartHandler, getCookieName } =
44-
reactStartHelpers(createAuth, {
45-
convexSiteUrl: process.env.VITE_CONVEX_SITE_URL!,
46-
})
47-
4811
export default http

src/routes/_libraries/login.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ import { useIsDark } from '~/hooks/useIsDark'
33
import { FaGithub, FaGoogle } from 'react-icons/fa'
44
import splashLightImg from '~/images/splash-light.png'
55
import splashDarkImg from '~/images/splash-dark.png'
6-
import { fetchServerAuth } from '~/utils/auth.isomorphic'
76
import { redirect } from '@tanstack/react-router'
87

98
export const Route = createFileRoute({
109
component: LoginPage,
11-
loader: async () => {
12-
const { token, userId } = await fetchServerAuth()
10+
loader: async ({ context }) => {
11+
const user = await context.ensureUser()
1312

14-
if (token) {
13+
if (user) {
1514
throw redirect({ to: '/account' })
1615
}
1716
},

src/routes/admin/index.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
import * as React from 'react'
2-
import { Link, Outlet, redirect } from '@tanstack/react-router'
3-
import { CgClose, CgMenuLeft } from 'react-icons/cg'
1+
import { Link } from '@tanstack/react-router'
42
import { FaLock, FaUsers, FaChartBar } from 'react-icons/fa'
5-
import { twMerge } from 'tailwind-merge'
6-
import logoColor100w from '~/images/logo-color-100w.png'
7-
import { ThemeToggle } from '~/components/ThemeToggle'
83
import { useQuery } from 'convex/react'
94
import { api } from 'convex/_generated/api'
10-
import { fetchServerAuth } from '~/utils/auth.isomorphic'
115

126
export const Route = createFileRoute({
13-
beforeLoad: async () => {
14-
const { userId } = await fetchServerAuth()
15-
16-
if (!userId) {
17-
throw redirect({ to: '/login' })
18-
}
19-
},
207
component: AdminPage,
218
})
229

src/routes/admin/route.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import * as React from 'react'
2-
import { Link, LinkOptions, Outlet, redirect } from '@tanstack/react-router'
2+
import { Link, LinkOptions, Outlet } from '@tanstack/react-router'
33
import { CgClose, CgMenuLeft } from 'react-icons/cg'
44
import { FaHome, FaLock, FaUser, FaUsers } from 'react-icons/fa'
55
import { twMerge } from 'tailwind-merge'
66
import logoColor100w from '~/images/logo-color-100w.png'
77
import { ThemeToggle } from '~/components/ThemeToggle'
88
import { useQuery } from 'convex/react'
99
import { api } from 'convex/_generated/api'
10-
import { fetchServerAuth } from '~/utils/auth.isomorphic'
1110

1211
export const Route = createFileRoute({
13-
beforeLoad: async () => {
14-
const { userId } = await fetchServerAuth()
15-
16-
if (!userId) {
17-
throw redirect({ to: '/login' })
18-
}
12+
beforeLoad: async ({ context }) => {
13+
await context.ensureUser()
1914
},
2015
component: () => {
2116
return (

src/routes/admin/users.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
FaChevronRight,
1313
} from 'react-icons/fa'
1414
import type { Id } from 'convex/_generated/dataModel'
15-
import { fetchServerAuth } from '~/utils/auth.isomorphic'
1615
import {
1716
useReactTable,
1817
getCoreRowModel,
@@ -29,13 +28,6 @@ type User = {
2928
}
3029

3130
export const Route = createFileRoute({
32-
beforeLoad: async () => {
33-
const { userId } = await fetchServerAuth()
34-
35-
if (!userId) {
36-
throw redirect({ to: '/login' })
37-
}
38-
},
3931
component: UsersPage,
4032
})
4133

src/routes/api/auth/$.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { reactStartHandler } from '~/utils/auth.server'
1+
import { reactStartHandler } from "~/server/auth"
2+
23

34
export const ServerRoute = createServerFileRoute().methods({
45
GET: ({ request }) => {

src/server/auth.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { convexAdapter } from '@convex-dev/better-auth'
2+
import { convex } from '@convex-dev/better-auth/plugins'
3+
import { betterAuth } from 'better-auth'
4+
import { reactStartHelpers } from '@convex-dev/better-auth/react-start'
5+
import { GenericCtx } from 'convex/_generated/server'
6+
import { betterAuthComponent } from 'convex/auth'
7+
8+
// You'll want to replace this with an environment variable
9+
const siteUrl = process.env.URL
10+
11+
export const createAuth = (ctx: GenericCtx) =>
12+
betterAuth({
13+
// All auth requests will be proxied through your TanStack Start server
14+
baseURL: siteUrl,
15+
database: convexAdapter(ctx, betterAuthComponent),
16+
17+
// Simple non-verified email/password to get started
18+
socialProviders: {
19+
github: {
20+
clientId: process.env.GITHUB_OAUTH_CLIENT_ID as string,
21+
clientSecret: process.env.GITHUB_OAUTH_CLIENT_SECRET as string,
22+
},
23+
google: {
24+
clientId: process.env.GOOGLE_OAUTH_CLIENT_ID as string,
25+
clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET as string,
26+
},
27+
},
28+
plugins: [
29+
// The Convex plugin is required
30+
convex(),
31+
],
32+
})
33+
34+
export const { fetchSession, reactStartHandler, getCookieName } =
35+
reactStartHelpers(createAuth, {
36+
convexSiteUrl: process.env.VITE_CONVEX_SITE_URL!,
37+
})

src/utils/auth.isomorphic.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)