-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #904 from lens-protocol/T-19860/useRefreshToken
feat: adds useRefreshToken experimental hook
- Loading branch information
Showing
7 changed files
with
80 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@lens-protocol/react": patch | ||
"@lens-protocol/react-native": patch | ||
"@lens-protocol/react-web": patch | ||
--- | ||
|
||
**feat:** adds `useRefreshToken` experimental hook |
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,70 @@ | ||
import { useMemo, useSyncExternalStore } from 'react'; | ||
|
||
import { CredentialsStorage } from '../authentication/adapters/CredentialsStorage'; | ||
import { JwtCredentials } from '../authentication/adapters/JwtCredentials'; | ||
import { useSharedDependencies } from '../shared'; | ||
|
||
type Callback = () => void; | ||
|
||
class ExternalStorageAdapter { | ||
private cache: JwtCredentials | null = null; | ||
|
||
constructor(private readonly credentialsStorage: CredentialsStorage) {} | ||
|
||
subscribe = (cb: Callback): Callback => { | ||
const subscription = this.credentialsStorage.subscribe((credentials) => { | ||
this.cache = credentials; | ||
cb(); | ||
}); | ||
return () => subscription.unsubscribe(); | ||
}; | ||
|
||
get = () => this.cache; | ||
} | ||
|
||
function useCredentials() { | ||
const { credentialsStorage } = useSharedDependencies(); | ||
const adapter = useMemo( | ||
() => new ExternalStorageAdapter(credentialsStorage), | ||
[credentialsStorage], | ||
); | ||
const credentials = useSyncExternalStore(adapter.subscribe, adapter.get); | ||
|
||
return credentials; | ||
} | ||
|
||
/** | ||
* Returns the current access token. | ||
* | ||
* @experimental This API is VERY experimental and might change in the future. | ||
* @defaultValue `null` if not authenticated. | ||
*/ | ||
export function useAccessToken() { | ||
const credentials = useCredentials(); | ||
|
||
return credentials?.accessToken ?? null; | ||
} | ||
|
||
/** | ||
* Returns the current identity token. | ||
* | ||
* @experimental This API is VERY experimental and might change in the future. | ||
* @defaultValue `null` if not authenticated. | ||
*/ | ||
export function useIdentityToken() { | ||
const credentials = useCredentials(); | ||
|
||
return credentials?.identityToken ?? null; | ||
} | ||
|
||
/** | ||
* Returns the current refresh token. | ||
* | ||
* @experimental This API is VERY experimental and might change in the future. | ||
* @defaultValue `null` if not authenticated. | ||
*/ | ||
export function useRefreshToken() { | ||
const credentials = useCredentials(); | ||
|
||
return credentials?.refreshToken ?? null; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './useAccessToken'; | ||
export * from './credentials'; | ||
export * from './useApolloClient'; | ||
export * from './useIdentityToken'; | ||
export * from './useStorage'; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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