Skip to content

Commit

Permalink
Google Auth Button Format cleanup --prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
akizito committed May 18, 2024
1 parent a31fec6 commit 09c379b
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 54 deletions.
19 changes: 14 additions & 5 deletions nesis/frontend/client/src/GoogleAuthContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? <GoogleOAuthProvider value={google_client_id} clientId={google_client_id}>{children}</GoogleOAuthProvider> : <DefaultConfigContext.Provider value={googleAuthEnabled}>{children}</DefaultConfigContext.Provider>}
{googleAuthEnabled ? (
<GoogleOAuthProvider
value={google_client_id}
clientId={google_client_id}
>
{children}
</GoogleOAuthProvider>
) : (
<DefaultConfigContext.Provider value={googleAuthEnabled}>
{children}
</DefaultConfigContext.Provider>
)}
</>

);
}
24 changes: 12 additions & 12 deletions nesis/frontend/client/src/components/GoogleButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ 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');
}

return (
<>
<button className={`${classes.orloginbutton} my-3`} onClick={googleLogin}>
<button className={`${classes.orloginbutton} my-3`} onClick={googleLogin}>
<img className={`${classes.loginorimg} mx-1`} src={GoogleIcon} />
Sign in with Google
</button>
Expand Down
1 change: 0 additions & 1 deletion nesis/frontend/client/src/components/GoogleButton.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ describe('<GoogleButton>', () => {
const buttonComponent = getByText('Sign in With Google');
expect(buttonComponent).toBeInTheDocument();
});

});
4 changes: 2 additions & 2 deletions nesis/frontend/client/src/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -21,7 +21,7 @@ root.render(
<ConfigContextProvider>
<GoogleContextProvider>
<Route path="/" component={App} />
</GoogleContextProvider>
</GoogleContextProvider>
</ConfigContextProvider>
</Router>
</ThemeProvider>
Expand Down
12 changes: 9 additions & 3 deletions nesis/frontend/client/src/pages/SignInPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -147,7 +149,10 @@ const SignInPage = () => {
<Row>
<Col className={`${classes.colsign} px-1`} lg={10}>
{googleAuthEnabled && !toggleCreds && (
<GoogleButton onFailure={setError} onSuccess={handleSuccess} />
<GoogleButton
onFailure={setError}
onSuccess={handleSuccess}
/>
)}
</Col>
</Row>
Expand Down Expand Up @@ -194,7 +199,8 @@ const SignInPage = () => {
)}
{oauthEnabled && (
<div className={classes.toggleCredsDiv}>
<span className={classes.toggleCreds}
<span
className={classes.toggleCreds}
onClick={() => setToggleCreds(!toggleCreds)}
>
Use {!toggleCreds ? 'password' : 'Azure'}
Expand Down
3 changes: 1 addition & 2 deletions nesis/frontend/client/src/styles/SignInPage.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,4 @@
cursor: pointer;
min-width: 50%;
background-color: #d5dee896;
}

}
8 changes: 4 additions & 4 deletions nesis/frontend/client/src/utils/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function renderWithRouter(
{
route = '/',
history = createMemoryHistory({ initialEntries: [route] }),
} = {}
} = {},
) {
const Wrapper = ({ children }) => (
<Router history={history}>{children}</Router>
Expand All @@ -23,17 +23,17 @@ export function renderWithRouter(
};
}

const queryClient = new QueryClient()
const queryClient = new QueryClient();

export function renderWithContext(ui, options) {
return {
...renderWithRouter(
<ToasterContextProvider>
<ConfigContextProvider>
<GoogleContextProvider>{ui}</GoogleContextProvider>
</ConfigContextProvider >
</ConfigContextProvider>
</ToasterContextProvider>,
options
options,
),
};
}
Expand Down
51 changes: 26 additions & 25 deletions nesis/frontend/server/api/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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;
Expand All @@ -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')
Expand Down

0 comments on commit 09c379b

Please sign in to comment.