From 09c379b0bfef2a2846b7ee9bfb5b09a5074b3e0c Mon Sep 17 00:00:00 2001 From: Allan Kizito Date: Sat, 18 May 2024 17:41:05 -0400 Subject: [PATCH] Google Auth Button Format cleanup --prettier --- .../frontend/client/src/GoogleAuthContext.js | 19 +++++-- .../client/src/components/GoogleButton.js | 24 ++++----- .../src/components/GoogleButton.test.js | 1 - nesis/frontend/client/src/index.js | 4 +- nesis/frontend/client/src/pages/SignInPage.js | 12 +++-- .../client/src/styles/SignInPage.module.css | 3 +- nesis/frontend/client/src/utils/testUtils.js | 8 +-- nesis/frontend/server/api/sessions.js | 51 ++++++++++--------- 8 files changed, 68 insertions(+), 54 deletions(-) diff --git a/nesis/frontend/client/src/GoogleAuthContext.js b/nesis/frontend/client/src/GoogleAuthContext.js index e4d3031..8b03cc8 100644 --- a/nesis/frontend/client/src/GoogleAuthContext.js +++ b/nesis/frontend/client/src/GoogleAuthContext.js @@ -4,16 +4,25 @@ import { useConfig } from './ConfigContext'; const DefaultConfigContext = React.createContext({}); - export default function GoogleContextProvider({ children }) { - const config = useConfig(); const google_client_id = config?.auth?.OAUTH_GOOGLE_CLIENT_ID; - const googleAuthEnabled = google_client_id && config?.auth?.OAUTH_GOOGLE_ENABLED; + const googleAuthEnabled = + google_client_id && config?.auth?.OAUTH_GOOGLE_ENABLED; return ( <> - {googleAuthEnabled ? {children} : {children}} + {googleAuthEnabled ? ( + + {children} + + ) : ( + + {children} + + )} - ); } diff --git a/nesis/frontend/client/src/components/GoogleButton.js b/nesis/frontend/client/src/components/GoogleButton.js index 12c47ef..1e57524 100644 --- a/nesis/frontend/client/src/components/GoogleButton.js +++ b/nesis/frontend/client/src/components/GoogleButton.js @@ -5,24 +5,24 @@ import parseApiErrorMessage from '../utils/parseApiErrorMessage'; import GoogleIcon from '../images/GoogleIcon.png'; import classes from '../styles/SignInPage.module.css'; -export default function GoogleButton({ onFailure, onSuccess}) { - const client = useClient(); +export default function GoogleButton({ onFailure, onSuccess }) { + const client = useClient(); - const googleLogin = useGoogleLogin( { + const googleLogin = useGoogleLogin({ onSuccess: (tokenRespose) => { - client.post('sessions', { google: tokenRespose}) + client + .post('sessions', { google: tokenRespose }) .then((response) => { - onSuccess(response?.body?.email, response); + onSuccess(response?.body?.email, response); }) .catch((error) => { - onFailure(parseApiErrorMessage(error)); - }) + onFailure(parseApiErrorMessage(error)); + }); }, onError: (error) => { - handleFailure(error); - } - - }); + handleFailure(error); + }, + }); function handleFailure(error) { onFailure('Could not login using Google'); @@ -30,7 +30,7 @@ export default function GoogleButton({ onFailure, onSuccess}) { return ( <> - diff --git a/nesis/frontend/client/src/components/GoogleButton.test.js b/nesis/frontend/client/src/components/GoogleButton.test.js index 6c4f28d..780d37a 100644 --- a/nesis/frontend/client/src/components/GoogleButton.test.js +++ b/nesis/frontend/client/src/components/GoogleButton.test.js @@ -8,5 +8,4 @@ describe('', () => { const buttonComponent = getByText('Sign in With Google'); expect(buttonComponent).toBeInTheDocument(); }); - }); diff --git a/nesis/frontend/client/src/index.js b/nesis/frontend/client/src/index.js index a60ce55..76463d6 100644 --- a/nesis/frontend/client/src/index.js +++ b/nesis/frontend/client/src/index.js @@ -1,5 +1,5 @@ import React from 'react'; -import {createRoot} from 'react-dom/client'; +import { createRoot } from 'react-dom/client'; import './index.css'; import App from './App'; import { BrowserRouter as Router, Route } from 'react-router-dom'; @@ -21,7 +21,7 @@ root.render( - + diff --git a/nesis/frontend/client/src/pages/SignInPage.js b/nesis/frontend/client/src/pages/SignInPage.js index b8d4675..bd73f36 100644 --- a/nesis/frontend/client/src/pages/SignInPage.js +++ b/nesis/frontend/client/src/pages/SignInPage.js @@ -95,7 +95,9 @@ const SignInPage = () => { const history = useHistory(); const config = useConfig(); const azureAuthEnabled = config?.auth?.OAUTH_AZURE_ENABLED; - const googleAuthEnabled = config?.auth?.OAUTH_GOOGLE_ENABLED && config?.auth?.OAUTH_GOOGLE_CLIENT_ID !== undefined; + const googleAuthEnabled = + config?.auth?.OAUTH_GOOGLE_ENABLED && + config?.auth?.OAUTH_GOOGLE_CLIENT_ID !== undefined; const oauthEnabled = azureAuthEnabled || googleAuthEnabled; function submit(session, actions) { @@ -147,7 +149,10 @@ const SignInPage = () => { {googleAuthEnabled && !toggleCreds && ( - + )} @@ -194,7 +199,8 @@ const SignInPage = () => { )} {oauthEnabled && (
- setToggleCreds(!toggleCreds)} > Use {!toggleCreds ? 'password' : 'Azure'} diff --git a/nesis/frontend/client/src/styles/SignInPage.module.css b/nesis/frontend/client/src/styles/SignInPage.module.css index d51e60c..a2c4ea6 100644 --- a/nesis/frontend/client/src/styles/SignInPage.module.css +++ b/nesis/frontend/client/src/styles/SignInPage.module.css @@ -51,5 +51,4 @@ cursor: pointer; min-width: 50%; background-color: #d5dee896; - } - +} diff --git a/nesis/frontend/client/src/utils/testUtils.js b/nesis/frontend/client/src/utils/testUtils.js index 6ddd193..cf9b9c0 100644 --- a/nesis/frontend/client/src/utils/testUtils.js +++ b/nesis/frontend/client/src/utils/testUtils.js @@ -12,7 +12,7 @@ export function renderWithRouter( { route = '/', history = createMemoryHistory({ initialEntries: [route] }), - } = {} + } = {}, ) { const Wrapper = ({ children }) => ( {children} @@ -23,7 +23,7 @@ export function renderWithRouter( }; } -const queryClient = new QueryClient() +const queryClient = new QueryClient(); export function renderWithContext(ui, options) { return { @@ -31,9 +31,9 @@ export function renderWithContext(ui, options) { {ui} - + , - options + options, ), }; } diff --git a/nesis/frontend/server/api/sessions.js b/nesis/frontend/server/api/sessions.js index b8a43ba..367cfd4 100644 --- a/nesis/frontend/server/api/sessions.js +++ b/nesis/frontend/server/api/sessions.js @@ -16,9 +16,8 @@ const post = (requests, profile) => async (request, response) => { if (session.azure) { oauthProvider = authenticateWithAzure(requests, profile, session.azure); - } else if(session.google){ + } else if (session.google) { oauthProvider = authenticateWithGoogle(requests, profile, session.google); - } else { oauthProvider = requests .post(`${url}/sessions`) @@ -94,30 +93,32 @@ function authenticateWithAzure(requests, profile, azure) { function authenticateWithGoogle(requests, profile, google) { const googleApiUrl = 'https://www.googleapis.com/oauth2/v3/userinfo'; - return requests - .get(googleApiUrl) - .set('Authorization', `Bearer ${google.access_token}`) - .set('Content-Type', 'application/json') - .send() - .then((res) => { - if (res.body?.email === undefined || res.error) { - // invalid credentials - const error = new Error('Invalid Google credentials'); - Object.assign(error, { - status:401, - response: { - body: 'Invalid google credentials', - } - }); - throw error; - } else { - return res.body;} - }) - .then((userInfo) => sendOauthSession(requests, userInfo.name, userInfo.email, profile)); - + return requests + .get(googleApiUrl) + .set('Authorization', `Bearer ${google.access_token}`) + .set('Content-Type', 'application/json') + .send() + .then((res) => { + if (res.body?.email === undefined || res.error) { + // invalid credentials + const error = new Error('Invalid Google credentials'); + Object.assign(error, { + status: 401, + response: { + body: 'Invalid google credentials', + }, + }); + throw error; + } else { + return res.body; + } + }) + .then((userInfo) => + sendOauthSession(requests, userInfo.name, userInfo.email, profile), + ); } -function sendOauthSession(requests, name, email, profile) { +function sendOauthSession(requests, name, email, profile) { const url = profile.SERVICE_ENDPOINT; const oauth_token_key = profile.NESIS_OAUTH_TOKEN_KEY; const oauth_token_value = profile.NESIS_OAUTH_TOKEN_VALUE; @@ -126,7 +127,7 @@ function sendOauthSession(requests, name, email, profile) { name: name, [oauth_token_key]: oauth_token_value, }; - + return requests .post(`${url}/sessions`) .set('Accept', 'application/json')