Skip to content
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

chore(dbauth-mw): Refactor web side dbAuth creation #10460

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .changesets/10460.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
- chore(dbauth-mw): Refactor web side dbAuth creation (#10460) by @dac09

This PR changes how the webside auth is initialised, by removing the `createMiddlewareAuth` function, instead it just detects it internally.

For dbAuth this is what it will looks like:

```js:web/src/auth.ts
import {
createDbAuthClient,
createAuth,
} from '@redwoodjs/auth-dbauth-web'

const dbAuthClient = createDbAuthClient({
middleware: true,
})

// Internally we know to use the middleware version of the client
// because middleware is set to true above!
export const { AuthProvider, useAuth } = createAuth(dbAuthClient)

```

For other auth providers we are going to export a similar looking function:

```js
import { createAuth, createSupabaseAuthClient } from '@redwoodjs/auth-supabase-web'

// This function is new, and just wraps creating supabase👇
const supabaseClient = createSupabaseAuthClient({
supabaseUrl: process.env.SUPABASE_URL || '',
supabaseKey: process.env.SUPABASE_KEY || '',
middleware: true
})

export const { AuthProvider, useAuth } = createAuth(supabaseClient)
```

This also means our users won't need to change where supabase client is imported from, for example.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { act, renderHook } from '@testing-library/react'

import type { CustomProviderHooks, DbAuthClientArgs } from '../dbAuth'
import { createDbAuthClient, createMiddlewareAuth } from '../dbAuth'
import { createDbAuthClient, createAuth } from '../dbAuth'

import { fetchMock } from './dbAuth.test'

Expand All @@ -16,7 +16,7 @@ export function getMwDbAuth(
) {
// We have to create a special createDbAuthClient with middleware = true
const dbAuthClient = createDbAuthClient({ ...args, middleware: true })
const { useAuth, AuthProvider } = createMiddlewareAuth(dbAuthClient, {
const { useAuth, AuthProvider } = createAuth(dbAuthClient, {
useCurrentUser: args.useCurrentUser,
useHasRole: args.useHasRole,
})
Expand Down
9 changes: 7 additions & 2 deletions packages/auth-providers/dbAuth/web/src/dbAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ export type CustomProviderHooks = {
) => (rolesToCheck: string | string[]) => boolean
}

export function createMiddlewareAuth(
// This is the middleware-edition auth function
// Overrides the default getCurrentUser to fetch it from middleware instead
function createMiddlewareAuth(
dbAuthClient: ReturnType<typeof createDbAuthClient>,
customProviderHooks?: CustomProviderHooks,
) {
return createAuthentication(dbAuthClient, {
// @MARK This is key! 👇
// Override the default getCurrentUser to fetch it from middleware instead
...customProviderHooks,
useCurrentUser:
customProviderHooks?.useCurrentUser ??
Expand All @@ -48,6 +49,10 @@ export function createAuth(
) => (rolesToCheck: string | string[]) => boolean
},
) {
if (dbAuthClient.useMiddlewareAuth) {
return createMiddlewareAuth(dbAuthClient, customProviderHooks)
}

return createAuthentication(dbAuthClient, customProviderHooks)
}

Expand Down
Loading