Write date to last login

This commit is contained in:
ShootingStar91
2024-07-12 16:53:34 +03:00
parent 2eac075ffc
commit 304329bb6c
+8 -6
View File
@@ -10,29 +10,31 @@ const router = Router()
router.post('/login', async (req, res) => {
const { username, password } = req.body
const result = await nowDb.com_users.findFirst({
const foundUser = await nowDb.com_users.findFirst({
where: {
user_name: username,
},
select: { user_name: true, password: true, user_id: true, now_user_group: true },
})
const passwordMatches = result && (await bcrypt.compare(password as string, result.password as string))
const passwordMatches = foundUser && (await bcrypt.compare(password as string, foundUser.password as string))
if (!passwordMatches) return res.status(403).send()
const token = jwt.sign({ username: result.user_name, id: result.user_id }, SECRET, {
const token = jwt.sign({ username: foundUser.user_name, id: foundUser.user_id }, SECRET, {
expiresIn: LOGIN_VALID_MS,
})
const personResult = await nowDb.com_people.findFirst({ where: { user_id: result.user_id } })
const personResult = await nowDb.com_people.findFirst({ where: { user_id: foundUser.user_id } })
if (!personResult) throw new Error('User found but not person; this should not happen.')
await nowDb.com_users.update({ where: { user_id: foundUser.user_id }, data: { last_login: new Date() } })
return res.status(200).send({
token,
username: result.user_name,
role: getRole(result.now_user_group ?? ''),
username: foundUser.user_name,
role: getRole(foundUser.now_user_group ?? ''),
initials: personResult.initials,
})
})