-
Notifications
You must be signed in to change notification settings - Fork 283
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(clerk-expo): Introduce support for LocalAuth with LocalCredentials #3663
Merged
Merged
Changes from 21 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
5d0aeef
feat(clerk-expo): Introduce LocalAuth utility
panteliselef d93018f
chore(clerk-expo): Update package.json
panteliselef f194c8b
chore(clerk-js): Add LocalAuthCredentials
panteliselef c2a1425
chore(clerk-expo): Rename to LocalCredentials
panteliselef d01daf8
feat(clerk-expo): Expose biometricType
panteliselef 0a06840
add react as devDep
panteliselef 4215762
add changeset
panteliselef e80425d
Merge branch 'refs/heads/main' into elef/user-119-local-auth-for-expo
panteliselef ecce02d
add package-lock.json
panteliselef 84edb49
use correct react version for expo
panteliselef e946474
drop react-native-mmkv
panteliselef a57936f
Merge branch 'main' into elef/user-119-local-auth-for-expo
panteliselef 7a5af17
Merge branch 'main' into elef/user-119-local-auth-for-expo
panteliselef 0cb780e
feat(clerk-expo): Add `userOwnsCredentials`
panteliselef 388877a
Mark deps as optional
panteliselef f323216
feat(clerk-expo): Support SDK 50
panteliselef dfccb21
Merge branch 'refs/heads/main' into elef/user-119-local-auth-for-expo
panteliselef a184992
revert husky deletion
panteliselef 2b021d2
update changeset
panteliselef 086e892
Drop provider and add web support
panteliselef ff0337d
Merge branch 'refs/heads/main' into elef/user-119-local-auth-for-expo
panteliselef c7fddbf
Prioritize face recognition over fingerprint
panteliselef 4c2a0f8
Add jsdoc
panteliselef 9e83bbe
Merge branch 'refs/heads/main' into elef/user-119-local-auth-for-expo
panteliselef 76706a0
Update packages/expo/src/hooks/useLocalCredentials/useLocalCredential…
panteliselef 9b7a409
update error
panteliselef 35e1326
Update packages/expo/src/hooks/useLocalCredentials/useLocalCredential…
panteliselef 6b6308d
Merge branch 'refs/heads/main' into elef/user-119-local-auth-for-expo
panteliselef 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 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,5 @@ | ||
--- | ||
"@clerk/clerk-expo": minor | ||
--- | ||
|
||
Introduce support for LocalAuthentication with `useLocalCredentials`. |
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
|
@@ -12,3 +12,4 @@ export { | |
} from '@clerk/clerk-react'; | ||
|
||
export * from './useOAuth'; | ||
export * from './useLocalCredentials'; |
This file contains 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 @@ | ||
export { useLocalCredentials } from './useLocalCredentials'; |
This file contains 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,31 @@ | ||
import type { SignInResource } from '@clerk/types'; | ||
|
||
type LocalCredentials = { | ||
identifier?: string; | ||
password: string; | ||
}; | ||
|
||
type BiometricType = 'fingerprint' | 'face-recognition'; | ||
|
||
type LocalCredentialsReturn = { | ||
setCredentials: (creds: LocalCredentials) => Promise<void>; | ||
hasCredentials: boolean; | ||
userOwnsCredentials: boolean | null; | ||
clearCredentials: () => Promise<void>; | ||
authenticate: () => Promise<SignInResource>; | ||
biometryType: BiometricType | null; | ||
}; | ||
|
||
const LocalCredentialsInitValues: LocalCredentialsReturn = { | ||
setCredentials: () => Promise.resolve(), | ||
hasCredentials: false, | ||
userOwnsCredentials: null, | ||
clearCredentials: () => Promise.resolve(), | ||
// @ts-expect-error Initial value cannot return what the type expects | ||
authenticate: () => Promise.resolve({}), | ||
biometryType: null, | ||
}; | ||
|
||
export { LocalCredentialsInitValues }; | ||
|
||
export type { LocalCredentials, BiometricType, LocalCredentialsReturn }; |
183 changes: 183 additions & 0 deletions
183
packages/expo/src/hooks/useLocalCredentials/useLocalCredentials.ts
This file contains 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,183 @@ | ||
import { useClerk, useSignIn, useUser } from '@clerk/clerk-react'; | ||
import { AuthenticationType, isEnrolledAsync, supportedAuthenticationTypesAsync } from 'expo-local-authentication'; | ||
import { | ||
deleteItemAsync, | ||
getItem, | ||
getItemAsync, | ||
setItemAsync, | ||
WHEN_PASSCODE_SET_THIS_DEVICE_ONLY, | ||
} from 'expo-secure-store'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { errorThrower } from '../../utils'; | ||
import type { BiometricType, LocalCredentials, LocalCredentialsReturn } from './shared'; | ||
|
||
const useEnrolledBiometric = () => { | ||
const [isEnrolled, setIsEnrolled] = useState(false); | ||
|
||
useEffect(() => { | ||
let ignore = false; | ||
|
||
void isEnrolledAsync().then(res => { | ||
if (ignore) { | ||
return; | ||
} | ||
setIsEnrolled(res); | ||
}); | ||
|
||
return () => { | ||
ignore = true; | ||
}; | ||
}, []); | ||
|
||
return isEnrolled; | ||
}; | ||
|
||
const useAuthenticationType = () => { | ||
const [authenticationType, setAuthenticationType] = useState<BiometricType | null>(null); | ||
|
||
useEffect(() => { | ||
let ignore = false; | ||
|
||
void supportedAuthenticationTypesAsync().then(numericTypes => { | ||
if (ignore) { | ||
return; | ||
} | ||
if (numericTypes.length === 0) { | ||
return; | ||
} | ||
|
||
if (numericTypes.includes(AuthenticationType.FINGERPRINT)) { | ||
setAuthenticationType('fingerprint'); | ||
} else { | ||
setAuthenticationType('face-recognition'); | ||
} | ||
}); | ||
|
||
return () => { | ||
ignore = true; | ||
}; | ||
}, []); | ||
|
||
return authenticationType; | ||
}; | ||
|
||
const useUserOwnsCredentials = ({ storeKey }: { storeKey: string }) => { | ||
const { user } = useUser(); | ||
const [userOwnsCredentials, setUserOwnsCredentials] = useState(false); | ||
|
||
const getUserCredentials = (storedIdentifier: string | null): boolean => { | ||
if (!user || !storedIdentifier) { | ||
return false; | ||
} | ||
|
||
const identifiers = [ | ||
user.emailAddresses.map(e => e.emailAddress), | ||
user.phoneNumbers.map(p => p.phoneNumber), | ||
].flat(); | ||
|
||
if (user.username) { | ||
identifiers.push(user.username); | ||
} | ||
return identifiers.includes(storedIdentifier); | ||
}; | ||
|
||
useEffect(() => { | ||
let ignore = false; | ||
getItemAsync(storeKey) | ||
.catch(() => null) | ||
.then(res => { | ||
if (ignore) { | ||
return; | ||
} | ||
setUserOwnsCredentials(getUserCredentials(res)); | ||
}); | ||
|
||
return () => { | ||
ignore = true; | ||
}; | ||
}, [storeKey, user]); | ||
|
||
return [userOwnsCredentials, setUserOwnsCredentials] as const; | ||
}; | ||
|
||
export const useLocalCredentials = (): LocalCredentialsReturn => { | ||
panteliselef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const { isLoaded, signIn } = useSignIn(); | ||
const { publishableKey } = useClerk(); | ||
|
||
const key = `__clerk_local_auth_${publishableKey}_identifier`; | ||
const pkey = `__clerk_local_auth_${publishableKey}_password`; | ||
const [hasLocalAuthCredentials, setHasLocalAuthCredentials] = useState(!!getItem(key)); | ||
const [userOwnsCredentials, setUserOwnsCredentials] = useUserOwnsCredentials({ storeKey: key }); | ||
const hasEnrolledBiometric = useEnrolledBiometric(); | ||
const authenticationType = useAuthenticationType(); | ||
|
||
const biometryType = hasEnrolledBiometric ? authenticationType : null; | ||
|
||
const setCredentials = async (creds: LocalCredentials) => { | ||
if (!(await isEnrolledAsync())) { | ||
return; | ||
} | ||
|
||
if (creds.identifier && !creds.password) { | ||
return errorThrower.throw( | ||
`useLocalCredentials: setCredentials() A password is required when specifying an identifier.`, | ||
); | ||
} | ||
|
||
if (creds.identifier) { | ||
await setItemAsync(key, creds.identifier); | ||
} | ||
|
||
const storedIdentifier = await getItemAsync(key).catch(() => null); | ||
|
||
if (!storedIdentifier) { | ||
return errorThrower.throw( | ||
`useLocalCredentials: setCredentials() an identifier should already be set in order to update its password.`, | ||
); | ||
} | ||
|
||
setHasLocalAuthCredentials(true); | ||
await setItemAsync(pkey, creds.password, { | ||
keychainAccessible: WHEN_PASSCODE_SET_THIS_DEVICE_ONLY, | ||
requireAuthentication: true, | ||
}); | ||
}; | ||
|
||
const clearCredentials = async () => { | ||
await Promise.all([deleteItemAsync(key), deleteItemAsync(pkey)]); | ||
panteliselef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setHasLocalAuthCredentials(false); | ||
setUserOwnsCredentials(false); | ||
}; | ||
|
||
const authenticate = async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 It would be nice to (optionally?) set the credentials after a successful auth |
||
if (!isLoaded) { | ||
return errorThrower.throw( | ||
`useLocalCredentials: authenticate() Clerk has not loaded yet. Wait for clerk to load before calling this function`, | ||
); | ||
} | ||
const identifier = await getItemAsync(key).catch(() => null); | ||
if (!identifier) { | ||
return errorThrower.throw(`useLocalCredentials: authenticate() the identifier could not be found`); | ||
} | ||
const password = await getItemAsync(pkey).catch(() => null); | ||
|
||
if (!password) { | ||
return errorThrower.throw(`useLocalCredentials: authenticate() cannot retrieve a password for that identifier`); | ||
} | ||
panteliselef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return signIn.create({ | ||
strategy: 'password', | ||
identifier, | ||
password, | ||
}); | ||
}; | ||
return { | ||
panteliselef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setCredentials, | ||
hasCredentials: hasLocalAuthCredentials, | ||
userOwnsCredentials, | ||
clearCredentials, | ||
authenticate, | ||
biometryType, | ||
panteliselef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
}; |
6 changes: 6 additions & 0 deletions
6
packages/expo/src/hooks/useLocalCredentials/useLocalCredentials.web.ts
This file contains 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,6 @@ | ||
import type { LocalCredentialsReturn } from './shared'; | ||
import { LocalCredentialsInitValues } from './shared'; | ||
|
||
export const useLocalCredentials = (): LocalCredentialsReturn => { | ||
return LocalCredentialsInitValues; | ||
}; |
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.
❓ Should we have something like
isPlatformSupported
inLocalCredentialsInitValues
in order to be easier to check if the hook can be used on specific platforms like Web or a device that does not has any biometric support, as now it seems you will have to check ifbiometryType
is null which is not so clear if you don't take a look at the codeThere 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.
For devices that do not support biometric, android and iOS fallback to PIN, and if a pin does not exist then
biometricType
will be null.with biometricType you get the available authenticator and it is useful mostly for UI purposes. Displaying a Face ID or a touch ID icon for example.
Maybe simply changing the name to
supportedBiometricType
would do ?We don't wanna indicate that the device can use biometric, but instead if we can use local authentication (biometric or not) in order to store credentials.
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.
I was mostly referring for platforms that this will not work at all like web,the biometric support was just an example as I didn't know if this fallbacks to pin