tachybase_todo/packages/plugins/@nocobase/plugin-saml/src/client/SAMLButton.tsx
YANG QIA 06f11a2d08
refactor(auth): move auth client from core to the plugin & refactor auth client api (#3215)
* refactor(auth): auth client api

* fix: build

* fix: dependencies

* fix: fix T-2777

* fix: fix T-2776

* chore: update type

* fix: build

* fix: allowSignUp

* fix: file name

* fix: file name

* refactor: client api

* fix: build

* chore: update name

* fix: tsx must be loaded with --import instead of --loader

* fix: type

* fix: type

* fix: type

* fix: type

* fix: bug

* chore: improve wording

* fix: test

---------

Co-authored-by: chenos <chenlinxh@gmail.com>
2023-12-21 20:19:25 +08:00

53 lines
1.3 KiB
TypeScript

import { LoginOutlined } from '@ant-design/icons';
import { css, useAPIClient } from '@nocobase/client';
import { Button, Space, message } from 'antd';
import React, { useEffect } from 'react';
import { useSamlTranslation } from './locale';
import { useLocation } from 'react-router-dom';
import { Authenticator } from '@nocobase/plugin-auth/client';
export const SAMLButton = ({ authenticator }: { authenticator: Authenticator }) => {
const { t } = useSamlTranslation();
const api = useAPIClient();
const location = useLocation();
const login = async () => {
const response = await api.request({
method: 'post',
url: 'saml:getAuthUrl',
headers: {
'X-Authenticator': authenticator.name,
},
});
const authUrl = response?.data?.data;
window.location.replace(authUrl);
};
useEffect(() => {
const params = new URLSearchParams(location.search);
const name = params.get('authenticator');
const error = params.get('error');
if (name !== authenticator.name) {
return;
}
if (error) {
message.error(error);
return;
}
});
return (
<Space
direction="vertical"
className={css`
display: flex;
`}
>
<Button shape="round" block icon={<LoginOutlined />} onClick={login}>
{t(authenticator.title)}
</Button>
</Space>
);
};