Skip to content

Commit

Permalink
Fix onBeforeLogin hook params (#2254)
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho authored Aug 27, 2024
1 parent 8bf411f commit dd83783
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 28 deletions.
4 changes: 4 additions & 0 deletions waspc/data/Generator/templates/sdk/wasp/server/auth/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ type OnBeforeLoginHookParams = {
* Provider ID object that contains the provider name and the provide user ID.
*/
providerId: ProviderId
/**
* User that is trying to log in.
*/
user: Awaited<ReturnType<typeof findAuthWithUserBy>>['user']
/**
* Request object that can be used to access the incoming request.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,21 @@ async function getAuthIdFromProviderDetails({
if (existingAuthIdentity) {
const authId = existingAuthIdentity.{= authFieldOnAuthIdentityEntityName =}.id

// NOTE: Fetching the user to pass it to the login hooks - it's a bit wasteful
// but we wanted to keep the onAfterLoginHook params consistent for all auth providers.
const auth = await findAuthWithUserBy({ id: authId })

// NOTE: We are calling login hooks here even though we didn't log in the user yet.
// It's because we have access to the OAuth tokens here and we want to pass them to the hooks.
// We could have stored the tokens temporarily and called the hooks after the session is created,
// but this keeps the implementation simpler.
// The downside of this approach is that we can't provide the session to the login hooks, but this is
// an okay trade-off because OAuth tokens are more valuable to users than the session ID.
await onBeforeLoginHook({ req, providerId })

// NOTE: Fetching the user to pass it to the onAfterLoginHook - it's a bit wasteful
// but we wanted to keep the onAfterLoginHook params consistent for all auth providers.
const auth = await findAuthWithUserBy({ id: authId })
await onBeforeLoginHook({
req,
providerId,
user: auth.user,
})

// NOTE: check the comment above onBeforeLoginHook for the explanation why we call onAfterLoginHook here.
await onAfterLoginHook({
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 11 additions & 16 deletions web/docs/auth/auth-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,7 @@ app myApp {
```

```js title="src/auth/hooks.js"
export const onBeforeOAuthRedirect = async ({
url,
oauth,
prisma,
req,
}) => {
export const onBeforeOAuthRedirect = async ({ url, oauth, prisma, req }) => {
console.log('query params before oAuth redirect', req.query)

// Saving query params for later use in onAfterSignup or onAfterLogin hooks
Expand Down Expand Up @@ -388,7 +383,7 @@ app myApp {
```js title="src/auth/hooks.js"
import { HttpError } from 'wasp/server'

export const onBeforeLogin = async ({ providerId, prisma, req }) => {
export const onBeforeLogin = async ({ providerId, user, prisma, req }) => {
if (
providerId.providerName === 'email' &&
providerId.providerUserId === '[email protected]'
Expand Down Expand Up @@ -417,6 +412,7 @@ import type { OnBeforeLoginHook } from 'wasp/server/auth'

export const onBeforeLogin: OnBeforeLoginHook = async ({
providerId,
user,
prisma,
req,
}) => {
Expand Down Expand Up @@ -733,12 +729,7 @@ Wasp ignores this hook's **return value**.
<TabItem value="js" label="JavaScript">

```js title="src/auth/hooks.js"
export const onBeforeOAuthRedirect = async ({
url,
oauth,
prisma,
req,
}) => {
export const onBeforeOAuthRedirect = async ({ url, oauth, prisma, req }) => {
// Hook code goes here

return { url }
Expand Down Expand Up @@ -776,11 +767,11 @@ The hook receives an object as **input** with the following properties:

The `oauth` object has the following fields:

- `uniqueRequestId: string`
- `uniqueRequestId: string`

The unique request ID for the OAuth flow (you might know it as the `state` parameter in OAuth.)
The unique request ID for the OAuth flow (you might know it as the `state` parameter in OAuth.)

You can use the unique request ID to save data (e.g. request query params) that you can later use in the `onAfterSignup` or `onAfterLogin` hooks.
You can use the unique request ID to save data (e.g. request query params) that you can later use in the `onAfterSignup` or `onAfterLogin` hooks.

- Plus the [common hook input](#common-hook-input)

Expand Down Expand Up @@ -819,6 +810,10 @@ The hook receives an object as **input** with the following properties:

- [`providerId: ProviderId`](#providerid-fields)

- `user: User`

The user that is trying to log in.

- Plus the [common hook input](#common-hook-input)

Wasp ignores this hook's **return value**.
Expand Down

0 comments on commit dd83783

Please sign in to comment.