Skip to content

Commit

Permalink
Fixes show login form's provider
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeBro committed Oct 9, 2024
1 parent be6b515 commit 160e91e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 31 deletions.
89 changes: 63 additions & 26 deletions src/app/auth/LoginForm.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,75 @@
import React from 'react';
import React, { useEffect } from 'react';

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

import { Icon } from '@/components';

export const LoginForm = ({ onSuccess = () => undefined, ...rest }: TODO) => {
export const LoginForm = ({
onSuccess = () => undefined,
provider,
...rest
}: TODO) => {
const { t } = useTranslation();
useEffect(() => {
if (provider === undefined) {
provider = 'github';

Check warning on line 18 in src/app/auth/LoginForm.tsx

View workflow job for this annotation

GitHub Actions / Build and Test (14)

Assignments to the 'provider' variable from inside React Hook useEffect will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useEffect

Check warning on line 18 in src/app/auth/LoginForm.tsx

View workflow job for this annotation

GitHub Actions / Build and Test (16)

Assignments to the 'provider' variable from inside React Hook useEffect will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useEffect

Check warning on line 18 in src/app/auth/LoginForm.tsx

View workflow job for this annotation

GitHub Actions / Build and Test (lts/*)

Assignments to the 'provider' variable from inside React Hook useEffect will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useEffect
}
}, []);

return (
<Stack {...rest}>
<Button
w="full"
colorScheme="github"
bg="github.800"
color="white"
leftIcon={<FiGithub />}
ms="auto"
onClick={() => signIn('github')}
>
{t('auth:login.actions.github.login')}
</Button>
<Box borderRadius="md" bg="github.100" color="github.800" p="4">
<Text>
<Icon icon={FiInfo} /> Only users from{' '}
<Text as="span" fontWeight="medium">
{process.env.NEXT_PUBLIC_GITHUB_ALLOWED_ORGANIZATIONS}
</Text>{' '}
GitHub organisation(s) will be able to use the application once logged
in.
</Text>
</Box>
</Stack>
<>
{provider === 'github' ? (
<Stack {...rest}>
<Button
w="full"
colorScheme="github"
bg="github.800"
color="white"
leftIcon={<FiGithub />}
ms="auto"
onClick={() => signIn('github')}
>
{t('auth:login.actions.github.login')}
</Button>
<Box borderRadius="md" bg="github.100" color="github.800" p="4">
<Text>
<Icon icon={FiInfo} /> Only users from{' '}
<Text as="span" fontWeight="medium">
{process.env.NEXT_PUBLIC_GITHUB_ALLOWED_ORGANIZATIONS}
</Text>{' '}
GitHub organisation(s) will be able to use the application once
logged in.
</Text>
</Box>
</Stack>
) : (
<Stack>
<Button
w="full"
colorScheme="gitlab"
bg="github.800"
color="white"
leftIcon={<FiGitlab />}
ms="auto"
onClick={() => signIn('gitlab')}
>
{t('auth:login.actions.gitlab.login')}
</Button>
<Box borderRadius="md" bg="github.100" color="github.800" p="4">
<Text>
<Icon icon={FiInfo} /> Only users from{' '}
<Text as="span" fontWeight="medium">
{process.env.NEXT_PUBLIC_GITHUB_ALLOWED_ORGANIZATIONS}
</Text>{' '}
GitLab organisation(s) will be able to use the application once
logged in.
</Text>
</Box>
</Stack>
)}
</>
);
};
10 changes: 8 additions & 2 deletions src/app/auth/LoginModalInterceptor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useRef, useState } from 'react';

import {
Heading,
Expand Down Expand Up @@ -28,6 +28,8 @@ export const LoginModalInterceptor = () => {
const pathnameRef = useRef(pathname);
pathnameRef.current = pathname;

const [provider, setProvider] = useState<string>('github');

useEffect(() => {
const interceptor = Axios.interceptors.response.use(
(r) => r,
Expand All @@ -36,6 +38,10 @@ export const LoginModalInterceptor = () => {
error?.response?.status === 401 &&
pathnameRef.current !== '/login'
) {
if (error.response.data && error.response.data.provider) {
setProvider(error.response.data.provider);
}

queryCache.cancelQueries();
onOpen();
}
Expand Down Expand Up @@ -76,7 +82,7 @@ export const LoginModalInterceptor = () => {
<ModalBody p="6">
<Heading size="lg">{t('auth:interceptor.title')}</Heading>
<Text mb="2">{t('auth:interceptor.description')}</Text>
<LoginForm onSuccess={handleLogin} />
<LoginForm onSuccess={handleLogin} provider={provider} />
</ModalBody>
</ModalContent>
</Modal>
Expand Down
10 changes: 7 additions & 3 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export const badRequest = (res: NextApiResponse) => {
return res.status(400).end();
};

export const notSignedIn = (res: NextApiResponse) => {
export const notSignedIn = (res: NextApiResponse, url?: string) => {
if (url && (url as string).endsWith('api/csv/issue')) {
return res.status(401).json({ provider: 'gitlab' });
}

return res.status(401).end();
};

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

const session = await getServerSession(req, res, authOptions);
if (!session) {
return notSignedIn(res);
return notSignedIn(res, req.url);
}
}

Expand Down

0 comments on commit 160e91e

Please sign in to comment.