-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoginForm.tsx
More file actions
133 lines (121 loc) · 4.37 KB
/
LoginForm.tsx
File metadata and controls
133 lines (121 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import React, { useCallback, useEffect, useState } from 'react';
import { Link, useNavigate, useSearchParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { BaseForm } from '@app/components/common/forms/BaseForm/BaseForm';
import { useAppDispatch, useAppSelector } from '@app/hooks/reduxHooks';
import { doLogin } from '@app/store/slices/authSlice';
import { useFeedback } from '@app/hooks/useFeedback';
import GoogleIcon from '@app/assets/icons/google.svg';
import * as S from './LoginForm.styles';
import * as Auth from '@app/components/layouts/AuthLayout/AuthLayout.styles';
import { useResponsive } from '@app/hooks/useResponsive';
import {
BITBUCKET_OAUTH2_URL,
GITHUB_OAUTH_URL,
GOOGLE_OAUTH_URL,
handleOauth2Submit,
handleOauthSubmit,
} from '@app/constants/oauthHandler';
import { GithubOutlined } from '@ant-design/icons';
import { BitBucketOutlined } from '@app/components/dashboard/common/VendorDropdown/vendorIcons/VendorIcons';
interface LoginFormData {
email: string;
password: string;
}
export const LoginForm: React.FC = () => {
const navigate = useNavigate();
const dispatch = useAppDispatch();
const { t } = useTranslation();
const { notification } = useFeedback();
const { isDesktop } = useResponsive();
const user = useAppSelector((state) => state.user.user);
const token = useAppSelector((state) => state.auth.token);
const [searchParams] = useSearchParams();
const pageState = searchParams.get('state');
const [isLoading, setLoading] = useState(false);
useEffect(() => {
if (!pageState && user) {
navigate('/');
} else if (pageState && user) {
window.location.href = `${pageState}?token=${token}`;
navigate('/');
}
}, [navigate, pageState, token, user]);
const handleSubmit = useCallback(
async (values: LoginFormData) => {
setLoading(true);
try {
const token = await dispatch(doLogin(values)).unwrap();
if (pageState) {
window.location.href = `${pageState}?token=${token}`;
}
navigate('/');
} catch (err) {
notification.error({ message: err instanceof Error ? err.message : 'Login failed' });
} finally {
setLoading(false);
}
},
[dispatch, navigate, notification, pageState]
);
const handleGoogleLogin = useCallback(
async () => handleOauthSubmit({}, GOOGLE_OAUTH_URL, pageState ? false : isDesktop),
[isDesktop, pageState]
);
const handleGithubLogin = useCallback(
async () => handleOauthSubmit({}, GITHUB_OAUTH_URL, pageState ? false : isDesktop),
[isDesktop, pageState]
);
const handleBitbucketLogin = useCallback(
async () => handleOauth2Submit(BITBUCKET_OAUTH2_URL, pageState ? false : isDesktop),
[isDesktop, pageState]
);
return (
<Auth.FormWrapper>
<BaseForm
layout="vertical"
onFinish={handleSubmit}
requiredMark="optional"
initialValues={{
email: '',
password: '',
}}
>
<Auth.FormTitle style={{textAlign: "center"}}>{t('auth.common.login')}</Auth.FormTitle>
<hr></hr>
<BaseForm.Item noStyle>
<Auth.SocialButton type="default" htmlType="button" onClick={handleGoogleLogin}>
<Auth.SocialIconWrapper>
<GoogleIcon />
</Auth.SocialIconWrapper>
{t('auth.login.googleLink')}
</Auth.SocialButton>
</BaseForm.Item>
<BaseForm.Item noStyle>
<Auth.SocialButton type="default" htmlType="button" onClick={handleGithubLogin}>
<Auth.SocialIconWrapper>
<GithubOutlined />
</Auth.SocialIconWrapper>
{t('auth.login.githubLink')}
</Auth.SocialButton>
</BaseForm.Item>
<BaseForm.Item noStyle>
<Auth.SocialButton type="default" htmlType="button" onClick={handleBitbucketLogin}>
<Auth.SocialIconWrapper>
<BitBucketOutlined />
</Auth.SocialIconWrapper>
{t('auth.login.bitbucketLink')}
</Auth.SocialButton>
</BaseForm.Item>
<Auth.FooterWrapper>
<Auth.Text>
{t('auth.login.noAccount')}{' '}
<Link to="/auth/sign-up">
<Auth.LinkText>{t('auth.common.here')}</Auth.LinkText>
</Link>
</Auth.Text>
</Auth.FooterWrapper>
</BaseForm>
</Auth.FormWrapper>
);
};