-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from rmarscher/lucia-v3-dec-23
Update to latest lucia v3 beta, oslo and arctic
- Loading branch information
Showing
21 changed files
with
627 additions
and
752 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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { NextResponse } from 'next/server' | ||
import type { NextRequest } from 'next/server' | ||
|
||
// This function can be marked `async` if using `await` inside | ||
export async function middleware(request: NextRequest) { | ||
if (request.method === 'POST' && request.body) { | ||
try { | ||
const cloned = request.clone() | ||
const requestHeaders = new Headers(request.headers) | ||
const formData = await cloned.formData() | ||
const userJson = formData.get('user') | ||
if (typeof userJson === 'string') { | ||
requestHeaders.set('x-apple-user', userJson) | ||
} | ||
return NextResponse.next({ | ||
request: { | ||
// New request headers | ||
headers: requestHeaders, | ||
}, | ||
}) | ||
} catch (e: unknown) { | ||
console.error('error parsing oauth post', e) | ||
} | ||
} | ||
} | ||
|
||
// See "Matching Paths" below to learn more | ||
export const config = { | ||
matcher: ['/oauth/apple'], | ||
} |
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,13 +1,15 @@ | ||
import { OAuthSignInScreen } from 'app/features/oauth/screen' | ||
import { OAuthSignInScreen, OAuthSignInScreenProps } from 'app/features/oauth/screen' | ||
import Head from 'next/head' | ||
|
||
export default function Page() { | ||
export { getServerSideProps } from 'app/features/oauth/screen' | ||
|
||
export default function Page(props: OAuthSignInScreenProps) { | ||
return ( | ||
<> | ||
<Head> | ||
<title>OAuth Sign In</title> | ||
</Head> | ||
<OAuthSignInScreen /> | ||
<OAuthSignInScreen {...props} /> | ||
</> | ||
) | ||
} |
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
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,26 +1,22 @@ | ||
import { getAuthOptions } from './shared' | ||
import { D1Adapter } from '@lucia-auth/adapter-sqlite' | ||
import type { Context as HonoContext, HonoRequest } from 'hono' | ||
import { Lucia } from 'lucia' | ||
import type { Middleware } from 'lucia' | ||
import { getAllowedOriginHost } from '.' | ||
import type { Context as HonoContext, Next } from 'hono' | ||
import { Bindings } from '../worker' | ||
import { verifyRequestOrigin } from 'oslo/request' | ||
|
||
export const hono = (): Middleware<[HonoContext]> => { | ||
return ({ args }) => { | ||
const [context] = args | ||
return { | ||
request: context.req, | ||
setCookie: (cookie) => { | ||
context.res.headers.append('set-cookie', cookie.serialize()) | ||
}, | ||
} | ||
export const csrfMiddleware = async (c: HonoContext<{ Bindings: Bindings }>, next: Next) => { | ||
// CSRF middleware | ||
if (c.req.method === 'GET') { | ||
return next() | ||
} | ||
const originHeader = c.req.header('origin') | ||
const hostHeader = c.req.header('host') | ||
const allowedOrigin = getAllowedOriginHost(c.env.APP_URL, c.req.raw) | ||
if ( | ||
!originHeader || | ||
!hostHeader || | ||
!verifyRequestOrigin(originHeader, [hostHeader, ...(allowedOrigin ? [allowedOrigin] : [])]) | ||
) { | ||
return c.body(null, 403) | ||
} | ||
return next() | ||
} | ||
|
||
export const createHonoAuth = (db: D1Database, appUrl: string, request?: HonoRequest) => { | ||
return new Lucia(new D1Adapter(db, { session: 'session', user: 'user' }), { | ||
...getAuthOptions(db, appUrl, request), | ||
middleware: hono(), | ||
}) | ||
} | ||
|
||
export type HonoLucia = ReturnType<typeof createHonoAuth> |
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,68 @@ | ||
import { Adapter, DatabaseSessionAttributes, DatabaseUserAttributes, Lucia, TimeSpan } from 'lucia' | ||
import { DrizzleSQLiteAdapter } from '@lucia-auth/adapter-drizzle' | ||
import { SessionTable, UserTable } from '../db/schema' | ||
import { DB } from '../db/client' | ||
|
||
/** | ||
* Lucia's isValidRequestOrigin method will compare the | ||
* origin of the request to the configured host. | ||
* We want to allow cross-domain requests from our APP_URL so return that | ||
* if the request origin host matches the APP_URL host. | ||
* @link https://github.com/lucia-auth/lucia/blob/main/packages/lucia/src/utils/url.ts | ||
*/ | ||
export const getAllowedOriginHost = (app_url: string, request?: Request) => { | ||
if (!app_url || !request) return undefined | ||
const requestOrigin = request.headers.get('Origin') | ||
const requestHost = requestOrigin ? new URL(requestOrigin).host : undefined | ||
const appHost = new URL(app_url).host | ||
return requestHost === appHost ? appHost : undefined | ||
} | ||
|
||
export const createAuth = (db: DB, appUrl: string) => { | ||
// @ts-ignore Expect type errors because this is D1 and not SQLite... but it works | ||
const adapter = new DrizzleSQLiteAdapter(db, SessionTable, UserTable) | ||
// cast probably only needed until adapter-drizzle is updated | ||
return new Lucia(adapter as Adapter, { | ||
...getAuthOptions(appUrl), | ||
}) | ||
} | ||
|
||
export const getAuthOptions = (appUrl: string) => { | ||
const env = !appUrl || appUrl.startsWith('http:') ? 'DEV' : 'PROD' | ||
return { | ||
getUserAttributes: (data: DatabaseUserAttributes) => { | ||
return { | ||
email: data.email || '', | ||
} | ||
}, | ||
// Optional additional session attributes to expose | ||
// If updated, also update createSession() in packages/api/src/auth/user.ts | ||
getSessionAttributes: (databaseSession: DatabaseSessionAttributes) => { | ||
return {} | ||
}, | ||
sessionExpiresIn: new TimeSpan(365, 'd'), | ||
sessionCookie: { | ||
name: 'auth_session', | ||
expires: false, | ||
attributes: { | ||
secure: env === 'PROD', | ||
sameSite: 'lax' as const, | ||
}, | ||
}, | ||
|
||
// If you want more debugging, uncomment this | ||
// experimental: { | ||
// debugMode: true, | ||
// }, | ||
} | ||
} | ||
|
||
declare module 'lucia' { | ||
interface Register { | ||
Lucia: ReturnType<typeof createAuth> | ||
} | ||
interface DatabaseSessionAttributes {} | ||
interface DatabaseUserAttributes { | ||
email: string | null | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.