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

feat(server-auth): dbAuth 3/3 - handle login, logout, signup, etc. requests if forwarded from middleware #10457

Merged
merged 10 commits into from
Apr 16, 2024

Conversation

dac09
Copy link
Collaborator

@dac09 dac09 commented Apr 15, 2024

Closes #10448

This PR updates the DbAuthHandler class to handle requests forwarded from middleware, so it can generate responses for login, logout, signup, etc. These are POST requests - it used to be to the /auth function, but now they will be captured by dbAuth middleware and forwarded onto DbAuthHandler.

High level changes:

  • use the Headers class in each of the "method" responses. This allows us to set multi-value headers like Set-Cookie. A simple object would not. See type AuthMethodOutput
  • extracts buildResponse into a testable function and adds test. For Set-Cookie headers we return an array of strings.

In the middleware here's the code I had for the final conversion:

    if (AUTHHANDLER_REQUEST) {
      const output = await dbAuthHandler(req)

      const finalHeaders = new Headers()
      Object.entries(output.headers).forEach(([key, value]) => {
        if (Array.isArray(value)) {
          value.forEach((v) => finalHeaders.append(key, v))
        } else {
          finalHeaders.append(key, value)
        }
      })

      return new MiddlewareResponse(output.body, {
        headers: finalHeaders,
        status: output.statusCode,
      })
    }

Tasks

@dac09 dac09 requested a review from dthyresson April 15, 2024 09:21
@dac09 dac09 added this to the SSR milestone Apr 15, 2024
@dac09 dac09 added the release:feature This PR introduces a new feature label Apr 15, 2024
@dac09 dac09 self-assigned this Apr 15, 2024
@dac09 dac09 changed the title feat(server-auth): dbAuth 2/3 - handle login, logout, signup, etc. requests if forwarded from middleware feat(server-auth): dbAuth 3/3 - handle login, logout, signup, etc. requests if forwarded from middleware Apr 15, 2024
Copy link
Contributor

@dthyresson dthyresson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dac09 Confirmed login, logout, signup and currentUser work in classic GraphQL app.

Both session and auth-provider cookies are set ... and cleared on logout.

See the change the the test import in packages/auth-providers/dbAuth/api/src/__tests__/buildDbAuthResponse.test.ts but LGTM and approved to :shipit:

@@ -0,0 +1,69 @@
import { describe, it, expect } from 'vitest'

import { buildDbAuthResponse } from '/Users/dac09/Code/redwood/packages/auth-providers/dbAuth/api/src/shared'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dac09 This test fails locally as it points to a local package with your "dac" path.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dac09 Changing to "import { buildDbAuthResponse } from '../shared'" fixes test run. I'll post the change

@@ -0,0 +1,69 @@
import { describe, it, expect } from 'vitest'

import { buildDbAuthResponse } from '/Users/dac09/Code/redwood/packages/auth-providers/dbAuth/api/src/shared'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { buildDbAuthResponse } from '/Users/dac09/Code/redwood/packages/auth-providers/dbAuth/api/src/shared'
import { buildDbAuthResponse } from '../shared'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I committed the change FYI

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dac09 dac09 merged commit dca417f into redwoodjs:main Apr 16, 2024
46 checks passed
@dac09 dac09 deleted the feat/dbauth-handler-mw-post-requests branch April 16, 2024 05:35
dac09 added a commit that referenced this pull request Apr 16, 2024
…-dbauth-middleware

* 'main' of github.com:redwoodjs/redwood:
  chore(location): Accept URL-like object (#10467)
  fix(router): Remove barrel exports from router.tsx (#10464)
  chore(dbauth-mw): Refactor web side dbAuth creation (#10460)
  chore(router): Prevent circular dependency for namedRoutes (#10463)
  chore(router): route-validators: Better types and clean up comments (#10462)
  feat(server-auth): dbAuth 3/3 -  handle login, logout, signup, etc. requests if forwarded from middleware (#10457)
  docs(router): Document new NavLink className replacement behavior (#10401)
  chore(refactor): Split the router out into smaller logical units (#10434)
  feat(server-auth): Part 1/3: dbAuth middleware support (web side changes) (#10444)
  chore(auth): Build: Put ESM at the root, and CJS in /cjs (#10458)
  fix(ssr): Successfully serve static assets like `favicon.png` (#10455)
  chore(deps): update chore (#10367)
  (docs) Fix useCache headers and links (#10451)
  chore: remove aws-lambda (#10450)
  chore(deps): update dependency typescript to v5.4.5 (#10452)
  feat(og-gen): Update implementation of useLocation | Update App template (#10441)
  feat(og-gen): Adds package and vite plugin for dynamic og generation (#10439)
dac09 added a commit that referenced this pull request Apr 16, 2024
…g-gen-mw-p2

* 'main' of github.com:redwoodjs/redwood:
  chore(dbauth-mw): Refactor web side dbAuth creation (#10460)
  chore(router): Prevent circular dependency for namedRoutes (#10463)
  chore(router): route-validators: Better types and clean up comments (#10462)
  feat(server-auth): dbAuth 3/3 -  handle login, logout, signup, etc. requests if forwarded from middleware (#10457)
  docs(router): Document new NavLink className replacement behavior (#10401)
  chore(refactor): Split the router out into smaller logical units (#10434)
  feat(server-auth): Part 1/3: dbAuth middleware support (web side changes) (#10444)
  chore(auth): Build: Put ESM at the root, and CJS in /cjs (#10458)
  fix(ssr): Successfully serve static assets like `favicon.png` (#10455)
  chore(deps): update chore (#10367)
  (docs) Fix useCache headers and links (#10451)
  chore: remove aws-lambda (#10450)
  chore(deps): update dependency typescript to v5.4.5 (#10452)
dac09 added a commit to dac09/redwood that referenced this pull request Apr 18, 2024
* 'main' of github.com:redwoodjs/redwood:
  Revert "chore(location): Accept URL-like object" (redwoodjs#10473)
  RSC: Be consistent about inlining rollup input (redwoodjs#10472)
  chore(paths): Remove outdated comment (redwoodjs#10471)
  feat(server-auth): Update getAuthenticationContext to support cookies and tokens both (redwoodjs#10465)
  chore(location): Accept URL-like object (redwoodjs#10467)
  fix(router): Remove barrel exports from router.tsx (redwoodjs#10464)
  chore(dbauth-mw): Refactor web side dbAuth creation (redwoodjs#10460)
  chore(router): Prevent circular dependency for namedRoutes (redwoodjs#10463)
  chore(router): route-validators: Better types and clean up comments (redwoodjs#10462)
  feat(server-auth): dbAuth 3/3 -  handle login, logout, signup, etc. requests if forwarded from middleware (redwoodjs#10457)
  docs(router): Document new NavLink className replacement behavior (redwoodjs#10401)
  chore(refactor): Split the router out into smaller logical units (redwoodjs#10434)
  feat(server-auth): Part 1/3: dbAuth middleware support (web side changes) (redwoodjs#10444)
  chore(auth): Build: Put ESM at the root, and CJS in /cjs (redwoodjs#10458)
  fix(ssr): Successfully serve static assets like `favicon.png` (redwoodjs#10455)
  chore(deps): update chore (redwoodjs#10367)
  (docs) Fix useCache headers and links (redwoodjs#10451)
  chore: remove aws-lambda (redwoodjs#10450)
  chore(deps): update dependency typescript to v5.4.5 (redwoodjs#10452)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature This PR introduces a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Auth] Implement POST handlers for login, signup, etc. on dbAuth
2 participants