-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds new session management and way to get authenticated client via r…
…eact-sdk
- Loading branch information
1 parent
c15023c
commit 3447581
Showing
7 changed files
with
150 additions
and
9 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { StorageKeys, User } from "@turnkey/sdk-browser"; | ||
import { useSessionStorage } from "usehooks-ts"; | ||
|
||
interface UserSession { | ||
user?: Omit<User, "session"> | undefined; | ||
session?: User["session"]; | ||
} | ||
|
||
/** | ||
* Hook for managing the user session stored in local storage. | ||
* This hook is reactive and updates whenever the value in local storage changes. | ||
* | ||
* @returns {UserSession | null} An object containing user details and session information. | ||
* | ||
* @example | ||
* const { user, session } = useUserSession() | ||
* | ||
* if (user) { | ||
* // user is defined and thus has previously logged in | ||
* } else { | ||
* // no user found in local storage | ||
* } | ||
* | ||
* if (session?.read) { | ||
* // session.read is defined therefore user is authenticated with a read session | ||
* if (session.expiry && Date.now() < session.expiry) { | ||
* // Session is still valid | ||
* } | ||
* } | ||
* | ||
* if (session?.write) { | ||
* // session.write is defined therefore user is authenticated with a read/write session | ||
* if (session.expiry && Date.now() < session.expiry) { | ||
* // Session is still valid | ||
* } | ||
* } | ||
* | ||
* if (!session) { | ||
* // no session, user is not authenticated | ||
* } | ||
*/ | ||
export function useUserSession(): UserSession { | ||
// Use the StorageKeys.UserSession key to manage session storage | ||
const [user] = useSessionStorage<User | undefined>( | ||
StorageKeys.UserSession, | ||
undefined | ||
); | ||
|
||
// Destructure user object to separate session from other user details | ||
const { session, userId, username, organization } = user ?? {}; | ||
|
||
// Return the structured object with separated user details and session | ||
return { | ||
user: | ||
userId && organization | ||
? { | ||
userId: userId ?? "", | ||
username: username ?? "", | ||
organization: organization ?? "", | ||
} | ||
: undefined, | ||
session: session ?? undefined, | ||
}; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.