♻️(front) add option to disable silent login
When displayed in an iframe, not being logged-in causes a redirection to the home page. Which is not what we want with the SDK integration. We just want to stay on the same page.
This commit is contained in:
@@ -10,7 +10,13 @@ import { attemptSilentLogin, canAttemptSilentLogin } from '../utils/silentLogin'
|
||||
* Here our wrapper just returns false in that case, without triggering an error:
|
||||
* this is done to prevent unnecessary query retries with react query
|
||||
*/
|
||||
export const fetchUser = (): Promise<ApiUser | false> => {
|
||||
export const fetchUser = (
|
||||
opts: {
|
||||
attemptSilent?: boolean
|
||||
} = {
|
||||
attemptSilent: true,
|
||||
}
|
||||
): Promise<ApiUser | false> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
fetchApi<ApiUser>('/users/me')
|
||||
.then(resolve)
|
||||
@@ -19,7 +25,7 @@ export const fetchUser = (): Promise<ApiUser | false> => {
|
||||
if (error instanceof ApiError && error.statusCode === 401) {
|
||||
// make sure to not resolve the promise while trying to silent login
|
||||
// so that consumers of fetchUser don't think the work already ended
|
||||
if (canAttemptSilentLogin()) {
|
||||
if (opts.attemptSilent && canAttemptSilentLogin()) {
|
||||
attemptSilentLogin(300)
|
||||
} else {
|
||||
resolve(false)
|
||||
|
||||
@@ -11,10 +11,15 @@ import { initializeSupportSession } from '@/features/support/hooks/useSupport'
|
||||
*
|
||||
* `isLoggedIn` is undefined while query is loading and true/false when it's done
|
||||
*/
|
||||
export const useUser = () => {
|
||||
export const useUser = (
|
||||
opts: {
|
||||
fetchUserOptions?: Parameters<typeof fetchUser>[0]
|
||||
} = {}
|
||||
) => {
|
||||
const query = useQuery({
|
||||
// eslint-disable-next-line @tanstack/query/exhaustive-deps
|
||||
queryKey: [keys.user],
|
||||
queryFn: fetchUser,
|
||||
queryFn: () => fetchUser(opts.fetchUserOptions),
|
||||
staleTime: Infinity,
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user