-
-
Save charmander/0a3c300b2c26bb48b0a5e99ef65fc729 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.use(session({ | |
store: store, | |
secret: config.sessions.secret, | |
cookie: { maxage: 30 * 24 * 60 * 60 * 1000, secure: true }, | |
resave: false, | |
saveUninitialized: false | |
})) | |
app.get('/loginrefresh', function (req, res) { | |
console.log(req.session) | |
console.log(req.session.userId); | |
var isUndefined = typeof req.session.userId == 'undefined'; | |
console.log("is userId undefined", isUndefined) | |
if (typeof req.session.userId == 'undefined') { | |
console.log("session undefined") | |
return res.sendStatus(200); | |
} | |
pool.query('SELECT * FROM users WHERE id = $1', [parseInt(req.session.userId)]).then(users => { | |
console.log("why is this hit?") | |
console.log("users", users) | |
if (users.length === 0) { | |
return res.send("User was not found."); | |
} else { | |
var userData = { | |
username: users.rows[0].username, | |
userId: users.rows[0].id | |
} | |
GetMostRecentTeam(users.rows[0].id) | |
.then(function (data) { | |
userData.recentteam = data.rows; | |
console.log("data rows") | |
return res.json(userData); | |
}) | |
} | |
}) | |
}) | |
app.post('/login', function (req, res) { | |
let user; | |
pool.query('SELECT * FROM users WHERE LOWER(username) = LOWER($1)', [req.body.username]).then( (users) => { | |
if (users.length === 0) { | |
res.send("User does not exist") | |
} else { | |
user = users.rows[0]; | |
console.log("USER DATA", user) | |
return Promise.try(() => { | |
return scrypt.verifyHash(req.body.password, user.password); | |
}).then(() => { | |
return GetMostRecentTeam(user.id).then(data => { | |
req.session.userId = user.id; | |
console.log("User Id", req.session.userId) | |
if (data.rows.length == 0) { | |
return res.json({ username: user.username, userId: user.Id, recentteam: []}) | |
} | |
console.log("Get most recent team", data) | |
return res.json( {username: user.username, userId: user.Id, recentteam: data.rows }) | |
}).catch(e => { | |
console.log("error", e) | |
}) | |
}).catch(scrypt.PasswordError, (err) => { | |
res.status(401).send("User password did not match"); | |
}).catch((err) => { | |
res.status(500).send("An error occured with the authentication server."); | |
}) | |
} | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment