Skip to content

Commit 160e91e

Browse files
committed
Fixes show login form's provider
1 parent be6b515 commit 160e91e

File tree

3 files changed

+78
-31
lines changed

3 files changed

+78
-31
lines changed

src/app/auth/LoginForm.tsx

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,75 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22

33
import { Box, Button, Stack, Text } from '@chakra-ui/react';
44
import { signIn } from 'next-auth/react';
55
import { useTranslation } from 'react-i18next';
6-
import { FiGithub, FiInfo } from 'react-icons/fi';
6+
import { FiGithub, FiGitlab, FiInfo } from 'react-icons/fi';
77

88
import { Icon } from '@/components';
99

10-
export const LoginForm = ({ onSuccess = () => undefined, ...rest }: TODO) => {
10+
export const LoginForm = ({
11+
onSuccess = () => undefined,
12+
provider,
13+
...rest
14+
}: TODO) => {
1115
const { t } = useTranslation();
16+
useEffect(() => {
17+
if (provider === undefined) {
18+
provider = 'github';
19+
}
20+
}, []);
1221

1322
return (
14-
<Stack {...rest}>
15-
<Button
16-
w="full"
17-
colorScheme="github"
18-
bg="github.800"
19-
color="white"
20-
leftIcon={<FiGithub />}
21-
ms="auto"
22-
onClick={() => signIn('github')}
23-
>
24-
{t('auth:login.actions.github.login')}
25-
</Button>
26-
<Box borderRadius="md" bg="github.100" color="github.800" p="4">
27-
<Text>
28-
<Icon icon={FiInfo} /> Only users from{' '}
29-
<Text as="span" fontWeight="medium">
30-
{process.env.NEXT_PUBLIC_GITHUB_ALLOWED_ORGANIZATIONS}
31-
</Text>{' '}
32-
GitHub organisation(s) will be able to use the application once logged
33-
in.
34-
</Text>
35-
</Box>
36-
</Stack>
23+
<>
24+
{provider === 'github' ? (
25+
<Stack {...rest}>
26+
<Button
27+
w="full"
28+
colorScheme="github"
29+
bg="github.800"
30+
color="white"
31+
leftIcon={<FiGithub />}
32+
ms="auto"
33+
onClick={() => signIn('github')}
34+
>
35+
{t('auth:login.actions.github.login')}
36+
</Button>
37+
<Box borderRadius="md" bg="github.100" color="github.800" p="4">
38+
<Text>
39+
<Icon icon={FiInfo} /> Only users from{' '}
40+
<Text as="span" fontWeight="medium">
41+
{process.env.NEXT_PUBLIC_GITHUB_ALLOWED_ORGANIZATIONS}
42+
</Text>{' '}
43+
GitHub organisation(s) will be able to use the application once
44+
logged in.
45+
</Text>
46+
</Box>
47+
</Stack>
48+
) : (
49+
<Stack>
50+
<Button
51+
w="full"
52+
colorScheme="gitlab"
53+
bg="github.800"
54+
color="white"
55+
leftIcon={<FiGitlab />}
56+
ms="auto"
57+
onClick={() => signIn('gitlab')}
58+
>
59+
{t('auth:login.actions.gitlab.login')}
60+
</Button>
61+
<Box borderRadius="md" bg="github.100" color="github.800" p="4">
62+
<Text>
63+
<Icon icon={FiInfo} /> Only users from{' '}
64+
<Text as="span" fontWeight="medium">
65+
{process.env.NEXT_PUBLIC_GITHUB_ALLOWED_ORGANIZATIONS}
66+
</Text>{' '}
67+
GitLab organisation(s) will be able to use the application once
68+
logged in.
69+
</Text>
70+
</Box>
71+
</Stack>
72+
)}
73+
</>
3774
);
3875
};

src/app/auth/LoginModalInterceptor.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef } from 'react';
1+
import React, { useEffect, useRef, useState } from 'react';
22

33
import {
44
Heading,
@@ -28,6 +28,8 @@ export const LoginModalInterceptor = () => {
2828
const pathnameRef = useRef(pathname);
2929
pathnameRef.current = pathname;
3030

31+
const [provider, setProvider] = useState<string>('github');
32+
3133
useEffect(() => {
3234
const interceptor = Axios.interceptors.response.use(
3335
(r) => r,
@@ -36,6 +38,10 @@ export const LoginModalInterceptor = () => {
3638
error?.response?.status === 401 &&
3739
pathnameRef.current !== '/login'
3840
) {
41+
if (error.response.data && error.response.data.provider) {
42+
setProvider(error.response.data.provider);
43+
}
44+
3945
queryCache.cancelQueries();
4046
onOpen();
4147
}
@@ -76,7 +82,7 @@ export const LoginModalInterceptor = () => {
7682
<ModalBody p="6">
7783
<Heading size="lg">{t('auth:interceptor.title')}</Heading>
7884
<Text mb="2">{t('auth:interceptor.description')}</Text>
79-
<LoginForm onSuccess={handleLogin} />
85+
<LoginForm onSuccess={handleLogin} provider={provider} />
8086
</ModalBody>
8187
</ModalContent>
8288
</Modal>

src/utils/api.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export const badRequest = (res: NextApiResponse) => {
1818
return res.status(400).end();
1919
};
2020

21-
export const notSignedIn = (res: NextApiResponse) => {
21+
export const notSignedIn = (res: NextApiResponse, url?: string) => {
22+
if (url && (url as string).endsWith('api/csv/issue')) {
23+
return res.status(401).json({ provider: 'gitlab' });
24+
}
25+
2226
return res.status(401).end();
2327
};
2428

@@ -50,10 +54,10 @@ export const apiMethods =
5054
if (!method.isPublic) {
5155
// getSession is now deprecated and is way slower than getServerSession because
5256
// it does an extra fetch out over the internet to confirm data from itself
53-
const session = await getServerSession(req, res, authOptions);
5457

58+
const session = await getServerSession(req, res, authOptions);
5559
if (!session) {
56-
return notSignedIn(res);
60+
return notSignedIn(res, req.url);
5761
}
5862
}
5963

0 commit comments

Comments
 (0)