Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/samdsk/lab-pwm
Browse files Browse the repository at this point in the history
  • Loading branch information
samdsk committed Apr 4, 2023
2 parents a8843d6 + 4672807 commit 0715ffc
Show file tree
Hide file tree
Showing 141 changed files with 5,819 additions and 34,538 deletions.
38 changes: 38 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ wp-content/themes/twenty*/
# ignore node dependency directories
node_modules/

# ignore env
# ignore env
.env
credentials.txt
*.json

!utils/tweets_data.json
# ignore log files and databases
*.log
*.sql
Expand Down
37 changes: 0 additions & 37 deletions app.js

This file was deleted.

41 changes: 41 additions & 0 deletions controllers/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const recaptcha = require('../utils/recaptcha')
const sendEmail = require('../utils/sendEmail')

const getContact = async (req,res,next) =>{
if(!req.session.username || !req.session.email)
res.render('pages/contact',{contact:true})
else
res.render('pages/contact',{contact:true,logout:true})

}

const postContact = async (req,res,next) =>{
const {name,email,subject,message} = req.body
let catpcha = await recaptcha(req.body['g-recaptcha-response'])
if(!catpcha) return res.json(JSON.stringify({error:"Invalid captcha!"}))

const mail_opt = {
from:name+" : "+email,
to:process.env.EMAIL,
subject:name+" "+subject,
text:`message from ${name}
email: ${email}
subject:${subject}:
message:${message}`,
html:`
<h4 class="h4">Contact form</h4>
<h5 class="h5">Name: ${name} Email: ${email}</h5>
<h5 class="h5">Subject: ${subject}</h5>
<p>Message: ${message}</p>
`
}

await sendEmail(mail_opt).then(()=>{
return res.json(JSON.stringify({success:"Your message has been sent!"}))
}).catch((err)=>{
return res.json(JSON.stringify({error:"Failed to send message!"}))
})
}


module.exports = {getContact,postContact}
2 changes: 1 addition & 1 deletion controllers/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const getDashboard = async (req,res,next) =>{
res.redirect('/dashboard/profile')
res.redirect('/dashboard/history')
}

module.exports = getDashboard
51 changes: 51 additions & 0 deletions controllers/forgot_psw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const Auth = require('../models/Auth')
const jwt = require('jsonwebtoken')
const sendEmail = require('../utils/sendEmail')
const User = require('../models/User')

const JWT_EXP = '30m'

// FIXME change the email address

const postEmail = async (req,res,next) => {
if(req.body.email){
Auth.findOne({email:req.body.email},async (err,auth)=>{

if(err) return res.sendStatus(500)
if(!auth) return res.json(JSON.stringify({error:"Invalid email"}))

const name = await User.findById(auth._id)

const secret = process.env.Server_Secret + auth.password
const token = jwt.sign({
email:auth.email
},secret,{expiresIn:JWT_EXP})

const link = `http://${process.env.HOSTNAME}/reset-password/${auth.email}/${token}`

const mail_opt = {
from:'Twitter Analytics App',
to:sendTo,
subject:'Twitter Analytics App Password Recovery',
text:'You have requested for a password reset. Follow this link: ' + link,
html:`
<h4 class="h4">Hi ${name}!</h4>
<p>You have requested for a password reset, follow the following link</p>
<a href="${link}">${link}</a>
`
}

await sendEmail(mail_opt).then(()=>{
return res.json(JSON.stringify({success:"Email sent to "+req.body.email}))
}).catch((err)=>{
return res.json(JSON.stringify({error:"Failed to send message!"}))
})
})

}else{
return res.sendStatus(500)
}

}

module.exports = {postEmail}
38 changes: 36 additions & 2 deletions controllers/history.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
const getHistory = async (req,res,next) => {
res.render('pages/history',{logout:true,username:req.session.username})
const Auth = require('../models/Auth')
const User = require('../models/User')
const SearchResults = require('../models/SearchResults')

const getSearchedResults = async (email) => {
let user = await Auth.findOne({email:email})
let searched = await User.findOne({_id:user._id},'searched')
searched = searched.searched
const search_ids = []
const projections = `_id date name user_img username
start_date end_date followings followers total_tweets total.count`;

await Promise.all(
searched.map( async (id) => {
let temp = await SearchResults.findById(id,projections)
search_ids.push(temp)
})
)

return search_ids
}

const getHistory = async (req,res,next) => {

let results = await getSearchedResults(req.session.email)

results = results.sort( (a,b)=>{
return new Date(a.date) < new Date(b.date) ? 1 : -1
})

res.render('pages/history',{
logout:true,
username:req.session.username,
email_hash:req.session.gravatar,
results:results,
history:true,
dashboard:true,
})
}

module.exports = getHistory
36 changes: 20 additions & 16 deletions controllers/login.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
const Auth = require('../models/Auth')
const bcrypt = require('bcrypt')
const {createError} = require('../errors/customError')
const jwt = require('jsonwebtoken')
const User = require('../models/User')

const User = require('../models/User')
const SessionDuration = 1000 * 60 * 60 * 60
const md5 = require('md5')

const login = async (req,res,next) => {
const {login_email, login_password, login_remember } = req.body

Auth.findOne({email:login_email}, async (err,auth)=> {
if(auth === null) return res.redirect("/?error=Credentials are not valid")
console.log(req.body);
const {email, password, remember} = req.body

await bcrypt.compare(login_password,auth.password).then(async (check)=>{

if(!check) return res.redirect("/?error=Credentials are not valid")
Auth.findOne({email:email}, async (err,auth)=> {
if(auth == null) return res.redirect("/?error="+encodeURIComponent("Invalid credentials"))

//const token = jwt.sign({email:login_email},process.env.Server_Secret,{expiresIn:"20s"})
await bcrypt.compare(password,auth.password).then(async (check)=>{
if(!check) return res.json("/?error="+encodeURIComponent("Invalid credentials"))
console.log("here");
const username = await User.findOne({_id:auth._id}).populate('_id')
const email_hash = md5(email.trim().toLowerCase())

const username = await User.findOne({_id:auth._id}).populate('_id')
req.session.username = username.name
req.session.email = username._id.email
req.session.gravatar = email_hash
console.log(req.session);
// ! session maxage
if(login_remember === "on") req.session.cookie.maxAge = SessionDuration
if(remember === "on") req.session.cookie.maxAge = SessionDuration

req.session.save()
return res.redirect('/dashboard/history')

res.redirect('/dashboard/profile')

}).catch((err)=>{console.log(err)})
}).catch((err)=>{
res.sendStatus(500)
})
})

}
Expand Down
97 changes: 90 additions & 7 deletions controllers/profile.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,96 @@
const bcrypt = require('bcrypt')
const Auth = require("../models/Auth")
const User = require("../models/User")
const SearchResults = require('../models/SearchResults')
const recaptcha = require('../utils/recaptcha')

const getProfile = async (req,res,next) => {
req.session.email
res.render('pages/profile',
{
logout:true,
username:req.session.username,
email: req.session.email

res.render('pages/profile',{
logout:true,
username:req.session.username,
email: req.session.email,
email_hash:req.session.gravatar,
profile:true,
dashboard:true,
})
}

const postProfile = async (req,res,next) => {
if(req.session.email){

let catpcha = await recaptcha(req.body['g-recaptcha-response'])
if(!catpcha) return res.json(JSON.stringify({error:"Invalid captcha!"}))

const {password,new_password,new_password_confirm} = req.body
if(new_password !== new_password_confirm)
return res.json(JSON.stringify({error:"Passwords don't match"}))

Auth.findOne({email:req.session.email},async function(err,auth){
bcrypt.compare(password,auth.password).then(async (check)=>{

if(!check) return res.json(JSON.stringify({error:"Credentials are not valid"}))
const password = await bcrypt.hash(new_password,10)
await Auth.findByIdAndUpdate(auth._id,{password:password})

return res.json(JSON.stringify({success:"Password updated"}))
})

})
}else{
return res.sendStatus(500)
}


}

const deleteProfile = async (req,res,next) => {
if(req.session.email){
Auth.findOne({email:req.session.email},async function(err,auth){
console.log(req.body.password);
await bcrypt.compare(req.body.password,auth.password).then(async (check)=>{

if(!check) return res.json(JSON.stringify({error:"Credentials are not valid"}))
console.log("pass: psw");

let searched = await User.findById(auth._id,'searched')

await Promise.all(
searched.searched.map(async(x)=>{
SearchResults.findByIdAndRemove(x).exec((err,data)=>{
if(err) {
return res.json(JSON.stringify({error:"Profile delete: SearchResults error"}))
}
console.log("pass: search",x);
})
})
)

Auth.findByIdAndRemove(auth._id).exec((err,data)=>{
if(err) {
return res.json(JSON.stringify({error:"Profile delete: Auth error"}))
}
console.log("pass: delete auth");
})

User.findByIdAndRemove(auth._id).exec((err,data)=>{
if(err) {
return res.json(JSON.stringify({error:"Profile delete: User error"}))

}
console.log("pass: delete user");
})

req.session.destroy()
return res.json(JSON.stringify({success:"Profile has been delete successfully"}))
})

})
}else{
return res.sendStatus(500)
}
}



module.exports = getProfile
module.exports = {getProfile,postProfile,deleteProfile}
Loading

0 comments on commit 0715ffc

Please sign in to comment.