working cookie authentication
This commit is contained in:
parent
f32a949f25
commit
4fb8db3d53
27
app.js
27
app.js
@ -9,8 +9,10 @@ var streamsRouter = require('./routes/getStreams');
|
|||||||
var getUserAccounts = require('./routes/getUserAccounts')
|
var getUserAccounts = require('./routes/getUserAccounts')
|
||||||
var singleUserCheck = require('./routes/singleCheck')
|
var singleUserCheck = require('./routes/singleCheck')
|
||||||
var addAccount = require('./routes/addAccount')
|
var addAccount = require('./routes/addAccount')
|
||||||
|
var readCookie = require('./routes/readCookie')
|
||||||
|
var login = require('./routes/login')
|
||||||
var app = express();
|
var app = express();
|
||||||
// const basicAuth = require('express-basic-auth')
|
const basicAuth = require('express-basic-auth')
|
||||||
|
|
||||||
|
|
||||||
// view engine setup
|
// view engine setup
|
||||||
@ -20,19 +22,26 @@ app.set('view engine', 'jade');
|
|||||||
app.use(logger('dev'));
|
app.use(logger('dev'));
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(express.urlencoded({ extended: false }));
|
app.use(express.urlencoded({ extended: false }));
|
||||||
app.use(cookieParser());
|
app.use(cookieParser('82e4e438a0705fabf61f9854e3b575af'));
|
||||||
app.use(express.static(path.join(__dirname, 'public')));
|
app.use(express.static(path.join(__dirname, 'public')));
|
||||||
|
const users = {
|
||||||
|
users: {
|
||||||
|
'Karl': 'TEST',
|
||||||
|
'Darren': 'TEST',
|
||||||
|
'Duly': 'TEST',
|
||||||
|
},
|
||||||
|
challenge: true,
|
||||||
|
realm: 'foo',
|
||||||
|
}
|
||||||
|
|
||||||
app.use('/', indexRouter);
|
app.use('/', indexRouter);
|
||||||
|
app.use('/login', basicAuth(users), login)
|
||||||
app.use('/getStreams', streamsRouter);
|
app.use('/getStreams', streamsRouter);
|
||||||
app.use('/getUserAccounts', getUserAccounts);
|
app.use('/getUserAccounts', getUserAccounts);
|
||||||
app.use('/singleCheck', singleUserCheck)
|
app.use('/singleCheck', basicAuth(users), singleUserCheck)
|
||||||
app.use('/addAccount', addAccount)
|
app.use('/addAccount', basicAuth(users), addAccount)
|
||||||
// app.use(basicAuth({
|
app.use('/readCookie', readCookie)
|
||||||
// users: { 'BBLBTV': 'BBLBTV' },
|
|
||||||
// challenge: true,
|
|
||||||
// realm: 'foo',
|
|
||||||
// }))
|
|
||||||
|
|
||||||
// catch 404 and forward to error handler
|
// catch 404 and forward to error handler
|
||||||
app.use(function (req, res, next) {
|
app.use(function (req, res, next) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
|
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
|
||||||
import { Navigation, Footer, Home, About, ServerList } from "./components";
|
import { Navigation, Footer, Home, Accounts, ServerList } from "./components";
|
||||||
function App() {
|
function App() {
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div className="App">
|
||||||
@ -8,7 +8,7 @@ function App() {
|
|||||||
<Navigation />
|
<Navigation />
|
||||||
<Switch>
|
<Switch>
|
||||||
<Route path="/" exact component={() => <Home />} />
|
<Route path="/" exact component={() => <Home />} />
|
||||||
<Route path="/about" exact component={() => <About />} />
|
<Route path="/Accounts" exact component={() => <Accounts />} />
|
||||||
<Route path="/ServerList" exact component={() => <ServerList />} />
|
<Route path="/ServerList" exact component={() => <ServerList />} />
|
||||||
</Switch>
|
</Switch>
|
||||||
<Footer />
|
<Footer />
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import MTable from "./accountTable";
|
import MTable from "./accountTable";
|
||||||
|
|
||||||
function About() {
|
function Accounts() {
|
||||||
return (
|
return (
|
||||||
<div style={{ padding: "30px" }}>
|
<div style={{ padding: "30px" }}>
|
||||||
<MTable/>
|
<MTable/>
|
||||||
@ -9,4 +9,4 @@ function About() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default About;
|
export default Accounts;
|
@ -1,30 +1,103 @@
|
|||||||
import React from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
|
import ReactDOM from "react-dom";
|
||||||
|
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
|
function View(props) {
|
||||||
|
const { screen, setScreen } = props;
|
||||||
|
|
||||||
|
const [data, setData] = useState();
|
||||||
|
|
||||||
|
const deleteCookie = async () => {
|
||||||
|
try {
|
||||||
|
await axios.get("/readCookie/clear");
|
||||||
|
setScreen("auth");
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getData = async () => {
|
||||||
|
try {
|
||||||
|
const res = await axios.get("/get-data");
|
||||||
|
console.log(res.data);
|
||||||
|
setData(res.data);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
function Home() {
|
|
||||||
return (
|
return (
|
||||||
<div className="home">
|
<div>
|
||||||
<div class="container">
|
<p>{screen}</p>
|
||||||
<div class="row align-items-center my-5">
|
<p>{data}</p>
|
||||||
<div class="col-lg-7">
|
<button onClick={getData}>Get Data</button>
|
||||||
<img
|
<button onClick={deleteCookie}>Logout</button>
|
||||||
class="img-fluid rounded mb-4 mb-lg-0"
|
|
||||||
src="http://placehold.it/900x400"
|
|
||||||
alt=""
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div class="col-lg-5">
|
|
||||||
<h1 class="font-weight-light">Home</h1>
|
|
||||||
<p>
|
|
||||||
Lorem Ipsum is simply dummy text of the printing and typesetting
|
|
||||||
industry. Lorem Ipsum has been the industry's standard dummy text
|
|
||||||
ever since the 1500s, when an unknown printer took a galley of
|
|
||||||
type and scrambled it to make a type specimen book.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Home;
|
function App() {
|
||||||
|
const [screen, setScreen] = useState("auth");
|
||||||
|
const [username, setUsername] = useState();
|
||||||
|
const [password, setPassword] = useState();
|
||||||
|
|
||||||
|
const auth = async () => {
|
||||||
|
try {
|
||||||
|
const res = await axios.get("/login", {
|
||||||
|
auth: { username, password },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.data.screen !== undefined) {
|
||||||
|
setScreen('/about');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const readCookie = async () => {
|
||||||
|
try {
|
||||||
|
const res = await axios.get("/readCookie");
|
||||||
|
|
||||||
|
if (res.data.screen !== undefined) {
|
||||||
|
setScreen(res.data.screen);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
setScreen("auth");
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
readCookie();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="App">
|
||||||
|
{screen === "auth" ? (
|
||||||
|
<div>
|
||||||
|
<label>Username: </label>
|
||||||
|
<br />
|
||||||
|
<input type="text" onChange={(e) => setUsername(e.target.value)} />
|
||||||
|
<br />
|
||||||
|
<label>Password: </label>
|
||||||
|
<br />
|
||||||
|
<input
|
||||||
|
type="password"
|
||||||
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
|
/>
|
||||||
|
<br />
|
||||||
|
<button onClick={auth}>Login</button>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<View screen={screen} setScreen={setScreen} />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default App;
|
||||||
|
|
||||||
|
const rootElement = document.getElementById("root");
|
||||||
|
ReactDOM.render(<App />, rootElement);
|
||||||
|
@ -24,11 +24,11 @@ function Navigation(props) {
|
|||||||
</li>
|
</li>
|
||||||
<li
|
<li
|
||||||
class={`nav-item ${
|
class={`nav-item ${
|
||||||
props.location.pathname === "/about" ? "active" : ""
|
props.location.pathname === "/accounts" ? "active" : ""
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
<Link class="nav-link" to="/about">
|
<Link class="nav-link" to="/accounts">
|
||||||
About
|
Accounts
|
||||||
</Link>
|
</Link>
|
||||||
</li>
|
</li>
|
||||||
<li
|
<li
|
||||||
|
@ -32,8 +32,7 @@ export default class MatDataTable extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount(prevProps) {
|
componentDidMount(prevProps) {
|
||||||
const user = "Karl";
|
const url = `/getUserAccounts`;
|
||||||
const url = `/getUserAccounts?userName=${user}`;
|
|
||||||
axios.get(url).then((results) => {
|
axios.get(url).then((results) => {
|
||||||
console.log(results);
|
console.log(results);
|
||||||
console.log(results.data);
|
console.log(results.data);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
export { default as Navigation } from "./Navigation";
|
export { default as Navigation } from "./Navigation";
|
||||||
export { default as Footer } from "./Footer";
|
export { default as Footer } from "./Footer";
|
||||||
export { default as Home } from "./Home";
|
export { default as Home } from "./Home";
|
||||||
export { default as About } from "./About";
|
export { default as Accounts } from "./Accounts";
|
||||||
export { default as ServerList } from "./ServerList";
|
export { default as ServerList } from "./ServerList";
|
@ -57,7 +57,7 @@ async function main() {
|
|||||||
console.log('Finished')
|
console.log('Finished')
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
// main()
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
singleCheck,
|
singleCheck,
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
"@material-ui/icons": "^4.11.2",
|
"@material-ui/icons": "^4.11.2",
|
||||||
"axios": "^0.21.1",
|
"axios": "^0.21.1",
|
||||||
"bcrypt": "^5.0.0",
|
"bcrypt": "^5.0.0",
|
||||||
"cookie-parser": "~1.4.4",
|
"cookie-parser": "^1.4.5",
|
||||||
"cryptr": "^6.0.2",
|
"cryptr": "^6.0.2",
|
||||||
"debug": "~2.6.9",
|
"debug": "~2.6.9",
|
||||||
"express": "~4.16.1",
|
"express": "~4.16.1",
|
||||||
|
@ -5,9 +5,12 @@ const { getUserAccounts } = require('../lib/getUser')
|
|||||||
|
|
||||||
/* POST postUser page. */
|
/* POST postUser page. */
|
||||||
router.get('/', async function (req, res, next) {
|
router.get('/', async function (req, res, next) {
|
||||||
let data = await getUserAccounts(req.query.userName)
|
if (req.signedCookies.user === undefined) {
|
||||||
|
res.send('Cookie Not Set')
|
||||||
|
} else {
|
||||||
|
let data = await getUserAccounts(req.signedCookies.user)
|
||||||
res.send(data)
|
res.send(data)
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
26
routes/login.js
Normal file
26
routes/login.js
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var router = express.Router();
|
||||||
|
|
||||||
|
/* GET home page. */
|
||||||
|
router.get('/', function (req, res, next) {
|
||||||
|
const options = {
|
||||||
|
httpOnly: true,
|
||||||
|
signed: true,
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (req.auth.user === 'Karl') {
|
||||||
|
res.cookie('user', 'Karl', options)
|
||||||
|
}
|
||||||
|
else if (req.auth.user === 'Darren') {
|
||||||
|
res.cookie('user', 'Darren', options)
|
||||||
|
}
|
||||||
|
else if (req.auth.user === 'Duly') {
|
||||||
|
res.cookie('user', 'Duly', options)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.render('index', { title: 'No Auth' });
|
||||||
|
}
|
||||||
|
res.render('index', { title: 'Express' });
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
19
routes/readCookie.js
Normal file
19
routes/readCookie.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var router = express.Router();
|
||||||
|
|
||||||
|
/* POST postUser page. */
|
||||||
|
router.get('/', async function (req, res, next) {
|
||||||
|
if (req.signedCookies.user === 'Karl') {
|
||||||
|
res.send(req.signedCookies);
|
||||||
|
} else if (req.signedCookies.name === 'user') {
|
||||||
|
res.send({ screen: 'user' });
|
||||||
|
} else {
|
||||||
|
res.send('No Cookie Set');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get('/clear', (req, res) => {
|
||||||
|
res.clearCookie('user').end();
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
Loading…
x
Reference in New Issue
Block a user