Skip to content

Commit

Permalink
feat(dbAuth): Hint about migration after setup (#10875)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Jun 25, 2024
1 parent 163de94 commit 986edb4
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 26 deletions.
90 changes: 90 additions & 0 deletions packages/auth-providers/dbAuth/setup/src/__tests__/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,5 +315,95 @@ export const handler = createGraphQLHandler({
)
})
})

describe('db migration hint', () => {
it('is not included if no db model was created', async () => {
const packageJsonPath = path.resolve(__dirname, '../../package.json')

vol.fromJSON(
{
[packageJsonPath]: '{ "version": "0.0.0" }',
},
redwoodProjectPath,
)

await handler({
webauthn: false,
createUserModel: false,
generateAuthPages: true,
force: false,
})

expect(jest.mocked(console).log.mock.calls[0][0]).not.toContain(
'yarn rw prisma migrate dev',
)
})

it('is not included for WebAuthn if no db model was created', async () => {
const packageJsonPath = path.resolve(__dirname, '../../package.json')

vol.fromJSON(
{
[packageJsonPath]: '{ "version": "0.0.0" }',
},
redwoodProjectPath,
)

await handler({
webauthn: true,
createUserModel: false,
generateAuthPages: true,
force: false,
})

expect(jest.mocked(console).log.mock.calls[0][0]).not.toContain(
'yarn rw prisma migrate dev',
)
})

it('is included if db model was created', async () => {
const packageJsonPath = path.resolve(__dirname, '../../package.json')

vol.fromJSON(
{
[packageJsonPath]: '{ "version": "0.0.0" }',
},
redwoodProjectPath,
)

await handler({
webauthn: false,
createUserModel: true,
generateAuthPages: true,
force: false,
})

expect(jest.mocked(console).log.mock.calls[0][0]).toContain(
'yarn rw prisma migrate dev',
)
})

it('is included if db model was created', async () => {
const packageJsonPath = path.resolve(__dirname, '../../package.json')

vol.fromJSON(
{
[packageJsonPath]: '{ "version": "0.0.0" }',
},
redwoodProjectPath,
)

await handler({
webauthn: false,
createUserModel: true,
generateAuthPages: true,
force: false,
})

expect(jest.mocked(console).log.mock.calls[0][0]).toContain(
'yarn rw prisma migrate dev',
)
})
})
})
})
11 changes: 9 additions & 2 deletions packages/auth-providers/dbAuth/setup/src/setupData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ export const notes = [
'change this secret to a new value and deploy. To create a new secret, run:',
'',
' yarn rw generate secret',
'',
]

export const notesCreatedUserModel = [
`${colors.warning('Done! But you have a little more work to do:')}\n`,
'If you expose any of your user data via GraphQL be sure to exclude',
'`hashedPassword` and `salt` from the SDL file that defines the',
'fields for your user.',
'`hashedPassword` and `salt` (or whatever you named them) from the',
'SDL file that defines the fields for your user.',
'',
"To get the actual user that's logged in, take a look at `getCurrentUser()`",
`in \`${libPath}/auth.js\`. We default it to something simple, but you may`,
Expand All @@ -112,6 +113,12 @@ export const notesCreatedUserModel = [
'change this secret to a new value and deploy. To create a new secret, run:',
'',
' yarn rw generate secret',
'',
"A new User model was added to your schema. Don't forget to migrate your db",
'before you try using dbAuth:',
'',
' yarn rw prisma migrate dev',
'',
]

export const noteGenerate = [
Expand Down
3 changes: 1 addition & 2 deletions packages/auth-providers/dbAuth/setup/src/setupHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
import {
notes as webAuthnNotes,
noteGenerate as webAuthnNoteGenerate,
notesCreatedUserModel as webAuthnNotesCreatedUserModel,
extraTask as webAuthnExtraTask,
webPackages as webAuthnWebPackages,
apiPackages as webAuthnApiPackages,
Expand All @@ -47,7 +46,7 @@ export async function handler({

if (webAuthn) {
if (createDbUserModel) {
oneMoreThing.push(...webAuthnNotesCreatedUserModel)
oneMoreThing.push(...notesCreatedUserModel)
} else {
oneMoreThing.push(...webAuthnNotes)
}
Expand Down
22 changes: 0 additions & 22 deletions packages/auth-providers/dbAuth/setup/src/webAuthn.setupData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,7 @@ export const notes = [
'change this secret to a new value and deploy. To create a new secret, run:',
'',
' yarn rw generate secret',
]

// Only thing different here compared to the notes for when *not* setting up
// webauthn is the very end where we hint about adding pages for WebAuthn
// prompts
export const notesCreatedUserModel = [
`${colors.warning('Done! But you have a little more work to do:')}\n`,
'If you expose any of your user data via GraphQL be sure to exclude',
'`hashedPassword` and `salt` (or whatever you named them) from the',
'SDL file that defines the fields for your user.',
'',
"To get the actual user that's logged in, take a look at `getCurrentUser()`",
`in \`${libPath}/auth.js\`. We default it to something simple, but you may`,
'use different names for your model or unique ID fields, in which case you',
'need to update those calls (instructions are in the comment above the code).',
'',
'Finally, we created a SESSION_SECRET environment variable for you in',
`${path.join(getPaths().base, '.env')}. This value should NOT be checked`,
'into version control and should be unique for each environment you',
'deploy to. If you ever need to log everyone out of your app at once',
'change this secret to a new value and deploy. To create a new secret, run:',
'',
' yarn rw generate secret',
]

export const noteGenerate = [
Expand Down

0 comments on commit 986edb4

Please sign in to comment.