-
Notifications
You must be signed in to change notification settings - Fork 1
[DOP-21436] add Keycloak auth #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| API_URL="http://localhost:8000/v1/" | ||
| API_URL=http://localhost:8000 | ||
| AUTH_PROVIDER=dummyAuthProvider |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| import React, { useEffect } from 'react'; | ||
| import { useKeycloakLogout } from '@entities/auth'; | ||
| import { SpinOverlay } from '@shared/ui'; | ||
|
|
||
| import { AuthError } from './AuthError'; | ||
|
|
||
| export const KeycloakAuthError = () => { | ||
| const { mutate: logout, isSuccess } = useKeycloakLogout(); | ||
|
|
||
| useEffect(() => { | ||
| logout(null); | ||
| }, [logout]); | ||
|
|
||
| if (!isSuccess) { | ||
| return <SpinOverlay />; | ||
| } | ||
|
|
||
| return <AuthError />; | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,9 @@ | ||
| import { Storage } from '@shared/constants'; | ||
| import { AUTH_PROVIDER, AuthProviderType, Storage } from '@shared/constants'; | ||
|
|
||
| export const isAuthenticated = () => !!localStorage.getItem(Storage.ACCESS_TOKEN); | ||
| export const isAuthenticated = () => { | ||
| if (AUTH_PROVIDER === AuthProviderType.DUMMY) { | ||
| return !!localStorage.getItem(Storage.ACCESS_TOKEN); | ||
| } | ||
|
|
||
| return !!localStorage.getItem(Storage.IS_KEYCLOAK_AUTH); | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export * from './useCurrentUserInfo'; | ||
| export * from './useKeycloakCallback'; | ||
| export * from './useKeycloakLogout'; |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,17 +1,16 @@ | ||
| import { useQuery, UseQueryResult } from '@tanstack/react-query'; | ||
| import { Storage } from '@shared/constants'; | ||
| import { useQuery, UseQueryOptions, UseQueryResult } from '@tanstack/react-query'; | ||
|
|
||
| import { authService } from '../../authService'; | ||
| import { AuthUser } from '../../types'; | ||
| import { AuthQueryKey } from '../../keys'; | ||
|
|
||
| /** Hook for getting current user info from backend */ | ||
| export const useCurrentUserInfo = (): UseQueryResult<AuthUser> => { | ||
| const accessToken = localStorage.getItem(Storage.ACCESS_TOKEN); | ||
|
|
||
| export const useCurrentUserInfo = ( | ||
| options?: Pick<UseQueryOptions<AuthUser>, 'enabled' | 'throwOnError'>, | ||
| ): UseQueryResult<AuthUser> => { | ||
| return useQuery({ | ||
| queryKey: [AuthQueryKey.GET_CURRENT_USER_INFO], | ||
| queryFn: authService.getCurrentUserInfo, | ||
| enabled: !!accessToken, | ||
| ...options, | ||
| }); | ||
| }; |
This file contains hidden or 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,14 @@ | ||
| import { useSuspenseQuery, UseSuspenseQueryResult } from '@tanstack/react-query'; | ||
|
|
||
| import { authService } from '../../authService'; | ||
| import { KeycloakCallbackRequest } from '../../types'; | ||
| import { AuthQueryKey } from '../../keys'; | ||
|
|
||
| /** Hook for getting auth tokens from keycloak callback from backend */ | ||
| export const useKeycloakCallback = (params: KeycloakCallbackRequest): UseSuspenseQueryResult<null> => { | ||
| return useSuspenseQuery({ | ||
| queryKey: [AuthQueryKey.KEYCLOAK_CALLBACK], | ||
| queryFn: () => authService.keycloakCallback(params), | ||
| gcTime: 0, | ||
| }); | ||
| }; |
This file contains hidden or 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,11 @@ | ||
| import { UseMutationResult, useMutation } from '@tanstack/react-query'; | ||
|
|
||
| import { authService } from '../../authService'; | ||
|
|
||
| /** Hook for logout from keycloak */ | ||
| export const useKeycloakLogout = (): UseMutationResult<null> => { | ||
| return useMutation({ | ||
| mutationFn: authService.keycloakLogout, | ||
| throwOnError: true, | ||
| }); | ||
| }; |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export const AuthQueryKey = { | ||
| LOGIN: 'LOGIN', | ||
| GET_CURRENT_USER_INFO: 'GET_CURRENT_USER_INFO', | ||
| KEYCLOAK_CALLBACK: 'KEYCLOAK_CALLBACK', | ||
| } as const; |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './useAuth'; | ||
| export * from './useLogout'; | ||
| export * from './useLogin'; | ||
| export * from './useKeycloakLogin'; |
This file contains hidden or 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 @@ | ||
| import { Storage } from '@shared/constants'; | ||
| import { useNavigate } from 'react-router-dom'; | ||
|
|
||
| export const useKeycloakLogin = () => { | ||
| const navigate = useNavigate(); | ||
|
|
||
| const login = () => { | ||
| localStorage.setItem(Storage.IS_KEYCLOAK_AUTH, 'true'); | ||
| navigate('/connections'); | ||
| }; | ||
|
|
||
| return login; | ||
| }; |
This file contains hidden or 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 @@ | ||
| import { useKeycloakCallback, useKeycloakLogin } from '@entities/auth'; | ||
| import { useLayoutEffect } from 'react'; | ||
|
|
||
| import { KeycloakCallbackProps } from './types'; | ||
|
|
||
| export const KeycloakCallback = (props: KeycloakCallbackProps) => { | ||
| useKeycloakCallback(props); | ||
| const login = useKeycloakLogin(); | ||
|
|
||
| useLayoutEffect(login, [login]); | ||
|
|
||
| return null; | ||
| }; |
This file contains hidden or 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,3 @@ | ||
| export interface KeycloakCallbackProps { | ||
| code: string; | ||
| } |
This file contains hidden or 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,41 @@ | ||
| import React, { useEffect, useState } from 'react'; | ||
| import { useCurrentUserInfo, useKeycloakLogin } from '@entities/auth'; | ||
| import { Button, Typography } from 'antd'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import classes from './styles.module.less'; | ||
|
|
||
| const { Title } = Typography; | ||
|
|
||
| export const KeycloakLogin = () => { | ||
| const { t } = useTranslation('auth'); | ||
| const login = useKeycloakLogin(); | ||
| const [isEnabledRequest, setIsEnabledRequest] = useState(false); | ||
| const { isSuccess, isEnabled, isFetching } = useCurrentUserInfo({ enabled: isEnabledRequest, throwOnError: false }); | ||
|
|
||
| useEffect(() => { | ||
| if (isSuccess && isEnabled) { | ||
| login(); | ||
| } | ||
| }, [login, isSuccess, isEnabled]); | ||
|
|
||
| const handleLogin = () => { | ||
| setIsEnabledRequest(true); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={classes.wrapper}> | ||
| <Title>{t('auth')}</Title> | ||
| <Button | ||
| className={classes.button} | ||
| type="primary" | ||
| size="large" | ||
| htmlType="submit" | ||
| onClick={handleLogin} | ||
| loading={isFetching} | ||
| > | ||
| {t('signIn')} | ||
| </Button> | ||
| </div> | ||
| ); | ||
| }; |
This file contains hidden or 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,10 @@ | ||
| .wrapper { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 32px; | ||
| width: 420px; | ||
| padding: 32px; | ||
| background-color: @white; | ||
| border-radius: 16px; | ||
| box-shadow: @box-shadow-base; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export * from './Login'; | ||
| export * from './KeycloakLogin'; | ||
| export * from './KeycloakCallback'; |
This file contains hidden or 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,15 @@ | ||
| import React from 'react'; | ||
| import { KeycloakCallback } from '@features/auth'; | ||
| import { useSearchParams } from 'react-router-dom'; | ||
| import { Suspense } from '@shared/ui'; | ||
|
|
||
| export const KeycloakCallbackPage = () => { | ||
| const [searchParams] = useSearchParams(); | ||
| const code = searchParams.get('code')!; | ||
|
|
||
| return ( | ||
| <Suspense> | ||
| <KeycloakCallback code={code} /> | ||
| </Suspense> | ||
| ); | ||
| }; |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,11 @@ | ||
| import { Login } from '@features/auth'; | ||
| import { KeycloakLogin, Login } from '@features/auth'; | ||
| import { AUTH_PROVIDER, AuthProviderType } from '@shared/constants'; | ||
| import React from 'react'; | ||
|
|
||
| export const LoginPage = () => <Login />; | ||
| export const LoginPage = () => { | ||
| if (AUTH_PROVIDER === AuthProviderType.DUMMY) { | ||
| return <Login />; | ||
| } | ||
|
|
||
| return <KeycloakLogin />; | ||
| }; |
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './LoginPage'; | ||
| export * from './KeycloakCallbackPage'; |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,15 +1,16 @@ | ||
| import axios from 'axios'; | ||
|
|
||
| import { requestInterceptor, responseSuccessInterceptor } from './interceptors'; | ||
| import { requestInterceptor, responseErrorInterceptor, responseSuccessInterceptor } from './interceptors'; | ||
|
|
||
| export const axiosInstance = axios.create({ | ||
| baseURL: window.env?.API_URL || process.env.API_URL || 'http://localhost:8000', | ||
| headers: { | ||
| Accept: 'application/json', | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| withCredentials: true, | ||
| }); | ||
|
|
||
| axiosInstance.interceptors.request.use(requestInterceptor); | ||
|
|
||
| axiosInstance.interceptors.response.use(responseSuccessInterceptor); | ||
| axiosInstance.interceptors.response.use(responseSuccessInterceptor, responseErrorInterceptor); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.