Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 470
feat(clerk-expo): Add native Apple Sign-In support for iOS#7053
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
05a7607f78267e4eb2f18b791faff643dfea3d83bd03235eefdb49e3f314e21669581a3fa5f8ff3a7d027e8cdecb6d1e20b78ff243575bfa3157caf811564c129064ad7de3b9fc3581676d1014ff0a778b4fdde762f9e55ff8d0181ea2d4011b71b3ab2774e676621b1ba03c4d1a4d70bed16e5ea5e36a6e7bb2cd20e523ecd27adb4c317b6839fde884ba628a3033576490e758b668f9a6aa397be3f34bccdfc1c4287a3File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@clerk/clerk-expo': minor | ||
| '@clerk/types': patch | ||
| --- | ||
| Add native Apple Sign-In support for iOS via `useAppleSignIn()` hook. Requires `expo-apple-authentication` and native build (EAS Build or local prebuild). |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| import { renderHook } from '@testing-library/react'; | ||
| import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; | ||
| import { useSignInWithApple } from '../useSignInWithApple.ios'; | ||
| const mocks = vi.hoisted(() => { | ||
| return { | ||
| useSignIn: vi.fn(), | ||
| useSignUp: vi.fn(), | ||
| signInAsync: vi.fn(), | ||
| isAvailableAsync: vi.fn(), | ||
| randomUUID: vi.fn(), | ||
| }; | ||
| }); | ||
| vi.mock('@clerk/clerk-react', () => { | ||
| return { | ||
| useSignIn: mocks.useSignIn, | ||
| useSignUp: mocks.useSignUp, | ||
| }; | ||
| }); | ||
| vi.mock('expo-apple-authentication', () => { | ||
| return { | ||
| signInAsync: mocks.signInAsync, | ||
| isAvailableAsync: mocks.isAvailableAsync, | ||
| AppleAuthenticationScope: { | ||
| FULL_NAME: 0, | ||
| EMAIL: 1, | ||
| }, | ||
| }; | ||
| }); | ||
| vi.mock('expo-crypto', () => { | ||
| return { | ||
| default: { | ||
| randomUUID: mocks.randomUUID, | ||
| }, | ||
| randomUUID: mocks.randomUUID, | ||
| }; | ||
| }); | ||
| vi.mock('react-native', () => { | ||
| return { | ||
| Platform: { | ||
| OS: 'ios', | ||
| }, | ||
| }; | ||
| }); | ||
| describe('useSignInWithApple', () => { | ||
| const mockSignIn = { | ||
| create: vi.fn(), | ||
| createdSessionId: 'test-session-id', | ||
| firstFactorVerification: { | ||
| status: 'verified', | ||
| }, | ||
| }; | ||
| const mockSignUp = { | ||
| create: vi.fn(), | ||
| createdSessionId: null, | ||
| }; | ||
| const mockSetActive = vi.fn(); | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mocks.useSignIn.mockReturnValue({ | ||
| signIn: mockSignIn, | ||
| setActive: mockSetActive, | ||
| isLoaded: true, | ||
| }); | ||
| mocks.useSignUp.mockReturnValue({ | ||
| signUp: mockSignUp, | ||
| isLoaded: true, | ||
| }); | ||
| mocks.isAvailableAsync.mockResolvedValue(true); | ||
| mocks.randomUUID.mockReturnValue('test-nonce-uuid'); | ||
| }); | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
| describe('startAppleAuthenticationFlow', () => { | ||
| test('should return the hook with startAppleAuthenticationFlow function', () => { | ||
| const { result } = renderHook(() => useSignInWithApple()); | ||
| expect(result.current).toHaveProperty('startAppleAuthenticationFlow'); | ||
| expect(typeof result.current.startAppleAuthenticationFlow).toBe('function'); | ||
| }); | ||
| test('should successfully sign in existing user', async () => { | ||
| const mockIdentityToken = 'mock-identity-token'; | ||
| mocks.signInAsync.mockResolvedValue({ | ||
| identityToken: mockIdentityToken, | ||
| }); | ||
| mockSignIn.create.mockResolvedValue(undefined); | ||
| mockSignIn.firstFactorVerification.status = 'verified'; | ||
| mockSignIn.createdSessionId = 'test-session-id'; | ||
| const { result } = renderHook(() => useSignInWithApple()); | ||
| const response = await result.current.startAppleAuthenticationFlow(); | ||
| expect(mocks.isAvailableAsync).toHaveBeenCalled(); | ||
| expect(mocks.randomUUID).toHaveBeenCalled(); | ||
| expect(mocks.signInAsync).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| requestedScopes: expect.any(Array), | ||
| nonce: 'test-nonce-uuid', | ||
| }), | ||
| ); | ||
| expect(mockSignIn.create).toHaveBeenCalledWith({ | ||
| strategy: 'oauth_token_apple', | ||
| token: mockIdentityToken, | ||
| }); | ||
| expect(response.createdSessionId).toBe('test-session-id'); | ||
| expect(response.setActive).toBe(mockSetActive); | ||
| }); | ||
| test('should handle transfer flow for new user', async () => { | ||
| const mockIdentityToken = 'mock-identity-token'; | ||
| mocks.signInAsync.mockResolvedValue({ | ||
| identityToken: mockIdentityToken, | ||
| }); | ||
| mockSignIn.create.mockResolvedValue(undefined); | ||
| mockSignIn.firstFactorVerification.status = 'transferable'; | ||
| const mockSignUpWithSession = { ...mockSignUp, createdSessionId: 'new-user-session-id' }; | ||
| mocks.useSignUp.mockReturnValue({ | ||
| signUp: mockSignUpWithSession, | ||
| isLoaded: true, | ||
| }); | ||
| const { result } = renderHook(() => useSignInWithApple()); | ||
| const response = await result.current.startAppleAuthenticationFlow({ | ||
| unsafeMetadata: { source: 'test' }, | ||
| }); | ||
| expect(mockSignIn.create).toHaveBeenCalledWith({ | ||
| strategy: 'oauth_token_apple', | ||
| token: mockIdentityToken, | ||
| }); | ||
| expect(mockSignUp.create).toHaveBeenCalledWith({ | ||
| transfer: true, | ||
| unsafeMetadata: { source: 'test' }, | ||
| }); | ||
| expect(response.createdSessionId).toBe('new-user-session-id'); | ||
| }); | ||
| test('should handle user cancellation gracefully', async () => { | ||
| const cancelError = Object.assign(new Error('User canceled'), { code: 'ERR_REQUEST_CANCELED' }); | ||
| mocks.signInAsync.mockRejectedValue(cancelError); | ||
| const { result } = renderHook(() => useSignInWithApple()); | ||
| const response = await result.current.startAppleAuthenticationFlow(); | ||
| expect(response.createdSessionId).toBe(null); | ||
| expect(response.setActive).toBe(mockSetActive); | ||
| }); | ||
| test('should throw error when Apple Authentication is not available', async () => { | ||
| mocks.isAvailableAsync.mockResolvedValue(false); | ||
| const { result } = renderHook(() => useSignInWithApple()); | ||
| await expect(result.current.startAppleAuthenticationFlow()).rejects.toThrow( | ||
| 'Apple Authentication is not available on this device.', | ||
| ); | ||
| }); | ||
| test('should throw error when no identity token received', async () => { | ||
| mocks.signInAsync.mockResolvedValue({ | ||
| identityToken: null, | ||
| }); | ||
| const { result } = renderHook(() => useSignInWithApple()); | ||
| await expect(result.current.startAppleAuthenticationFlow()).rejects.toThrow( | ||
| 'No identity token received from Apple Sign-In.', | ||
| ); | ||
| }); | ||
| test('should return early when clerk is not loaded', async () => { | ||
| mocks.useSignIn.mockReturnValue({ | ||
| signIn: mockSignIn, | ||
| setActive: mockSetActive, | ||
| isLoaded: false, | ||
| }); | ||
| const { result } = renderHook(() => useSignInWithApple()); | ||
| const response = await result.current.startAppleAuthenticationFlow(); | ||
| expect(mocks.isAvailableAsync).not.toHaveBeenCalled(); | ||
| expect(mocks.signInAsync).not.toHaveBeenCalled(); | ||
| expect(response.createdSessionId).toBe(null); | ||
| }); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.