ExternalAuthButton.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { useCallback } from 'react';
  2. import { IExternalAuthProviderType } from '@growi/core';
  3. import { useTranslation } from 'next-i18next';
  4. const authIcon = {
  5. [IExternalAuthProviderType.google]: <span className="growi-custom-icons align-bottom">google</span>,
  6. [IExternalAuthProviderType.github]: <span className="growi-custom-icons align-bottom">github</span>,
  7. [IExternalAuthProviderType.facebook]: <span className="growi-custom-icons align-bottom">facebook</span>,
  8. [IExternalAuthProviderType.oidc]: <span className="growi-custom-icons align-bottom">openid</span>,
  9. [IExternalAuthProviderType.saml]: <span className="material-symbols-outlined align-bottom">key</span>,
  10. };
  11. const authLabel = {
  12. [IExternalAuthProviderType.google]: 'Google',
  13. [IExternalAuthProviderType.github]: 'GitHub',
  14. [IExternalAuthProviderType.facebook]: 'Facebook',
  15. [IExternalAuthProviderType.oidc]: 'OIDC',
  16. [IExternalAuthProviderType.saml]: 'SAML',
  17. };
  18. export const ExternalAuthButton = ({ authType }: {authType: IExternalAuthProviderType}): JSX.Element => {
  19. const { t } = useTranslation();
  20. const key = `btn-auth-${authType.toString()}`;
  21. const btnClass = `btn-auth-${authType.toString()}`;
  22. const handleLoginWithExternalAuth = useCallback(() => {
  23. window.location.href = `/passport/${authType.toString()}`;
  24. }, [authType]);
  25. return (
  26. <button
  27. key={key}
  28. type="button"
  29. className={`btn btn-secondary ${btnClass} my-2 col-10 col-sm-7 mx-auto d-flex`}
  30. onClick={handleLoginWithExternalAuth}
  31. >
  32. <span>{authIcon[authType]}</span>
  33. <span className="flex-grow-1">{t('Sign in with External auth', { signin: authLabel[authType] })}</span>
  34. </button>
  35. );
  36. };