Aruthoth
Aruthoth
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
doing an if for each route I need to reach doesn't seem like an elegant way to solve it, but it's what worked in my test
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
I still need to see how this part still works in astro
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
import { auth } from '@/lib/auth'
import { defineMiddleware, sequence } from 'astro:middleware'

const setSession = defineMiddleware(async (c, next) => {
const isAuthed = await auth(c).api.getSession({
headers: c.request.headers,
})

if (isAuthed) {
c.locals.user = isAuthed.user
c.locals.session = isAuthed.session
} else {
c.locals.user = null
c.locals.session = null
}

return next()
})

const protectsRoutes = defineMiddleware(async (c, next) => {
const noSession = c.locals.user === null || c.locals.session === null

if (noSession && c.url.pathname !== '/') {
return next(
new Request('/login', {
headers: {
'x-redirect-to': c.url.pathname,
},
})
)
}

return next()
})

export const onRequest = sequence(setSession, protectsRoutes)
import { auth } from '@/lib/auth'
import { defineMiddleware, sequence } from 'astro:middleware'

const setSession = defineMiddleware(async (c, next) => {
const isAuthed = await auth(c).api.getSession({
headers: c.request.headers,
})

if (isAuthed) {
c.locals.user = isAuthed.user
c.locals.session = isAuthed.session
} else {
c.locals.user = null
c.locals.session = null
}

return next()
})

const protectsRoutes = defineMiddleware(async (c, next) => {
const noSession = c.locals.user === null || c.locals.session === null

if (noSession && c.url.pathname !== '/') {
return next(
new Request('/login', {
headers: {
'x-redirect-to': c.url.pathname,
},
})
)
}

return next()
})

export const onRequest = sequence(setSession, protectsRoutes)
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
I'm using Astro to develop, my middleware is like this for now
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
I did some initial tests to see what the authentication would be like and left it to finish after I finished the main part of the application
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
Well, I haven't "got to that part yet"
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
import type { OpenAPIHono, RouteConfig, RouteHandler } from '@hono/zod-openapi'
import type { Env } from 'hono'

import type { getAuth } from '@/lib/auth'

type Auth = ReturnType<typeof getAuth>
type VariableUser = Auth['$Infer']['Session']['user'] | null
type VariableSession = Auth['$Infer']['Session']['session'] | null

export interface AppBindings extends Env {
Bindings: {
AUTH_URL: string
AUTH_SECRET: string
AUTH_GOOGLE_CLIENT_ID: string
AUTH_GOOGLE_CLIENT_SECRET: string
DB: D1Database
}
Variables: {
user: VariableUser
session: VariableSession
}
}

export type AppOpenAPI = OpenAPIHono<AppBindings>

export type AppRouteHandler<R extends RouteConfig> = RouteHandler<R, AppBindings>
import type { OpenAPIHono, RouteConfig, RouteHandler } from '@hono/zod-openapi'
import type { Env } from 'hono'

import type { getAuth } from '@/lib/auth'

type Auth = ReturnType<typeof getAuth>
type VariableUser = Auth['$Infer']['Session']['user'] | null
type VariableSession = Auth['$Infer']['Session']['session'] | null

export interface AppBindings extends Env {
Bindings: {
AUTH_URL: string
AUTH_SECRET: string
AUTH_GOOGLE_CLIENT_ID: string
AUTH_GOOGLE_CLIENT_SECRET: string
DB: D1Database
}
Variables: {
user: VariableUser
session: VariableSession
}
}

export type AppOpenAPI = OpenAPIHono<AppBindings>

export type AppRouteHandler<R extends RouteConfig> = RouteHandler<R, AppBindings>
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
Look, I'm not sure if there's a more elegant way to do this, but I just created a few types
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
I forgot to mention, but I also changed this part
app.on(['GET', 'POST'], '/api/auth/**', (c) => getAuth(c).handler(c.req.raw))
app.on(['GET', 'POST'], '/api/auth/**', (c) => getAuth(c).handler(c.req.raw))
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
No description
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
If I initialize it incorrectly, like the first time I did, I only receive a 500 error.
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
Take a look to see if you're initializing Drizzle incorrectly, just like I did 😅
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
import { createMiddleware } from 'hono/factory'

import type { AppBindings } from '@/lib/types'

import { getAuth } from '@/lib/auth'

const userSession = createMiddleware<AppBindings>(async (c, next) => {
const auth = getAuth(c) // only change <------------------
const session = await auth.api.getSession({ headers: c.req.raw.headers })

if (!session) {
c.set('user', null)
c.set('session', null)
return next()
}

c.set('user', session.user)
c.set('session', session.session)
return next()
})

export default userSession
import { createMiddleware } from 'hono/factory'

import type { AppBindings } from '@/lib/types'

import { getAuth } from '@/lib/auth'

const userSession = createMiddleware<AppBindings>(async (c, next) => {
const auth = getAuth(c) // only change <------------------
const session = await auth.api.getSession({ headers: c.req.raw.headers })

if (!session) {
c.set('user', null)
c.set('session', null)
return next()
}

c.set('user', session.user)
c.set('session', session.session)
return next()
})

export default userSession
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
I also adjusted the middleware for the session
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
database: drizzleAdapter(c.env.DB, { -> database: drizzleAdapter(drizzle(c.env.DB), {
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
It was missing the step of creating the client with Drizzle first.
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
// new code
import { drizzle } from 'drizzle-orm/d1'

// code...

return betterAuth({
database: drizzleAdapter(drizzle(c.env.DB), { // add drizzle

// code...
// new code
import { drizzle } from 'drizzle-orm/d1'

// code...

return betterAuth({
database: drizzleAdapter(drizzle(c.env.DB), { // add drizzle

// code...
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
Revisiting the code more carefully, I realized the database is being initialized incorrectly.
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
At least I managed to create a new user and retrieve their session using Google.
32 replies
BABetter Auth
Created by Aruthoth on 12/9/2024 in #help
How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
Except for populating the database, I think I managed to get the authentication flow working.
32 replies