Skip to content

Commit

Permalink
Added an admin status and post status
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanF1nes committed Nov 16, 2023
1 parent 43818c7 commit 13b7322
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 26 deletions.
11 changes: 4 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const User = require('./models/users');
const indexRouter = require('./routes/index');
const signupRouter = require('./routes/signup');
const loginRouter = require('./routes/login');
const secretPageRouter = require('./routes/secretPage');
const adminRouter = require('./routes/admin');

const app = express();

Expand Down Expand Up @@ -49,13 +51,8 @@ passport.deserializeUser(async (id, done) => {
app.use('/', indexRouter);
app.use('/signup', signupRouter);
app.use('/login', loginRouter);
app.get('/admin', async (req, res, next) => {
const user = await User.findOneAndUpdate().populate('username').exec();
user.admin = true;
const result = await user.save();
res.redirect('/');

})
app.use('/secret', secretPageRouter);
app.use('/admin', adminRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
Expand Down
12 changes: 9 additions & 3 deletions controllers/indexController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ const User = require('../models/users');

const get_index = async (req, res) => {
const posts = await Post.find().populate('author').exec()
// const user = await User.find();
// console.log(user)
res.render('index', { title: 'Members Only | Project', user: req.user, posts: posts, admin: req.user });
const user = await User.findById(req.user)

if (user) {
const can_post = user.can_post;
res.render('index', { title: 'Members Only | Project', user: req.user, posts: posts, admin: req.user, canPost: can_post });
} else {
res.render('index', { title: 'Members Only | Project', user: req.user, posts: posts, admin: req.user })
}

}

module.exports = { get_index }
3 changes: 2 additions & 1 deletion models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ const UserSchema = new Schema({
email: { type: String, required: true },
username: { type: String, required: true },
password: { type: String, required: true },
can_post: { type: Boolean, default: false },
date_created: { type: Date },
posts: [{ type: mongoose.Schema.Types.Array, ref: 'Post' }],
admin: { type: Boolean }
admin: { type: Boolean, default: false }
})

UserSchema.virtual('name').get(() => {
Expand Down
3 changes: 3 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ a {
justify-content: center;
flex-direction: column;
}
.banner a {
font-size: 14px;
}
.links {
display: flex;
align-items: center;
Expand Down
24 changes: 24 additions & 0 deletions routes/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const express = require('express');
const router = express.Router();
const User = require('../models/users');

router.get('/', (req, res, next) => {
res.render('admin', { title: 'Members Only | Admin Page' })
})

router.post('/', async (req, res, next) => {
const admin = process.env.ADMINCODE

if (req.body.admincode == admin) {
await User.findByIdAndUpdate(
req.user._id,
{ admin: true },
{ new: true }
)
res.render('admin', { title: 'Members Only | Admin Page', success: 'You are now an admin.' })
} else {
res.render('admin', { title: 'Members Only | Admin Page', success: '' })
}
})

module.exports = router;
25 changes: 25 additions & 0 deletions routes/secretPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require('dotenv').config();
const express = require('express');
const router = express.Router();
const User = require('../models/users')

router.get('/', (req, res, next) => {
res.render('secretpage', { title: 'Members Page | Secret Page', message: '' })
})

router.post('/', async (req, res, next) => {
const secret = process.env.SECRET
// const getUser = req.user
if (req.body.secretcode == secret) {
await User.findByIdAndUpdate(
req.user._id,
{ can_post: true },
{ new: true }
)
res.render('secretpage', { title: 'Members Page | Secret Page', state: true, message: 'Congrats! You can post now!' })
} else {
res.render('secretpage', { title: 'Members Page | Secret Page', state: false, message: "Sorry that's not the secret code!" })
}
})

module.exports = router;
12 changes: 12 additions & 0 deletions views/admin.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extends layout

block content
h1 #{title}
h3 #{success}

a(href="/") Home

form(class="signup-form" method="POST")
label(for="admincode")
input(type="text" name="admincode")
button Send
22 changes: 11 additions & 11 deletions views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ block content
if (user)
h3(class="banner") Welcome to #{title} - <span class="username">#{user.username}</span>
div(class="links")
a(href="/") HOME
a(href="/logout") LOG OUT

div
form(class="post-form" method="POST")
label(for="post") Post:
textarea(name="post" required)
button Submit
a(href="/") Home
a(href="/logout") Log Out
if (user && canPost)
div
form(class="post-form" method="POST")
label(for="post") Post:
textarea(name="post" required)
button Submit
else
h3(class="banner") Welcome to #{title}
div(class="links")
a(href="/") HOME
a(href="/signup") SIGNUP
a(href="/login") LOGIN
a(href="/") Home
a(href="/signup") Sign Up
a(href="/login") Login
div
h3 Please <a href="/login"> log in </a> if you have an existing account.
h3 If not feel free to <a href="/signup">Sign up!</a>
Expand Down
4 changes: 2 additions & 2 deletions views/login.pug
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ block content

h1 #{title}
div(class="links")
a(href="/") HOME
a(href="/signup") SIGNUP
a(href="/") Home
a(href="/signup") Sign Up

form(class="signup-form" action="/login" method="POST")
label(for="username") Username:
Expand Down
7 changes: 7 additions & 0 deletions views/secretpage.pug
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ block content

h1 #{title}

if (state)
a(href="/") Home
h3 #{message}
else
a(href="/") Home
h3 #{message}

form(class="signup-form" method="POST")
label(for="secretcode")
input(type="text" name="secretcode")
Expand Down
4 changes: 2 additions & 2 deletions views/signup.pug
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ block content

h1 #{title}
div(class="links")
a(href="/") HOME
a(href="/login") LOGIN
a(href="/") Home
a(href="/login") Login

form(class="signup-form" method='POST')
label(for='firstname') First Name:
Expand Down

0 comments on commit 13b7322

Please sign in to comment.