Skip to content

Commit 9fafcf8

Browse files
committed
SHIP IT
1 parent bb0b36e commit 9fafcf8

File tree

8 files changed

+24
-37
lines changed

8 files changed

+24
-37
lines changed

packages/api/src/auth/index.ts

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

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

8180
let authDecoders: Array<Decoder> = []
8281

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ export class DbAuthHandler<
292292
corsContext: CorsContext | undefined
293293
sessionExpiresDate: string
294294
webAuthnExpiresDate: string
295+
encryptedSession: string | null = null
295296

296297
// class constant: list of auth methods that are supported
297298
static get METHODS(): AuthMethodNames[] {
@@ -402,9 +403,9 @@ export class DbAuthHandler<
402403
}
403404

404405
try {
405-
const [session, csrfToken] = decryptSession(
406-
getSession(this.cookie, this.options.cookie?.name)
407-
)
406+
this.encryptedSession = getSession(this.cookie, this.options.cookie?.name)
407+
408+
const [session, csrfToken] = decryptSession(this.encryptedSession)
408409
this.session = session
409410
this.sessionCsrfToken = csrfToken
410411
} catch (e) {
@@ -568,12 +569,8 @@ export class DbAuthHandler<
568569

569570
async getToken() {
570571
try {
571-
const user = await this._getCurrentUser()
572-
573-
// need to return *something* for our existing Authorization header stuff
574-
// to work, so return the user's ID in case we can use it for something
575-
// in the future
576-
return [user[this.options.authFields.id]]
572+
// Just return the encrypted session cookie, to be passed back in the Authorization header
573+
return [this.encryptedSession || '']
577574
} catch (e: any) {
578575
if (e instanceof DbAuthError.NotLoggedInError) {
579576
return this._logoutResponse()
@@ -1435,6 +1432,7 @@ export class DbAuthHandler<
14351432
_ok(body: string, headers = {}, options = { statusCode: 200 }) {
14361433
return {
14371434
statusCode: options.statusCode,
1435+
// @TODO should we do a null check in body?!
14381436
body: typeof body === 'string' ? body : JSON.stringify(body),
14391437
headers: { 'Content-Type': 'application/json', ...headers },
14401438
}

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

+3-14
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,17 @@ 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
1413
const session = dbAuthSession(req.event, cookieNameOption)
15-
const authHeaderUserId = token
16-
17-
if (session.id.toString() !== authHeaderUserId) {
18-
console.error('Authorization header does not match decrypted user ID')
19-
throw new Error('Authorization header does not match decrypted user ID')
20-
}
2114

15+
// We no longer compare the session id with the bearer token
16+
// Because we only pass around the encrypted session (in both cookie and header)
2217
return session
2318
}
2419
}
2520

2621
/** @deprecated use `createAuthDecoder` */
2722
export const authDecoder: Decoder = async (
28-
authHeaderValue: string,
23+
_authHeaderValue: string, // Browser: 4, FEServer: encryptedSession
2924
type: string,
3025
req: { event: APIGatewayProxyEvent }
3126
) => {
@@ -37,12 +32,6 @@ export const authDecoder: Decoder = async (
3732
// it fall back to the default cookie name `session`, making it backwards
3833
// compatible with existing RW apps.
3934
const session = dbAuthSession(req.event, undefined)
40-
const authHeaderUserId = authHeaderValue
41-
42-
if (session.id.toString() !== authHeaderUserId) {
43-
console.error('Authorization header does not match decrypted user ID')
44-
throw new Error('Authorization header does not match decrypted user ID')
45-
}
4635

4736
return session
4837
}

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const extractCookie = (event: APIGatewayProxyEvent) => {
3535
return eventGraphiQLHeadersCookie(event) || eventHeadersCookie(event)
3636
}
3737

38-
function extractSessionFromHeader(event: APIGatewayProxyEvent) {
38+
function extractEncryptedSessionFromHeader(event: APIGatewayProxyEvent) {
3939
return event.headers.authorization?.split(' ')[1]
4040
}
4141

@@ -88,17 +88,18 @@ export const dbAuthSession = (
8888
cookieNameOption: string | undefined
8989
) => {
9090
const cookieHeader = extractCookie(event)
91-
const sessionInAuthHeader = extractSessionFromHeader(event)
91+
const sessionInAuthHeader = extractEncryptedSessionFromHeader(event)
9292

93-
if (cookieHeader && !sessionInAuthHeader) {
93+
if (cookieHeader) {
94+
// i.e. Browser making a request
9495
const [session, _csrfToken] = decryptSession(
9596
getSession(cookieHeader, cookieNameOption)
9697
)
9798
return session
9899
} else if (sessionInAuthHeader) {
100+
// i.e. FE Sever makes the request, and adds encrypted session to the Authorization header
99101
const [session, _csrfToken] = decryptSession(sessionInAuthHeader)
100102

101-
console.log(`👉 \n ~ file: shared.ts:103 ~ session:`, session)
102103
return session
103104
} else {
104105
return null

packages/auth/src/AuthProvider/AuthProvider.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,10 @@ export function createAuthProvider<
148148
logIn,
149149
logOut,
150150
getToken:
151+
// When its rendering on the server, just get the token from the serverAuthState
151152
typeof window === 'undefined'
152153
? async () => {
153-
console.log(
154-
'xxxxx definitely calling this function',
155-
serverAuthState
156-
)
157-
return serverAuthState.token || null
154+
return serverAuthState.encryptedSession || null
158155
}
159156
: getToken,
160157
getCurrentUser,

packages/auth/src/AuthProvider/ServerAuthProvider.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { AuthProviderState } from './AuthProviderState'
44
import { defaultAuthProviderState } from './AuthProviderState'
55

66
export const ServerAuthContext = React.createContext<
7-
AuthProviderState<never> & { token: string | null }
8-
>({ ...defaultAuthProviderState, token: null })
7+
AuthProviderState<never> & { encryptedSession: string | null }
8+
>({ ...defaultAuthProviderState, encryptedSession: null })
99

1010
export const ServerAuthProvider = ServerAuthContext.Provider

packages/vite/src/streaming/createReactStreamingHandler.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ export const createReactStreamingHandler = async (
6565
const middleware = entryServerImport.middleware
6666

6767
if (middleware) {
68-
decodedAuthState = await middleware(req)
68+
try {
69+
decodedAuthState = (await middleware(req)).context
70+
} catch (e) {
71+
console.error('Whooopsie, error in middleware', e)
72+
}
6973
}
7074
}
7175

packages/web/src/apollo/links.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export function createAuthApolloLink(
4444
) {
4545
return new ApolloLink((operation, forward) => {
4646
const { token } = operation.getContext()
47-
4847
// Only add auth headers when there's a token. `token` is `null` when `!isAuthenticated`.
4948
const authHeaders = token
5049
? {

0 commit comments

Comments
 (0)