Skip to content

Commit 09f36a1

Browse files
committed
feat(astro): add clerk auth
Remove lucia auth.
1 parent a57cbda commit 09f36a1

File tree

13 files changed

+552
-57
lines changed

13 files changed

+552
-57
lines changed

packages/codius-astro/astro.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cloudflare from "@astrojs/cloudflare"
22
import react from "@astrojs/react"
33
import tailwind from "@astrojs/tailwind"
4+
import clerk from "@clerk/astro"
45
import { defineConfig } from "astro/config"
56
import simpleStackQuery from "simple-stack-query"
67

@@ -17,6 +18,7 @@ export default defineConfig({
1718
},
1819
}),
1920
integrations: [
21+
clerk(),
2022
react(),
2123
tailwind({
2224
applyBaseStyles: false,

packages/codius-astro/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@astrojs/cloudflare": "^11.0.4",
2222
"@astrojs/react": "^3.6.1",
2323
"@astrojs/tailwind": "^5.1.0",
24+
"@clerk/astro": "^1.0.12",
2425
"@lucia-auth/adapter-sqlite": "^3.0.1",
2526
"@octokit/request-error": "^6.1.1",
2627
"@octokit/rest": "^21.0.0",

packages/codius-astro/src/actions/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ export const server = {
1010
id: z.string(),
1111
}),
1212
handler: async ({ id }, context) => {
13-
if (!context.locals.user) {
13+
const userId = context.locals.auth().userId
14+
if (!userId) {
1415
throw new ActionError({
1516
code: "UNAUTHORIZED",
1617
})
1718
}
1819
const app = await context.locals.db.apps.delete({
1920
id,
20-
userId: context.locals.user.id,
21+
userId,
2122
})
2223
if (!app) {
2324
throw new ActionError({
@@ -60,7 +61,8 @@ export const server = {
6061
}),
6162
// https://github.com/withastro/roadmap/blob/actions/proposals/0046-actions.md#access-api-context
6263
handler: async ({ repoUrl, branch, directory }, context) => {
63-
if (!context.locals.user) {
64+
const userId = context.locals.auth().userId
65+
if (!userId) {
6466
throw new ActionError({
6567
code: "UNAUTHORIZED",
6668
})
@@ -73,7 +75,7 @@ export const server = {
7375
const commit = await getCommit({ owner, repo, branch })
7476

7577
const app = await context.locals.db.apps.create({
76-
userId: context.locals.user.id,
78+
userId,
7779
githubOwner: owner,
7880
repo,
7981
branch,
@@ -125,8 +127,8 @@ export const server = {
125127
const metadata: Stripe.MetadataParam = {
126128
appId,
127129
}
128-
if (context.locals.user) {
129-
metadata.userId = context.locals.user.id
130+
if (context.locals.auth().userId) {
131+
metadata.userId = context.locals.auth().userId
130132
}
131133
const session = await stripe.checkout.sessions.create({
132134
line_items: [

packages/codius-astro/src/components/UserAppsTable.astro

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ import {
99
TableHeader,
1010
TableRow,
1111
} from "@/components/ui/table"
12-
import type { User } from "lucia"
1312
1413
interface Props {
15-
user: User
14+
userId: string
1615
dispatcherHostname: string
1716
}
1817
19-
const { dispatcherHostname, user } = Astro.props
20-
const apps = await Astro.locals.db.apps.getByUserId(user.id)
18+
const { dispatcherHostname, userId } = Astro.props
19+
const apps = await Astro.locals.db.apps.getByUserId(userId)
2120
---
2221

2322
<Table>

packages/codius-astro/src/env.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference path="../.astro/actions.d.ts" />
22
/// <reference types="astro/client" />
3+
/// <reference types="@clerk/astro/env" />
34
type D1Database = import("@cloudflare/workers-types").D1Database
45
type DurableObjectNamespace =
56
import("@cloudflare/workers-types").DurableObjectNamespace
@@ -19,6 +20,8 @@ type ENV = {
1920
GITHUB_WEBHOOK_SECRET: string
2021
STRIPE_TOPUP_PRICE_ID: string
2122
STRIPE_SECRET_KEY: string
23+
PUBLIC_CLERK_PUBLISHABLE_KEY: string
24+
CLERK_SECRET_KEY: string
2225
BILLING_DURABLE_OBJECT: DurableObjectNamespace<BillingDurableObject>
2326
DB: D1Database
2427
}

packages/codius-astro/src/layouts/Layout.astro

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
---
22
import "@/styles/globals.css"
33
import { ModeToggle } from "@/components/ModeToggle"
4-
import LoginButton from "@/components/LoginButton.astro"
5-
import LogoutButton from "@/components/LogoutButton.astro"
4+
// import LoginButton from "@/components/LoginButton.astro"
5+
// import LogoutButton from "@/components/LogoutButton.astro"
6+
import {
7+
SignedIn,
8+
SignedOut,
9+
UserButton,
10+
SignInButton,
11+
} from "@clerk/astro/components"
612
713
interface Props {
814
title?: string
@@ -75,15 +81,12 @@ const { title = "Welcome to Codius" } = Astro.props
7581
</svg>
7682
<h1>Welcome to <span>Codius</span></h1>
7783
<ModeToggle client:load />
78-
{
79-
Astro.locals.user ? (
80-
<LogoutButton />
81-
) : (
82-
<div>
83-
<LoginButton />
84-
</div>
85-
)
86-
}
84+
<SignedOut>
85+
<SignInButton mode="modal" />
86+
</SignedOut>
87+
<SignedIn>
88+
<UserButton />
89+
</SignedIn>
8790
<slot />
8891
</body>
8992
</html>
Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { initializeLucia, initializeGitHub } from "@/lib/auth"
22
import { DB } from "@/lib/db"
3-
import { defineMiddleware } from "astro:middleware"
3+
// import { defineMiddleware } from "astro:middleware"
4+
import { clerkMiddleware } from "@clerk/astro/server"
5+
import type { MiddlewareHandler } from "astro"
6+
import { sequence } from "astro:middleware"
47

5-
// import { verifyRequestOrigin } from "lucia"
8+
const clerkAuth = clerkMiddleware()
69

7-
export const onRequest = defineMiddleware(async (context, next) => {
10+
const setupContext: MiddlewareHandler = (context, next) => {
811
context.locals.db = new DB(context.locals.runtime.env.DB)
912
const lucia = initializeLucia(context.locals.runtime.env.DB)
1013
context.locals.lucia = lucia
@@ -13,31 +16,7 @@ export const onRequest = defineMiddleware(async (context, next) => {
1316
context.locals.runtime.env.GITHUB_CLIENT_SECRET,
1417
)
1518
context.locals.github = github
16-
const sessionId = context.cookies.get(lucia.sessionCookieName)?.value ?? null
17-
if (!sessionId) {
18-
context.locals.user = null
19-
context.locals.session = null
20-
return next()
21-
}
22-
23-
const { session, user } = await lucia.validateSession(sessionId)
24-
if (session && session.fresh) {
25-
const sessionCookie = lucia.createSessionCookie(session.id)
26-
context.cookies.set(
27-
sessionCookie.name,
28-
sessionCookie.value,
29-
sessionCookie.attributes,
30-
)
31-
}
32-
if (!session) {
33-
const sessionCookie = lucia.createBlankSessionCookie()
34-
context.cookies.set(
35-
sessionCookie.name,
36-
sessionCookie.value,
37-
sessionCookie.attributes,
38-
)
39-
}
40-
context.locals.session = session
41-
context.locals.user = user
4219
return next()
43-
})
20+
}
21+
22+
export const onRequest = sequence(clerkAuth, setupContext)

packages/codius-astro/src/pages/apps.astro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { AppForm } from "@/components/AppForm"
33
import UserAppsTable from "@/components/UserAppsTable.astro"
44
import Layout from "@/layouts/Layout.astro"
55
6-
if (!Astro.locals.user) {
6+
const userId = Astro.locals.auth().userId
7+
if (!userId) {
78
return Astro.redirect("/")
89
}
910
---
@@ -12,7 +13,7 @@ if (!Astro.locals.user) {
1213
<main>
1314
<AppForm client:load />
1415
<UserAppsTable
15-
user={Astro.locals.user}
16+
userId={userId}
1617
dispatcherHostname={Astro.locals.runtime.env.DISPATHER_HOSTNAME}
1718
/>
1819
</main>

packages/codius-astro/src/pages/apps/[id].astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ if (!app) {
2828
})
2929
}
3030
31-
const userIsDeployer = Astro.locals.user?.id === app.deployer.id
31+
const userIsDeployer = Astro.locals.auth().userId === app.deployer.id
3232
3333
if (app.status !== "deployed" && !userIsDeployer) {
3434
return new Response(null, {

packages/codius-astro/src/pages/checkout-sessions/[id]/success.astro

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ if (!session.amount_total) {
3333
3434
const { appId, userId } = session.metadata
3535
36-
if (Astro.locals.user && userId && userId !== Astro.locals.user.id) {
36+
if (
37+
Astro.locals.auth().userId &&
38+
userId &&
39+
userId !== Astro.locals.auth().userId
40+
) {
3741
return new Response("Forbidden", { status: 403 })
3842
}
3943

0 commit comments

Comments
 (0)