Files
mern-template/server/src/controllers/auth/login-with-token.ts
2024-12-14 16:14:14 +02:00

33 lines
822 B
TypeScript

import { type RequestHandler } from 'express'
import jwt from '../../utils/jwt'
import Account from '../../models/Account'
const loginWithToken: RequestHandler = async (req, res, next) => {
try {
const { uid } = req.auth || {}
// Get account from DB, password is not verified because we're already token-authorized at this point
const account = await Account.findOne({ _id: uid }).select('-password')
if (!account) {
return next({
statusCode: 400,
message: 'Bad credentials',
})
}
// Generate access token
const token = jwt.signToken({ uid: account._id, role: account.role })
res.status(200).json({
message: 'Succesfully got account',
data: account,
token,
})
} catch (error) {
next(error)
}
}
export default loginWithToken