-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
craigrbarnes
committed
Mar 1, 2024
1 parent
5e879e1
commit bc87e1c
Showing
18 changed files
with
198 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
import { CoreState } from '../../reducers'; | ||
|
||
export interface AuthState { | ||
csrf?: string; | ||
accessToken?: string; | ||
} | ||
|
||
const initialState : AuthState = { | ||
csrf: undefined, | ||
accessToken: undefined, | ||
}; | ||
|
||
const slice = createSlice({ | ||
name: 'authState', | ||
initialState, | ||
reducers: { | ||
setCSRF: ( | ||
state: AuthState, | ||
action: PayloadAction<{ csrf: string }>, | ||
) => { | ||
return { ...state, csrf: action.payload.csrf }; | ||
}, | ||
setAccessToken: (state: AuthState, action: PayloadAction<{ accessToken: string }>) => { | ||
return { ...state, accessToken: action.payload.accessToken }; | ||
}, | ||
}, | ||
}); | ||
|
||
|
||
export const authReducer = slice.reducer; | ||
export const { setCSRF, setAccessToken } = slice.actions; | ||
|
||
export const selectAuthCSRF = (state: CoreState): string | undefined => | ||
state.auth.csrf; | ||
|
||
export const selectAccessToken = (state: CoreState): string | undefined => | ||
state.auth.accessToken; |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { type AuthzMapping, type ServiceAndMethod } from './types'; | ||
import { useGetAuthzMappingsQuery } from './authzMappingSlice'; | ||
import { setAccessToken, setCSRF, selectAccessToken, selectAuthCSRF } from './authStateSlice'; | ||
|
||
export { useGetAuthzMappingsQuery, type AuthzMapping, type ServiceAndMethod }; | ||
export { useGetAuthzMappingsQuery, type AuthzMapping, type ServiceAndMethod, setCSRF, setAccessToken, selectAccessToken, selectAuthCSRF}; |
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
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
70 changes: 70 additions & 0 deletions
70
packages/frontend/src/components/Login/CredentialsLogin.tsx
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, {useState} from 'react'; | ||
import { Button, Loader, TextInput } from '@mantine/core'; | ||
import { GEN3_DOMAIN, setAccessToken, useAuthorizeFromCredentialsMutation, useCoreDispatch } from '@gen3/core'; | ||
import { MdClose as CloseIcon } from 'react-icons/md'; | ||
import { useDeepCompareEffect } from 'use-deep-compare'; | ||
import { useRouter } from 'next/router'; | ||
import { showNotification } from '@mantine/notifications'; | ||
import { stripTrailingSlash } from '../../utils'; | ||
import { LoginRedirectProps } from './types'; | ||
|
||
const CredentialsLogin = ( { handleLoginSelected, redirectURL } : LoginRedirectProps) => { | ||
|
||
const dispatch = useCoreDispatch(); | ||
|
||
const [credentials, setCredentials] = useState(''); | ||
|
||
const [authorizeFromCredentials, { data, isError, isLoading }] = useAuthorizeFromCredentialsMutation(); | ||
|
||
useDeepCompareEffect( () => { | ||
if (data?.access_token) { | ||
fetch('/api/auth/setSessionToken', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ access_token: data.access_token }) | ||
}); | ||
|
||
dispatch(setAccessToken({ accessToken: data?.access_token })); | ||
handleLoginSelected('', redirectURL); | ||
}}, [data]); | ||
|
||
return ( | ||
<div className='flex w-full'> | ||
<TextInput value={credentials} | ||
onChange={(event) => { | ||
setCredentials(event.target.value); | ||
}} | ||
|
||
classNames={{ | ||
input: 'focus:border-2 focus:border-primary text-sm', | ||
}} | ||
size="sm" | ||
rightSection={ | ||
isLoading ? ( | ||
<Loader size={20} /> | ||
) : | ||
credentials.length > 0 && ( | ||
<CloseIcon | ||
onClick={() => { | ||
setCredentials(''); | ||
}} | ||
className="cursor-pointer" | ||
data-testid="search-input-clear-search" | ||
/> | ||
) | ||
} | ||
></TextInput> | ||
<Button color="blue" onClick={() => { | ||
const payload = { | ||
"api_key": "", | ||
"key_id": "" | ||
}; | ||
authorizeFromCredentials(payload); | ||
}}>Authorize</Button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CredentialsLogin; |
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import GqlQueryEditor from './GqlQueryEditor'; | ||
|
||
export default GqlQueryEditor; | ||
export { GqlQueryEditor }; |
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,5 @@ | ||
export const stripTrailingSlash = (str:string):string => { | ||
return str.endsWith('/') ? | ||
str.slice(0, -1) : | ||
str; | ||
}; |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
GEN3_COMMONS_NAME=brh | ||
NEXT_PUBLIC_GEN3_API=https://localhost:3010 | ||
NEXT_PUBLIC_GEN3_API=https://brhstaging.data-commons.org/ | ||
NEXT_PUBLIC_GEN3_DOMAIN=https://localhost:3010 | ||
NEXT_PUBLIC_GEN3_MDS_API=https://brh.data-commons.org/mds | ||
#NEXT_PUBLIC_GEN3_MDS_API=https://brh.data-commons.org/mds | ||
#NEXT_PUBLIC_GEN3_GUPPY_API=https://localhost:3010/guppy |
21 changes: 21 additions & 0 deletions
21
packages/sampleCommons/src/pages/api/auth/setSessionToken.ts
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,21 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { setCookie } from 'cookies-next'; | ||
import { decodeJwt, JWTPayload } from 'jose'; | ||
|
||
export default async function (req: NextApiRequest, res: NextApiResponse) { | ||
const { access_token } = req.body; | ||
const payload = decodeJwt(access_token) as JWTPayload; | ||
|
||
if (!payload) { | ||
res.status(400).json({ message: 'Invalid token' }); | ||
return; | ||
} | ||
console.log("payload: ", payload); | ||
setCookie( 'access_token', access_token, { | ||
req, res, | ||
maxAge: payload && payload.exp && payload.iat ? payload.exp - payload.iat : 60 * 60 * 24 * 30, | ||
sameSite: 'lax', | ||
secure: process.env.NODE_ENV === 'production', | ||
}); | ||
res.status(200).json({ message: 'Session token set' }); | ||
} |