Repository navigation
fix: Sign in with Apple failed with a server error #42
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { test } from 'node:test'; | ||
| import { SignJWT, exportJWK, generateKeyPair } from 'jose'; | ||
|
|
||
| const databaseUrl = process.env.TEST_DATABASE_URL; | ||
|
|
||
| const BUNDLE_ID = 'app.splitpro'; | ||
|
|
||
| void test( | ||
| 'Sign in with Apple: signs up, signs in again and links an existing account', | ||
| { skip: !databaseUrl }, | ||
| async () => { | ||
| assert.match(new URL(databaseUrl!).pathname, /test/i, 'Refusing to use a non-test database'); | ||
| process.env.DATABASE_URL = databaseUrl; | ||
| process.env.SKIP_ENV_VALIDATION = '1'; | ||
| process.env.NEXTAUTH_SECRET ??= 'apple-sign-in-test-secret'; | ||
| process.env.NEXTAUTH_URL ??= 'http://localhost:3000'; | ||
|
|
||
| const [{ db }, { env }, { signInWithApple }] = await Promise.all([ | ||
| import('~/server/db'), | ||
| import('~/env'), | ||
| import('~/server/mobileAuth'), | ||
| ]); | ||
| const mutableEnv = env as { -readonly [Key in keyof typeof env]: (typeof env)[Key] }; | ||
| const originalEnv = { ...env }; | ||
| mutableEnv.APPLE_BUNDLE_ID = BUNDLE_ID; | ||
| mutableEnv.NODE_ENV = 'production'; | ||
|
|
||
| // Stand in for Apple: our own key served as Apple's key set | ||
| const { publicKey, privateKey } = await generateKeyPair('RS256'); | ||
| const jwk = { ...(await exportJWK(publicKey)), kid: 'test', alg: 'RS256', use: 'sig' }; | ||
| const originalFetch = globalThis.fetch; | ||
| globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => | ||
| String(input).startsWith('https://appleid.apple.com/auth/keys') | ||
| ? new Response(JSON.stringify({ keys: [jwk] }), { | ||
| headers: { 'content-type': 'application/json' }, | ||
| }) | ||
| : originalFetch(input, init)) as typeof fetch; | ||
|
|
||
| const suffix = `${Date.now()}-${Math.round(Math.random() * 1e9)}`; | ||
| const identityToken = (sub: string, email: string) => | ||
| new SignJWT({ email, email_verified: 'true' }) | ||
| .setProtectedHeader({ alg: 'RS256', kid: 'test' }) | ||
| .setIssuer('https://appleid.apple.com') | ||
| .setAudience(BUNDLE_ID) | ||
| .setSubject(sub) | ||
| .setIssuedAt() | ||
| .setExpirationTime('5m') | ||
| .sign(privateKey); | ||
| const newEmail = `apple-new-${suffix}@example.com`; | ||
| const existing = await db.user.create({ | ||
| data: { email: `apple-existing-${suffix}@example.com`, name: 'Existing', currency: 'USD' }, | ||
| }); | ||
|
|
||
| try { | ||
| // First sign-in creates the account and links Apple to it | ||
| const first = await signInWithApple( | ||
| await identityToken(`sub-new-${suffix}`, newEmail), | ||
| { givenName: 'Ada', familyName: 'Lovelace' }, | ||
| `ip-${suffix}`, | ||
| ); | ||
| assert.ok(first.token); | ||
| const created = await db.user.findUnique({ where: { email: newEmail } }); | ||
| assert.equal(created?.name, 'Ada Lovelace'); | ||
| assert.equal(await db.account.count({ where: { provider: 'apple', userId: created.id } }), 1); | ||
|
|
||
| // Signing in again finds it through the linked Apple account | ||
| const again = await signInWithApple( | ||
| await identityToken(`sub-new-${suffix}`, newEmail), | ||
| null, | ||
| `ip-${suffix}`, | ||
| ); | ||
| assert.equal(again.user.email, newEmail); | ||
| assert.equal(await db.user.count({ where: { email: newEmail } }), 1); | ||
|
|
||
| // An Apple ID with the same verified email as an existing account signs in to it | ||
| const linked = await signInWithApple( | ||
| await identityToken(`sub-existing-${suffix}`, existing.email!), | ||
| null, | ||
| `ip-${suffix}`, | ||
| ); | ||
| assert.equal(linked.user.email, existing.email); | ||
| assert.equal( | ||
| await db.account.count({ where: { userId: existing.id, provider: 'apple' } }), | ||
| 1, | ||
| ); | ||
| } finally { | ||
| globalThis.fetch = originalFetch; | ||
| Object.assign(mutableEnv, originalEnv); | ||
| await db.user.deleteMany({ | ||
| where: { email: { in: [newEmail, existing.email!] } }, | ||
| }); | ||
| await db.$disconnect(); | ||
| } | ||
| }, | ||
| ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: oss-apps/split-pro-cloud
Length of output: 2515
Anchor the test database name check.
The guard at line 13 matches
/test/ianywhere in the URL pathname. A database such ascontestpasses the check and is used by the test. Require a delimited test segment instead, and keep acceptingsplitpro_testfrom CI.