Skip to content

Commit

Permalink
Merge branch 'functionality-authentication' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
hsnice16 committed May 12, 2022
2 parents 73b56c5 + a7baa41 commit 55cc851
Show file tree
Hide file tree
Showing 35 changed files with 701 additions and 92 deletions.
31 changes: 27 additions & 4 deletions 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@emotion/react": "^11.9.0",
"@emotion/styled": "^11.8.1",
"@mui/material": "^5.6.2",
"axios": "^0.27.2",
"classnames": "^2.3.1",
"dayjs": "^1.11.1",
"jwt-decode": "^3.1.2",
Expand Down
28 changes: 20 additions & 8 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import { Route, Routes, useLocation } from "react-router-dom";
import { Box, Container, useMediaQuery } from "@mui/material";
import { createTheme, ThemeProvider } from "@mui/material/styles";

import { LeftSideNavbar, RightSideBar } from "components";
import {
ProtectRoute,
RightSideBar,
RestrictRoute,
LeftSideNavbar,
} from "components";
import { Bookmarks, Explore, Home, Landing, Profile } from "pages";

import {
ROUTE_HOME,
CustomTheme,
ROUTE_BOOKMARKS,
ROUTE_EXPLORE,
ROUTE_HOME,
ROUTE_LANDING,
ROUTE_PROFILE,
ROUTE_BOOKMARKS,
} from "utils";

function App() {
Expand All @@ -23,7 +29,11 @@ function App() {
return (
<ThemeProvider theme={theme}>
{location.pathname === ROUTE_LANDING ? (
<Landing />
<Routes>
<Route element={<RestrictRoute />}>
<Route path={ROUTE_LANDING} element={<Landing />} />
</Route>
</Routes>
) : (
<Container className="container-lg">
<Box
Expand All @@ -39,10 +49,12 @@ function App() {
gridColumn={matches ? "span 9" : "span 6"}
>
<Routes>
<Route path={ROUTE_BOOKMARKS} element={<Bookmarks />} />
<Route path={ROUTE_EXPLORE} element={<Explore />} />
<Route path={ROUTE_HOME} element={<Home />} />
<Route path={ROUTE_PROFILE} element={<Profile />} />
<Route element={<ProtectRoute />}>
<Route path={ROUTE_BOOKMARKS} element={<Bookmarks />} />
<Route path={ROUTE_EXPLORE} element={<Explore />} />
<Route path={ROUTE_HOME} element={<Home />} />
<Route path={ROUTE_PROFILE} element={<Profile />} />
</Route>
</Routes>
</Box>

Expand Down
26 changes: 12 additions & 14 deletions src/backend/controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const sign = require("jwt-encode");
* */

export const signupHandler = function (schema, request) {
const { username, password, ...rest } = JSON.parse(request.requestBody);
const { email, password, ...rest } = JSON.parse(request.requestBody);
try {
// check if username already exists
const foundUser = schema.users.findBy({ username: username });
const foundUser = schema.users.findBy({ email });
if (foundUser) {
return new Response(
422,
Expand All @@ -29,23 +29,22 @@ export const signupHandler = function (schema, request) {
);
}
const _id = uuid();
const username = email.substring(0, email.indexOf("@"));

const newUser = {
_id,
createdAt: formatDate(),
updatedAt: formatDate(),
username,
email,
password,
username,
...rest,
followers: [],
following: [],
bookmarks: [],
};
const createdUser = schema.users.create(newUser);
const encodedToken = sign(
{ _id, username },
process.env.REACT_APP_JWT_SECRET
);
const encodedToken = sign({ _id, email }, process.env.REACT_APP_JWT_SECRET);
return new Response(201, {}, { createdUser, encodedToken });
} catch (error) {
return new Response(
Expand All @@ -61,27 +60,26 @@ export const signupHandler = function (schema, request) {
/**
* This handler handles user login.
* send POST Request at /api/auth/login
* body contains {username, password}
* body contains {email, password}
* */

export const loginHandler = function (schema, request) {
const { username, password } = JSON.parse(request.requestBody);
const { email, password } = JSON.parse(request.requestBody);

try {
const foundUser = schema.users.findBy({ username: username });
const foundUser = schema.users.findBy({ email });
if (!foundUser) {
return new Response(
404,
{},
{
errors: [
"The username you entered is not Registered. Not Found error",
],
errors: ["The email you entered is not Registered. Not Found error"],
}
);
}
if (password === foundUser.password) {
const encodedToken = sign(
{ _id: foundUser._id, username },
{ _id: foundUser._id, email },
process.env.REACT_APP_JWT_SECRET
);
return new Response(200, {}, { foundUser, encodedToken });
Expand Down
9 changes: 5 additions & 4 deletions src/backend/db/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { formatDate } from "../utils/authUtils";
export const users = [
{
_id: uuid(),
firstName: "Adarsh",
lastName: "Balika",
username: "adarshbalika",
password: "adarshBalika123",
lastName: "Wick",
firstName: "John",
username: "johnwick",
password: "johnWick",
createdAt: formatDate(),
updatedAt: formatDate(),
email: "[email protected]",
},
];
2 changes: 1 addition & 1 deletion src/backend/utils/authUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const requiresAuth = function (request) {
process.env.REACT_APP_JWT_SECRET
);
if (decodedToken) {
const user = this.db.users.findBy({ username: decodedToken.username });
const user = this.db.users.findBy({ email: decodedToken.email });
return user;
}
return new Response(
Expand Down
21 changes: 21 additions & 0 deletions src/components/compose/Compose.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import PropTypes from "prop-types";

export const Compose = ({ components, children }) => {
return (
<>
{components.reduceRight((acc, Comp) => {
return <Comp>{acc}</Comp>;
}, children)}
</>
);
};

Compose.propTypes = {
children: PropTypes.node,
components: PropTypes.arrayOf(PropTypes.elementType),
};

Compose.defaultProps = {
children: <></>,
components: [<></>],
};
16 changes: 15 additions & 1 deletion src/components/dialog/logout-dialog/LogoutDialog.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { TickIcon } from "assets";
import { useUser } from "context";
import PropTypes from "prop-types";
import { ROUTE_LANDING } from "utils";
import { FollowItem } from "components";
import { useNavigate } from "react-router-dom";
import styles from "./LogoutDialog.module.css";
import { useCookieHandler } from "custom-hooks";
import { Button, Dialog, DialogActions, DialogTitle } from "@mui/material";

export const LogoutDialog = ({ openLogoutDialog, setOpenLogoutDialog }) => {
const navigate = useNavigate();
const { eraseCookies } = useCookieHandler();
const { userInitialState, setUserState } = useUser();

const handleClose = () => {
setOpenLogoutDialog(false);
};

const handleLogOutClick = () => {
setUserState(userInitialState);
eraseCookies();
navigate(ROUTE_LANDING, { replace: true });
};

return (
<Dialog
open={openLogoutDialog}
Expand All @@ -32,7 +46,7 @@ export const LogoutDialog = ({ openLogoutDialog, setOpenLogoutDialog }) => {
<DialogActions className={styles.dialogAction}>
<Button
variant="text"
onClick={handleClose}
onClick={handleLogOutClick}
className={styles.btn_dialogLogOut}
>
Log out @hsnice16
Expand Down
64 changes: 47 additions & 17 deletions src/components/dialog/sign-in-dialog/SignInDialog.jsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import PropTypes from "prop-types";
import { SignInDialogData } from "data";
import { useAuthHandler } from "custom-hooks";
import styles from "./SignInDialog.module.css";

import {
DialogActionsCloseIcon,
FormError,
FormButton,
FormHeading,
FormWrapper,
PasswordInput,
DialogActionsCloseIcon,
} from "components";

import {
Checkbox,
Dialog,
Checkbox,
TextField,
DialogActions,
DialogContent,
FormControlLabel,
TextField,
} from "@mui/material";

const { buttonData } = SignInDialogData;

export const SignInDialog = ({ openSignInDialog, setOpenSignInDialog }) => {
const {
status,
error,
email,
password,
rememberMe,
handleInputChange,
handleGuestLogInClick,
handleSignInFormSubmit,
} = useAuthHandler();

const handleClose = () => {
setOpenSignInDialog(false);
};
Expand All @@ -32,33 +43,52 @@ export const SignInDialog = ({ openSignInDialog, setOpenSignInDialog }) => {
<DialogContent className={styles.dialogContent}>
<FormHeading headingText="Log In" />

{status === "error" && <FormError errorToShow={error} />}

<FormWrapper>
<TextField
autoFocus
label="Email Address"
type="email"
name="email"
value={email}
label="Email Address"
onChange={handleInputChange}
InputLabelProps={{ shrink: true }}
placeholder="[email protected]"
InputLabelProps={{
shrink: true,
}}
/>

<PasswordInput label="Password" placeholder="password" />
<PasswordInput
name="password"
label="Password"
value={password}
placeholder="password"
onChange={handleInputChange}
/>

<FormControlLabel
value="remember-me"
control={<Checkbox disableRipple />}
label="Remember me"
aria-label="remember me"
labelPlacement="end"
checked={rememberMe}
aria-label="remember me"
onChange={handleInputChange}
control={<Checkbox disableRipple />}
/>
</FormWrapper>
</DialogContent>

<DialogActions className={styles.action_logIn}>
{buttonData.map(({ _id, btnText, sxStyles }) => (
<FormButton key={_id} sxStyles={sxStyles} btnText={btnText} />
))}
<FormButton
status={status}
btnText="Log In"
onClick={handleSignInFormSubmit}
sxStyles={{ margin: "1rem 0 0.5rem" }}
/>
<FormButton
status={status}
btnText="Log In as a Guest"
onClick={handleGuestLogInClick}
sxStyles={{ margin: "0.5rem 0 0" }}
/>
</DialogActions>
</Dialog>
);
Expand Down
Loading

1 comment on commit 55cc851

@vercel
Copy link

@vercel vercel bot commented on 55cc851 May 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

think-loud – ./

think-loud-git-development-hsnice16.vercel.app
thinkloud.vercel.app
think-loud-hsnice16.vercel.app

Please sign in to comment.