mirror of
https://github.com/nowcommunity/nowdatabase.git
synced 2026-10-12 19:13:52 +03:00
Write date to last login
This commit is contained in:
@@ -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,
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user