Skip to content

Commit

Permalink
Use Google login in production
Browse files Browse the repository at this point in the history
  • Loading branch information
alex9smith committed Nov 21, 2024
1 parent f8df74a commit 6198a6d
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 14 deletions.
3 changes: 2 additions & 1 deletion frontend/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
VITE_API_BASE_URL=""
VITE_API_BASE_URL=""
VITE_OAUTH_CLIENT_ID=""
11 changes: 11 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dependencies": {
"@primer/octicons-react": "^19.12.0",
"@primer/react": "^37.5.0",
"@react-oauth/google": "^0.12.1",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
Expand Down
25 changes: 21 additions & 4 deletions frontend/src/components/Login/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
import { Box, Button, PageLayout } from "@primer/react";
import { useNavigate } from "react-router";
import { GoogleLogin } from "@react-oauth/google";
import TopNav from "../TopNav/TopNav";
import { authenticationService } from "../../services/authentication";
import { isProduction } from "../../constants";
import { fetchUserProfile } from "../../services/google";

function Login() {
const navigate = useNavigate();

async function handleSuccess(response) {
const user = await fetchUserProfile(response);
authenticationService.setUser(user);
navigate("/");
}

function loginHandler() {
authenticationService.setUser({ name: "test" });
authenticationService.setUser({
name: "Test",
given_name: "Test",
email: "[email protected]",
});
navigate("/");
}

const loginButton = isProduction() ? (
<GoogleLogin onSuccess={handleSuccess} onError={console.log} />
) : (
<Button onClick={loginHandler}>Login</Button>
);
return (
<PageLayout padding={"none"} containerWidth="full">
<TopNav />
<PageLayout.Content width={"full"} padding={"normal"}>
<Box>
<Button onClick={loginHandler}>Login</Button>
</Box>
<Box>{loginButton}</Box>
</PageLayout.Content>
</PageLayout>
);
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/components/TopNav/TopNav.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Box, Header, PageLayout } from "@primer/react";
import { Header, PageLayout } from "@primer/react";
import { HomeIcon } from "@primer/octicons-react";
import { useNavigate } from "react-router";
import { googleLogout } from "@react-oauth/google";
import { authenticationService } from "../../services/authentication";
import { isProduction } from "../../constants";

function TopNav() {
const navigate = useNavigate();
Expand All @@ -11,6 +13,9 @@ function TopNav() {
as="div"
onClick={() => {
authenticationService.logout();
if (isProduction()) {
googleLogout();
}
navigate("/login");
}}
>
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export function getApiBaseUrl() {
return import.meta.env.VITE_API_BASE_URL;
}

export function getOAuthClientId() {
return import.meta.env.VITE_OAUTH_CLIENT_ID;
}

export function isProduction() {
return import.meta.env.PROD;
}
21 changes: 13 additions & 8 deletions frontend/src/index.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { ThemeProvider, BaseStyles } from "@primer/react";
import { GoogleOAuthProvider } from "@react-oauth/google";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import "./index.css";

import Login from "./components/Login/Login";
import Home from "./components/Home/Home";
import homeLoader from "./components/Home/loader";
import reportWebVitals from "./reportWebVitals";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import RecipeDetails from "./components/RecipeDetails/RecipeDetails";
import recipeDetailsLoader from "./components/RecipeDetails/loader";
import Plan from "./components/Plan/Plan";
import planLoader from "./components/Plan/loader";
import PrivateRoutes from "./components/PrivateRoutes/PrivateRoutes";
import { getOAuthClientId } from "./constants";

const router = createBrowserRouter([
{
Expand Down Expand Up @@ -43,13 +46,15 @@ const router = createBrowserRouter([

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<ThemeProvider>
<BaseStyles>
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
</BaseStyles>
</ThemeProvider>
<GoogleOAuthProvider>
<ThemeProvider>
<BaseStyles>
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
</BaseStyles>
</ThemeProvider>
</GoogleOAuthProvider>
);

// If you want to start measuring performance in your app, pass a function
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/services/google.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export async function fetchUserProfile(tokenResponse) {
const response = await fetch(
"https://www.googleapis.com/oauth2/v3/userinfo",
{ headers: { Authorization: `Bearer ${tokenResponse.access_token}` } }
);

if (!response.ok) {
console.error(response);
}

return await response.json();
}

0 comments on commit 6198a6d

Please sign in to comment.