Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/auth jwt #1

Merged
merged 10 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
working on client side
  • Loading branch information
Daniel-Workman committed Jul 28, 2022
commit 5887ce6fae272dd8f748b3397a535a5ae06ef9d5
6 changes: 3 additions & 3 deletions client/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<!-- <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> -->
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!-- <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> -->
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!-- <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> -->
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Expand Down
25 changes: 0 additions & 25 deletions client/public/manifest.json

This file was deleted.

9 changes: 0 additions & 9 deletions client/src/App.test.tsx

This file was deleted.

14 changes: 11 additions & 3 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import React, { useState, useEffect } from "react";
import "./App.css";
import React, { useState } from "react";
import { Routes, Route } from "react-router-dom";
import AuthForm from "./components/AuthForm/AuthForm";
import HiddenContent from "./components/HiddenContent/HiddenContent";

function App() {
const [tokens, setTokens] = useState({});

// console.log(tokens);

return (
<>
<AuthForm />
<Routes>
<Route path="/" element={<AuthForm setTokens={setTokens} />} />
<Route path="/home" element={<HiddenContent tokens={tokens} />} />
</Routes>
</>
);
}
Expand Down
11 changes: 8 additions & 3 deletions client/src/components/AuthForm/AuthForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { Visibility, VisibilityOff } from "@material-ui/icons/";
import { Input, Button, InputAdornment, IconButton } from "@material-ui/core/";
import axios from "axios";

type Props = {};

function AuthForm({}: Props) {
function AuthForm(props: any) {
const { setTokens } = props;
const navigate = useNavigate();
//set the initial state for the auth form to be signin
const [isSignup, setIsSignup] = useState(false);
//create a new set of data for the form and set the initial state as the empty fields above
Expand Down Expand Up @@ -36,6 +36,8 @@ function AuthForm({}: Props) {
.post("http:https://localhost:5001/user/login-user", formData)
.then(function (response) {
console.log(response);
let data = response.data;
setTokens(data);
})
.catch(function (error) {
console.log(error);
Expand All @@ -46,11 +48,14 @@ function AuthForm({}: Props) {
.post("http:https://localhost:5001/user/create-user", formData)
.then(function (response) {
console.log(response);
let data = response.data;
setTokens(data);
})
.catch(function (error) {
console.log(error);
});
}
navigate("/home");
};

//when any input field in the formData state is updated it will take a copy of the initial state
Expand Down
41 changes: 41 additions & 0 deletions client/src/components/HiddenContent/HiddenContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { useState, useEffect } from "react";
import AuthForm from "../AuthForm/AuthForm";
import axios from "axios";

function HiddenContent(props: any) {
const { tokens } = props;
const refreshToken = tokens.newRefreshTkn;
// console.log(refreshToken);
// const MINUTE_MS = 300000;
// const test = 3000;

useEffect(() => {
const interval = setInterval(() => {
if (Object.keys(tokens).length === 0) {
axios
.post("http:https://localhost:5001/user/get-refresh-token", refreshToken)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
}, test);

return () => clearInterval(interval); // This represents the unmount function, in which you need to clear your interval to prevent memory leaks.
}, []);

return (
<>
<h1>HiddenContent</h1>
<p>
This is a page that can only be accessed via auth. It has refresh token
wrapped around this so that it will consistently check if the token is
expired.
</p>
</>
);
}

export default HiddenContent;
17 changes: 9 additions & 8 deletions client/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import React from "react";
import React, { useState } from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import "./App.css";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import HiddenContent from "./components/HiddenContent/HiddenContent";
import AuthForm from "./components/AuthForm/AuthForm";
import App from "./App";
import reportWebVitals from "./reportWebVitals";

const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);

root.render(
// <React.StrictMode>
<App />
<BrowserRouter>
<App />
</BrowserRouter>
// </React.StrictMode>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
15 changes: 0 additions & 15 deletions client/src/reportWebVitals.ts

This file was deleted.

1 change: 1 addition & 0 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const refreshToken = id => {
};

export const getRefreshToken = async (req, res) => {
console.log(req.body);
const refreshTkn = req.body.refreshToken;
if (!refreshTkn) {
return res.status(401).json("Token is required!");
Expand Down