forked from sarahforest/cs361
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.js
77 lines (65 loc) · 2.38 KB
/
profile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const express = require('express');
const mysql = require('./dbcon.js');
const { requireAuth } = require('./middleware.js');
const Utils = require('./utils.js');
var CryptoJS = require('crypto-js');
var bodyParser = require('body-parser');
const config = require('./config.js');
const router = express.Router();
router.get('/', requireAuth, function(req, res) {
const context = {};
context.userId = req.user.id;
context.name = req.user.name;
context.email = req.user.email;
//console.log(req.user);
res.render('profile', context);
});
router.post('/edit', requireAuth, function(req, res) {
//console.log('Edit profile called', req.body)
var sql = "SELECT id from users where email = ? and id != ?";
var inserts = [req.body.email, req.body.id];
sql = mysql.pool.query(sql, inserts, function(error, result, fields){
if(error){
res.write(JSON.stringify(error));
res.status(400);
res.end();
}
// if email address exists, render error on page.
// don't update profile.
else if (result[0]) {
var context = {
errors: 'Email address already exists. Profile changes not saved.',
userId: req.body.id,
email: req.body.original_email,
name: req.body.original_name
}
res.render('profile', context);
}
else {
if (req.body.password !== '') {
var ciphertext = CryptoJS.AES.encrypt(req.body.password, config.CRYPTO_SECRET).toString();
sql = "UPDATE users " +
"SET name = ?, " +
"email = ?, " +
"password = ? " +
"WHERE id = ?";
inserts = [req.body.name, req.body.email, ciphertext, req.body.id];
} else {
sql = "UPDATE users " +
"SET name = ?, " +
"email = ? " +
"WHERE id = ?";
inserts = [req.body.name, req.body.email, req.body.id];
}
sql = mysql.pool.query(sql, inserts, function(error, result, fields){
if(error){
res.write(JSON.stringify(error));
res.status(400);
res.end();
}
res.redirect('/profile');
});
}
});
});
module.exports = router;