-
Notifications
You must be signed in to change notification settings - Fork 14
Feature: Added useDirectusAuthHook for State-Based Authentication #364
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
13 commits
Select commit
Hold shift + click to select a range
e6f18cb
initial test hook
Sakuk3 61dac52
Added TSDoc to types
Sakuk3 07b4889
More TSDoc
Sakuk3 660e61b
better typing
Sakuk3 6d8d71b
make useDirectusAuth importable
Sakuk3 ef53797
rename useDirectusAuth
Sakuk3 f66f873
created seperate hooks folder
Sakuk3 bf4243a
fixed imports
Sakuk3 2cff643
rem redundant directus and apiUrl
Sakuk3 f75184c
Update useDirectusAuth.tsx
gremo b1e5ea4
added hook to README
Sakuk3 238e775
Update README.md
gremo 62727e1
Update README.md
gremo 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
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,95 @@ | ||
| import { DirectusAuthHook } from '../types'; | ||
| import { DirectusContext } from '../DirectusProvider'; | ||
| import React from 'react'; | ||
| import { UserType } from '@directus/sdk'; | ||
|
|
||
| /** | ||
| * A hook to access the Directus authentication state and methods. | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * import { useDirectusAuth } from 'react-directus'; | ||
| * | ||
| * const Login = () => { | ||
| * const { login } = useDirectusAuth(); | ||
| * | ||
| * const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => { | ||
| * e.preventDefault(); | ||
| * | ||
| * const { email, password } = e.currentTarget.elements; | ||
| * login(email.value, password.value) | ||
| * .catch((err) => { | ||
| * console.error(err); | ||
| * }); | ||
| * }; | ||
| * | ||
| * return ( | ||
| * <form onSubmit={handleSubmit}> | ||
| * <input type="email" name="email" /> | ||
| * <input type="password" name="password" /> | ||
| * <button type="submit">Login</button> | ||
| * </form> | ||
| * ); | ||
| * }; | ||
| * | ||
| * export default Login; | ||
| * ``` | ||
| */ | ||
| export const useDirectusAuth = (): DirectusAuthHook => { | ||
| const directusContext = React.useContext(DirectusContext); | ||
|
|
||
| if (!directusContext) { | ||
| throw new Error('useDirectusAuth has to be used within the DirectusProvider'); | ||
| } | ||
|
|
||
| const { | ||
| directus, | ||
| _authState: authState, | ||
| _setAuthState: setAuthState, | ||
| _directusUser: directusUser, | ||
| _setDirecctusUser: setDirectusUser, | ||
| } = directusContext; | ||
|
|
||
| const login = React.useCallback<DirectusAuthHook['login']>( | ||
| async (email: string, password: string) => { | ||
| await directus.auth.login({ | ||
| email, | ||
| password, | ||
| }); | ||
|
|
||
| const dUser = (await directus.users.me.read({ | ||
| fields: ['*'], | ||
| })) as UserType; | ||
|
|
||
| if (dUser) { | ||
| setDirectusUser(dUser); | ||
| setAuthState('authenticated'); | ||
gremo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| setDirectusUser(null); | ||
| setAuthState('unauthenticated'); | ||
| } | ||
| }, | ||
| [directus] | ||
| ); | ||
|
|
||
| const logout = React.useCallback<DirectusAuthHook['logout']>(async () => { | ||
| try { | ||
| await directus.auth.logout(); | ||
| } finally { | ||
| setAuthState('unauthenticated'); | ||
| setDirectusUser(null); | ||
| } | ||
| }, [directus]); | ||
|
|
||
| const value = React.useMemo<DirectusAuthHook>( | ||
| () => ({ | ||
| user: directusUser, | ||
| authState, | ||
| login, | ||
| logout, | ||
| }), | ||
| [directus, directusUser, authState] | ||
| ); | ||
|
|
||
| return value; | ||
| }; | ||
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export { DirectusProvider, useDirectus } from '@/DirectusProvider'; | ||
| export { DirectusAsset } from '@components/DirectusAsset'; | ||
| export { DirectusImage } from '@components/DirectusImage'; | ||
| export { useDirectusAuth } from '@hooks/useDirectusAuth'; |
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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| "paths": { | ||
| "@/*": ["*"], | ||
| "@components/*": ["components/*"], | ||
| "@hooks/*": ["hooks/*"] | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.