1 parent
badf5f2c81
commit
aaddfc7bfc
44 files changed
+1331
-867
No files matched your search
@@ -50,7 +50,12 @@ export const useOAuthCallback = () => {
|
||||
// effects twice during local development, so exchange each code once.
|
||||
processedCodeRef.current = code;
|
||||
|
||||
void signInWithOAuth({ code }).catch((error) => {
|
||||
const redirectUri =
|
||||
typeof window !== 'undefined' ?
|
||||
`${window.location.origin}/api/auth/baron-sso/callback`
|
||||
: undefined;
|
||||
|
||||
void signInWithOAuth({ code, redirectUri }).catch((error) => {
|
||||
if (error instanceof AxiosError && error.response) {
|
||||
const message = error.response.data as IFetchError;
|
||||
toast.error(
|
||||
|
||||
@@ -13,127 +13,33 @@
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
import { useEffect } from 'react';
|
||||
import Image from 'next/image';
|
||||
import { useRouter } from 'next/router';
|
||||
import { useTranslation } from 'next-i18next';
|
||||
|
||||
import { Button } from '@ufb/react';
|
||||
|
||||
import { useOAIQuery } from '@/shared';
|
||||
import { useTenantStore } from '@/entities/tenant';
|
||||
|
||||
interface IProps {}
|
||||
|
||||
const OAUTH_CALLBACK_URL_STORAGE_KEY = 'ufb.oauth.callback-url';
|
||||
const OAUTH_FORCE_LOGIN_STORAGE_KEY = 'ufb.oauth.force-login';
|
||||
|
||||
const SignInWithOAuthButton: React.FC<IProps> = () => {
|
||||
const { t } = useTranslation();
|
||||
const router = useRouter();
|
||||
const { tenant } = useTenantStore();
|
||||
|
||||
const callback_url = (router.query.callback_url ?? '') as string;
|
||||
const force_login =
|
||||
typeof window !== 'undefined' ?
|
||||
(router.query.force_login ??
|
||||
window.sessionStorage.getItem(OAUTH_FORCE_LOGIN_STORAGE_KEY) ??
|
||||
'')
|
||||
: '';
|
||||
|
||||
const { data } = useOAIQuery({
|
||||
path: '/api/admin/auth/signIn/oauth/loginURL',
|
||||
queryOptions: { enabled: tenant?.useOAuth ?? false },
|
||||
variables: {
|
||||
callback_url,
|
||||
force_login: force_login ? 'true' : undefined,
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!force_login || !data?.url || typeof window === 'undefined') return;
|
||||
|
||||
if (callback_url) {
|
||||
window.sessionStorage.setItem(
|
||||
OAUTH_CALLBACK_URL_STORAGE_KEY,
|
||||
callback_url,
|
||||
);
|
||||
} else {
|
||||
window.sessionStorage.removeItem(OAUTH_CALLBACK_URL_STORAGE_KEY);
|
||||
}
|
||||
|
||||
window.sessionStorage.setItem(OAUTH_FORCE_LOGIN_STORAGE_KEY, 'true');
|
||||
window.location.assign(data.url);
|
||||
}, [callback_url, data?.url, force_login]);
|
||||
|
||||
if (tenant?.oauthConfig?.loginButtonType === 'GOOGLE') {
|
||||
return (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="medium"
|
||||
disabled={!data?.url}
|
||||
onClick={() => {
|
||||
if (typeof window !== 'undefined') {
|
||||
if (callback_url) {
|
||||
window.sessionStorage.setItem(
|
||||
OAUTH_CALLBACK_URL_STORAGE_KEY,
|
||||
callback_url,
|
||||
);
|
||||
} else {
|
||||
window.sessionStorage.removeItem(OAUTH_CALLBACK_URL_STORAGE_KEY);
|
||||
}
|
||||
if (force_login) {
|
||||
window.sessionStorage.setItem(
|
||||
OAUTH_FORCE_LOGIN_STORAGE_KEY,
|
||||
'true',
|
||||
);
|
||||
} else {
|
||||
window.sessionStorage.removeItem(OAUTH_FORCE_LOGIN_STORAGE_KEY);
|
||||
}
|
||||
window.location.assign(data?.url ?? '');
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Image
|
||||
src="/assets/images/google.svg"
|
||||
alt="Google"
|
||||
width={24}
|
||||
height={24}
|
||||
/>
|
||||
Google {t('button.sign-in')}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
const callbackUrl =
|
||||
typeof router.query.callback_url === 'string' ? router.query.callback_url : '';
|
||||
const loginUrl = `/api/auth/baron-sso/login${
|
||||
callbackUrl ? `?callback_url=${encodeURIComponent(callbackUrl)}` : ''
|
||||
}`;
|
||||
|
||||
return (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="medium"
|
||||
disabled={!data?.url}
|
||||
onClick={() => {
|
||||
if (typeof window !== 'undefined') {
|
||||
if (callback_url) {
|
||||
window.sessionStorage.setItem(
|
||||
OAUTH_CALLBACK_URL_STORAGE_KEY,
|
||||
callback_url,
|
||||
);
|
||||
} else {
|
||||
window.sessionStorage.removeItem(OAUTH_CALLBACK_URL_STORAGE_KEY);
|
||||
}
|
||||
if (force_login) {
|
||||
window.sessionStorage.setItem(
|
||||
OAUTH_FORCE_LOGIN_STORAGE_KEY,
|
||||
'true',
|
||||
);
|
||||
} else {
|
||||
window.sessionStorage.removeItem(OAUTH_FORCE_LOGIN_STORAGE_KEY);
|
||||
}
|
||||
window.location.assign(data?.url ?? '');
|
||||
}
|
||||
window.location.assign(loginUrl);
|
||||
}}
|
||||
>
|
||||
{tenant?.oauthConfig?.loginButtonName ??
|
||||
`OAuth 2.0 ${t('button.sign-in')}`}
|
||||
<Image src="/assets/images/google.svg" alt="" width={20} height={20} />
|
||||
BARON-SSO {t('button.sign-in')}
|
||||
</Button>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user