Skip to content

Commit

Permalink
built the sign up and login oauth google logic for the client and ser…
Browse files Browse the repository at this point in the history
…ver, created an auth context to handle the authentication state, modified components using state to work with the context and created extra components and routes
  • Loading branch information
Fransco35 committed Jul 10, 2023
1 parent 43218a2 commit a39ffac
Show file tree
Hide file tree
Showing 28 changed files with 1,143 additions and 264 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ MONGO_URI="mongodb+srv://admin-fransco:[email protected]/
CLOUDINARY_NAME='dyieuptpt'
CLOUDINARY_API_KEY='413946266621849'
CLOUDINARY_API_SECRET='0IizkxSpuvSPhYzgJpuHDXpuzXw'
SECRET='secrecy'

GOOGLE_CLIENT_ID=282292901096-6p49lpsprjgu5pa8rf1fh48sdglen2dl.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=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

56 changes: 29 additions & 27 deletions client/package-lock.json

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

4 changes: 1 addition & 3 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.4.0",
"@fortawesome/free-brands-svg-icons": "^6.4.0",
"@fortawesome/free-regular-svg-icons": "^6.4.0",
"@fortawesome/free-solid-svg-icons": "^6.4.0",
"@fortawesome/react-fontawesome": "^0.2.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"babel-plugin-macros": "^3.1.0",
"axios": "^1.4.0",
"http-proxy-middleware": "^2.0.6",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
1 change: 0 additions & 1 deletion client/src/App.css

This file was deleted.

27 changes: 17 additions & 10 deletions client/src/App.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import { Route, Routes } from "react-router-dom"
import Navbar from './components/UI/Navbar';
import { Route, Routes, Navigate } from "react-router-dom";
import Navbar from "./components/UI/Navbar";
import Home from "./screens/Home";
import Write from "./screens/Write";
import Login from "./screens/Login";
import Signup from "./screens/Signup"
import Login from "./screens/Login/Login";
import Signup from "./screens/Signup/Signup";
import Success from "./screens/Success/Success";
import ArticleDetails from "./screens/ArticleDetails";
import './App.css';

import AuthContext from "./context/auth-context";
import { useContext } from "react";

function App() {
const context = useContext(AuthContext);

return (
<div className="App">
<Navbar />
<main>
<Routes>
<Route path="/" element={<Home />}/>
<Route path="/write" element={<Write />}/>
<Route path="/signup" element={<Signup />}/>
<Route path="/login" element={<Login />}/>
<Route path="/" element={<Home />} />
<Route
path="/write"
element={context.isLoggedin ? <Write /> : <Navigate to="/login" />}
/>
<Route path="/signup" element={<Signup />} />
<Route path="/login" element={<Login />} />
<Route path="/:articleId" element={<ArticleDetails />} />
<Route path="/success" element={<Success />} />
</Routes>
</main>
</div>
Expand Down
6 changes: 0 additions & 6 deletions client/src/babel-plugin-macros.config.js

This file was deleted.

6 changes: 0 additions & 6 deletions client/src/babel.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion client/src/components/UI/navbar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}

.navLogo{
font-family: 'Montserrat', sans-serif;
font-family: sans-serif;
font-weight: 700;
margin-right: 2rem;
padding-left: 2rem;
Expand Down
37 changes: 36 additions & 1 deletion client/src/components/article/ArticleForm.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
import Card from "../UI/Card"
import styles from "./articleform.module.css"
import { useRef } from "react"

const ArticleForm = props => {

const titleRef = useRef();
const imageRef = useRef();
const timeRef = useRef();
const descriptionRef = useRef();

const handleSubmit = event => {
event.preventDefault()

const enteredTitle = titleRef.current.value
const enteredImage = imageRef.current.value
const enteredTime = timeRef.current.value
const enteredDescription = descriptionRef.current.value

const articleData = {
title : enteredTitle,
image: enteredImage,
time: enteredTime,
description: enteredDescription
}

props.onAddArticle(articleData)

titleRef.current.value = ' '
imageRef.current.value = null
timeRef.current.value = null
descriptionRef.current.value = ' '


}

return (
<Card>
<form className={styles.form}>
<form className={styles.form} onSubmit={handleSubmit}>
<div className={styles.control}>
<label htmlFor="Title">Title</label>
<input
required
type="text"
name = "title"
id="title"
ref={titleRef}
/>
</div>
<div className={styles.control}>
Expand All @@ -23,6 +55,7 @@ const ArticleForm = props => {
name = "image"
id="image"
accept="image/png, image/jpg, image/jpeg, image/gif"
ref={imageRef}
/>
</div>
<div className={styles.control}>
Expand All @@ -34,6 +67,7 @@ const ArticleForm = props => {
id="duration"
min="1"
max="10"
ref={timeRef}
/>
</div>
<div className={styles.control}>
Expand All @@ -44,6 +78,7 @@ const ArticleForm = props => {
id="description"
rows="10"
cols="30"
ref={descriptionRef}
/>
</div>
<div className={styles.action}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
}

.detail h1 {
font-family: sans-serif;
line-height: 1.1;
color: #222;
font-size: 3.4rem;
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/article/articleform.module.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.form {
padding: 1rem;
font-family: 'Big Shoulders Text', cursive;
}

.control {
Expand All @@ -9,6 +8,7 @@

.control label {
display: block;
font-family: 'Big Shoulders Text', cursive;
font-weight: bold;
margin-bottom: 0.5rem;
}
Expand Down
64 changes: 64 additions & 0 deletions client/src/context/auth-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";

const AuthContext = React.createContext({
isLoggedin: false,
onSignUp: async (enteredData) => {},
googleSignIn: () => {},
});

export const AuthContextProvider = (props) => {
const navigate = useNavigate();
const [isLoggedIn, setIsLoggedIn] = useState(false);

const signup = async (enteredData) => {
try {
const response = await fetch("/api/signup", {
method: "POST",
body: JSON.stringify(enteredData),
headers: {
"Content-Type": "application/json",
},
});

if (response.status === 200) {
alert("successfully signed in");
setIsLoggedIn(true);
}
} catch (error) {
console.log(error.response);
}
};

const googleAuth = () => {
window.location.href = "http://localhost:3001/api/google";
};

const navigateHomeOnSuccess = () => {
setIsLoggedIn(true);

navigate("/");
};

const navigateWriteOnSuccess = () => {
setIsLoggedIn(true);

navigate("/write");
};

return (
<AuthContext.Provider
value={{
isLoggedin: isLoggedIn,
onSignUp: signup,
googleSignIn: googleAuth,
navigateHome: navigateHomeOnSuccess,
navigateWrite: navigateWriteOnSuccess,
}}
>
{props.children}
</AuthContext.Provider>
);
};

export default AuthContext;
4 changes: 4 additions & 0 deletions client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import ReactDOM from 'react-dom/client';
import { BrowserRouter } from "react-router-dom"
import './index.css';
import App from './App';
import { AuthContextProvider } from './context/auth-context';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(

<BrowserRouter>
<AuthContextProvider>
<App />
</AuthContextProvider>
</BrowserRouter>
);

Expand Down
2 changes: 1 addition & 1 deletion client/src/layout/header.module.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.header {
font-family: 'Montserrat', sans-serif;
font-family: sans-serif;
font-weight: 700;
height: 70vh;
background-image: linear-gradient(
Expand Down
Loading

0 comments on commit a39ffac

Please sign in to comment.