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

add secret client #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 13 additions & 3 deletions .github/workflows/fly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ on:
push:
branches:
- main

env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

jobs:
deploy:
name: Deploy app
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only
- name: Checkout code
uses: actions/checkout@v3
- name: Upload environment file
uses: actions/upload-env-file@v1
with:
ENV_FILE: ${{ secrets.ENV_FILE }}
- name: Deploy app
uses: superfly/flyctl-actions/setup-flyctl@master
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
run: flyctl deploy --remote-only
45 changes: 45 additions & 0 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 @@ -30,6 +30,7 @@
"cookie-parser": "^1.4.6",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-validator": "^6.14.3",
"node-fetch": "^2.6.9"
}
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const server = require('./server');
const {server} = require('./server');

const PORT = process.env.PORT || 8080;
server.listen(PORT, () => {
Expand Down
19 changes: 15 additions & 4 deletions src/routes/userEvents.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { addEventForm } = require('../templates/forms');
const { html } = require('../templates/html');
const { navbar } = require('../templates/nav');
const { validationResult } = require('express-validator');

const dbEventsHandler = require('../model/event');

Expand All @@ -12,13 +13,23 @@ function addEvent(req, res) {
res.send(html(title, navBar, content));
}

function postEvent(req, res) {
function postEvent(req, res, next) {
console.log(req.session);
const errors = validationResult(req);

if (!errors.isEmpty()) {
//return an error response with the validation errors
return res.status(400).json({ errors: errors.array() });
}
const { title, content, date, address } = req.body;
const userId = req.session.user_id;
dbEventsHandler.createEvent(title, content, date, address, userId); //1 will be session user id
dbEventsHandler.addEvent(title, content, date, address)
.then((event) => {
res.status(200).json({ message: 'Event added successfully', event });
})
.catch((err) => {
next(err);
});

res.redirect('/');
}

module.exports = { addEvent, postEvent };
43 changes: 37 additions & 6 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express');
const path = require('path');
const server = express();


const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');

Expand All @@ -14,6 +15,10 @@ const logOut = require('./routes/log-out');

const { getSession, removeSession } = require('./model/session');
const { socialAuth } = require('./routes/social-auth');
const { check, validationResult } = require('express-validator');




server.use(bodyParser.urlencoded({ extended: false }));
server.use(express.static(path.join(__dirname, 'public')));
Expand All @@ -24,13 +29,32 @@ server.get('/', home.get);
server.get('/log-in', logIn.get);
server.post('/log-in', logIn.post);
server.get('/sign-up', signUp.get);
server.post('/sign-up', signUp.post);
server.post(
'/sign-up',
[
check('email', 'Please enter a VValid email address').isEmail(),
check('password', 'Password must be AAat least 8 characters long').isLength({ min: 8 })
],
signUp.post
);

server.post('/log-out', logOut.post);

server.get('/add-event', addEvent); //add middleware
server.post('/add-event', postEvent); //add middleware
//
server.get('/add-event', addEvent); // middleware
server.post(
'/add-event',
[
check('title', 'Title is required').not().isEmpty(),
check('content', 'Content is required').not().isEmpty(),
check('date', 'Date is required').not().isEmpty(),
check('address', 'Address is required').not().isEmpty()
],
postEvent
);

server.get('/auth', socialAuth);
server.get('/auth', socialAuth);
server.post('/auth', socialAuth);

function sessions(req, res, next) {
const sid = req.signedCookies.sid; //undefined if there is not a sid
Expand All @@ -40,12 +64,19 @@ function sessions(req, res, next) {
const today = new Date();
if (expiry < today) {
removeSession(sid);
res.clearCookie(sid);
res.clearCookie('sid');
} else {
req.session = session;
}
}
next();
}

module.exports = server;
function confirmLogin(req, res, next) {
const isLoggedIn = req.session;
if (isLoggedIn) {
return res.redirect('/');
}
next();
}
module.exports = {server, validationResult, sessions, confirmLogin};
2 changes: 1 addition & 1 deletion src/templates/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function userForm(path, errors = {}, values = {}) {
<form method="POST" action="${path}">
<div class="form-item">
<label for="email">email</label>
<input type="email" id="email" name="email" required>
<input type="text" id="email" name="email" required>
</div>
<div class="form-item">
<label for="password">password</label>
Expand Down