-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix/improve-react-code
- Loading branch information
Showing
23 changed files
with
949 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use client'; | ||
|
||
import { Suspense } from 'react'; | ||
|
||
import PageOAuthCallback from '@/features/auth/PageOAuthCallback'; | ||
|
||
export default function Page() { | ||
return ( | ||
<Suspense> | ||
<PageOAuthCallback /> | ||
</Suspense> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { | ||
Button, | ||
ButtonProps, | ||
Divider, | ||
Flex, | ||
SimpleGrid, | ||
Text, | ||
} from '@chakra-ui/react'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { Icon } from '@/components/Icons'; | ||
import { toastCustom } from '@/components/Toast'; | ||
import { | ||
OAUTH_PROVIDERS, | ||
OAUTH_PROVIDERS_ENABLED_ARRAY, | ||
OAuthProvider, | ||
} from '@/features/auth/oauth-config'; | ||
import { trpc } from '@/lib/trpc/client'; | ||
|
||
export const OAuthLoginButton = ({ | ||
provider, | ||
...rest | ||
}: { | ||
provider: OAuthProvider; | ||
} & ButtonProps) => { | ||
const { t } = useTranslation(['auth']); | ||
const router = useRouter(); | ||
const loginWith = trpc.oauth.createAuthorizationUrl.useMutation({ | ||
onSuccess: (data) => { | ||
router.push(data.url); | ||
}, | ||
onError: (error) => { | ||
toastCustom({ | ||
status: 'error', | ||
title: t('auth:login.feedbacks.oAuthError.title', { | ||
provider: OAUTH_PROVIDERS[provider].label, | ||
}), | ||
description: error.message, | ||
}); | ||
}, | ||
}); | ||
|
||
return ( | ||
<Button | ||
onClick={() => loginWith.mutate({ provider: provider })} | ||
isLoading={loginWith.isLoading || loginWith.isSuccess} | ||
leftIcon={<Icon icon={OAUTH_PROVIDERS[provider].icon} />} | ||
{...rest} | ||
> | ||
{OAUTH_PROVIDERS[provider].label} | ||
</Button> | ||
); | ||
}; | ||
|
||
export const OAuthLoginButtonsGrid = () => { | ||
if (!OAUTH_PROVIDERS_ENABLED_ARRAY.length) return null; | ||
return ( | ||
<SimpleGrid columns={2} gap={3}> | ||
{OAUTH_PROVIDERS_ENABLED_ARRAY.map(({ provider }) => { | ||
return ( | ||
<OAuthLoginButton | ||
key={provider} | ||
provider={provider} | ||
_first={{ | ||
gridColumn: | ||
OAUTH_PROVIDERS_ENABLED_ARRAY.length % 2 !== 0 | ||
? 'span 2' | ||
: undefined, | ||
}} | ||
/> | ||
); | ||
})} | ||
</SimpleGrid> | ||
); | ||
}; | ||
|
||
export const OAuthLoginDivider = () => { | ||
const { t } = useTranslation(['common']); | ||
if (!OAUTH_PROVIDERS_ENABLED_ARRAY.length) return null; | ||
return ( | ||
<Flex alignItems="center" gap={2}> | ||
<Divider flex={1} /> | ||
<Text fontSize="xs" color="text-dimmed" textTransform="uppercase"> | ||
{t('common:or')} | ||
</Text> | ||
<Divider flex={1} /> | ||
</Flex> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
|
||
import { | ||
notFound, | ||
useParams, | ||
useRouter, | ||
useSearchParams, | ||
} from 'next/navigation'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { z } from 'zod'; | ||
|
||
import { LoaderFull } from '@/components/LoaderFull'; | ||
import { toastCustom } from '@/components/Toast'; | ||
import { ROUTES_ADMIN } from '@/features/admin/routes'; | ||
import { ROUTES_APP } from '@/features/app/routes'; | ||
import { zOAuthProvider } from '@/features/auth/oauth-config'; | ||
import { ROUTES_AUTH } from '@/features/auth/routes'; | ||
import { trpc } from '@/lib/trpc/client'; | ||
|
||
export default function PageOAuthCallback() { | ||
const { i18n, t } = useTranslation(['auth']); | ||
const router = useRouter(); | ||
const isTriggeredRef = useRef(false); | ||
const params = z | ||
.object({ provider: zOAuthProvider() }) | ||
.safeParse(useParams()); | ||
const searchParams = z | ||
.object({ code: z.string(), state: z.string() }) | ||
.safeParse({ | ||
code: useSearchParams().get('code'), | ||
state: useSearchParams().get('state'), | ||
}); | ||
const validateLogin = trpc.oauth.validateLogin.useMutation({ | ||
onSuccess: (data) => { | ||
if (data.account.authorizations.includes('ADMIN')) { | ||
router.replace(ROUTES_ADMIN.root()); | ||
return; | ||
} | ||
router.replace(ROUTES_APP.root()); | ||
}, | ||
onError: () => { | ||
toastCustom({ | ||
status: 'error', | ||
title: t('auth:login.feedbacks.loginError.title'), | ||
}); | ||
router.replace(ROUTES_AUTH.login()); | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
const trigger = () => { | ||
if (isTriggeredRef.current) return; | ||
isTriggeredRef.current = true; | ||
|
||
if (!(params.success && searchParams.success)) { | ||
notFound(); | ||
} | ||
|
||
validateLogin.mutate({ | ||
provider: params.data.provider, | ||
code: searchParams.data.code, | ||
state: searchParams.data.state, | ||
language: i18n.language, | ||
}); | ||
}; | ||
trigger(); | ||
}, [validateLogin, params, searchParams, i18n]); | ||
|
||
return <LoaderFull />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.