Skip to content

Commit 9e86135

Browse files
committed
Simple auth state parsing with middleware in entry server
1 parent 9d4a1e7 commit 9e86135

File tree

8 files changed

+46
-20
lines changed

8 files changed

+46
-20
lines changed

packages/api/src/auth/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export const getAuthenticationContext = async ({
7676
}
7777

7878
const { schema, token } = parseAuthorizationHeader(event)
79+
console.log(`👉 \n ~ file: index.ts:79 ~ token:`, token)
7980

8081
let authDecoders: Array<Decoder> = []
8182

packages/auth-providers/dbAuth/api/src/decoder.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const createAuthDecoder = (cookieNameOption: string): Decoder => {
1010
return null
1111
}
1212

13+
// @TODO for SSR we need to make sure we are passing the cookie from the FE to the BE
1314
const session = dbAuthSession(req.event, cookieNameOption)
1415
const authHeaderUserId = token
1516

packages/auth/src/AuthProvider/AuthProvider.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type { ReactNode } from 'react'
2-
import React, { useEffect, useState } from 'react'
2+
import React, { useContext, useEffect, useState } from 'react'
33

44
import type { AuthContextInterface, CurrentUser } from '../AuthContext'
55
import type { AuthImplementation } from '../AuthImplementation'
66

77
import type { AuthProviderState } from './AuthProviderState'
88
import { defaultAuthProviderState } from './AuthProviderState'
9+
import { ServerAuthContext } from './ServerAuthProvider'
910
import { useCurrentUser } from './useCurrentUser'
1011
import { useForgotPassword } from './useForgotPassword'
1112
import { useHasRole } from './useHasRole'
@@ -82,9 +83,11 @@ export function createAuthProvider<
8283
}: AuthProviderProps) => {
8384
// const [hasRestoredState, setHasRestoredState] = useState(false)
8485

86+
const serverAuthState = useContext(ServerAuthContext)
87+
8588
const [authProviderState, setAuthProviderState] = useState<
8689
AuthProviderState<TUser>
87-
>(defaultAuthProviderState)
90+
>(serverAuthState || defaultAuthProviderState)
8891

8992
const getToken = useToken(authImplementation)
9093

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react'
2+
3+
import type { AuthProviderState } from './AuthProviderState'
4+
import { defaultAuthProviderState } from './AuthProviderState'
5+
6+
export const ServerAuthContext = React.createContext<
7+
AuthProviderState<never> | undefined
8+
>(defaultAuthProviderState)
9+
10+
export const ServerAuthProvider = ServerAuthContext.Provider

packages/auth/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ export { AuthContextInterface, CurrentUser } from './AuthContext'
22
export { useNoAuth, UseAuth } from './useAuth'
33
export { createAuthentication } from './authFactory'
44
export type { AuthImplementation } from './AuthImplementation'
5+
6+
export * from './AuthProvider/ServerAuthProvider'

packages/vite/src/streaming/createReactStreamingHandler.ts

+9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export const createReactStreamingHandler = async (
5050
})
5151
}
5252

53+
let decodedAuthState
54+
5355
// Do this inside the handler for **dev-only**.
5456
// This makes sure that changes to entry-server are picked up on refresh
5557
if (!isProd) {
@@ -59,6 +61,12 @@ export const createReactStreamingHandler = async (
5961
fallbackDocumentImport = await viteDevServer.ssrLoadModule(
6062
rwPaths.web.document
6163
)
64+
65+
const middleware = entryServerImport.middleware
66+
67+
if (middleware) {
68+
decodedAuthState = await middleware(req)
69+
}
6270
}
6371

6472
const ServerEntry =
@@ -112,6 +120,7 @@ export const createReactStreamingHandler = async (
112120
cssLinks,
113121
isProd,
114122
jsBundles,
123+
authState: decodedAuthState,
115124
},
116125
{
117126
waitForAllReady: isSeoCrawler,

packages/vite/src/streaming/streamHelpers.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import type {
77
ReactDOMServerReadableStream,
88
} from 'react-dom/server'
99

10+
import { ServerAuthProvider } from '@redwoodjs/auth'
11+
import type { AuthProviderState } from '@redwoodjs/auth/src/AuthProvider/AuthProviderState'
1012
import type { TagDescriptor } from '@redwoodjs/web'
1113
// @TODO (ESM), use exports field. Cannot import from web because of index exports
1214
import {
@@ -26,6 +28,7 @@ interface RenderToStreamArgs {
2628
cssLinks: string[]
2729
isProd: boolean
2830
jsBundles?: string[]
31+
authState?: AuthProviderState<never> | undefined
2932
}
3033

3134
interface StreamOptions {
@@ -46,7 +49,9 @@ export async function reactRenderToStreamResponse(
4649
cssLinks,
4750
isProd,
4851
jsBundles = [],
52+
authState,
4953
} = renderOptions
54+
console.log(`👉 \n ~ file: streamHelpers.ts:54 ~ authState:`, authState)
5055

5156
if (!isProd) {
5257
// For development, we need to inject the react-refresh runtime
@@ -84,15 +89,21 @@ export async function reactRenderToStreamResponse(
8489

8590
const renderRoot = (path: string) => {
8691
return React.createElement(
87-
ServerHtmlProvider,
92+
ServerAuthProvider,
8893
{
89-
value: injectToPage,
94+
value: authState,
9095
},
91-
ServerEntry({
92-
url: path,
93-
css: cssLinks,
94-
meta: metaTags,
95-
})
96+
React.createElement(
97+
ServerHtmlProvider,
98+
{
99+
value: injectToPage,
100+
},
101+
ServerEntry({
102+
url: path,
103+
css: cssLinks,
104+
meta: metaTags,
105+
})
106+
)
96107
)
97108
}
98109

packages/web/src/components/ServerInject.tsx

-11
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,3 @@ export function useServerInsertedHTML(callback: () => React.ReactNode): void {
7070
addInsertedServerHTMLCallback(callback)
7171
}
7272
}
73-
74-
// @TODO use this in streamHelpers final block
75-
export const AppendToHead = ({ tagsToAppend }: { tagsToAppend: string }) => {
76-
return (
77-
<script
78-
dangerouslySetInnerHTML={{
79-
__html: `document?.head.append(${tagsToAppend})`,
80-
}}
81-
/>
82-
)
83-
}

0 commit comments

Comments
 (0)