Refactor code structure and remove redundant sections for improved readability and maintainability
All checks were successful
Deploy tt-tagebuch / deploy (push) Successful in 47s
All checks were successful
Deploy tt-tagebuch / deploy (push) Successful in 47s
This commit is contained in:
@@ -27,8 +27,8 @@ const activate = async (req, res, next) => {
|
||||
|
||||
const loginUser = async (req, res, next) => {
|
||||
try {
|
||||
const { email, password } = req.body;
|
||||
const result = await login(email, password);
|
||||
const { email, password, rememberMe } = req.body;
|
||||
const result = await login(email, password, { rememberMe });
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
@@ -37,7 +37,7 @@ const loginUser = async (req, res, next) => {
|
||||
|
||||
const logoutUser = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.headers['authorization']?.split(' ')[1];
|
||||
const token = req.headers['authorization']?.split(' ')[1] || req.headers.authcode;
|
||||
const result = await logout(token);
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
|
||||
@@ -65,7 +65,7 @@ const activateUser = async (activationCode) => {
|
||||
return user;
|
||||
};
|
||||
|
||||
const login = async (email, password) => {
|
||||
const login = async (email, password, options = {}) => {
|
||||
if (!email || !password) {
|
||||
const err = new Error('Email und Passwort sind erforderlich.');
|
||||
err.status = 400;
|
||||
@@ -83,13 +83,17 @@ const login = async (email, password) => {
|
||||
err.status = 403;
|
||||
throw err;
|
||||
}
|
||||
const token = jwt.sign({ userId: user.id }, process.env.JWT_SECRET, { expiresIn: '3h' });
|
||||
const rememberMe = Boolean(options.rememberMe);
|
||||
const expiresInMs = rememberMe ? 90 * 24 * 3600 * 1000 : 3 * 3600 * 1000;
|
||||
const expiresIn = rememberMe ? '90d' : '3h';
|
||||
const expiresAt = new Date(Date.now() + expiresInMs);
|
||||
const token = jwt.sign({ userId: user.id }, process.env.JWT_SECRET, { expiresIn });
|
||||
await UserToken.create({
|
||||
userId: user.id,
|
||||
token,
|
||||
expiresAt: new Date(Date.now() + 3 * 3600 * 1000),
|
||||
expiresAt,
|
||||
});
|
||||
return { token };
|
||||
return { token, expiresAt: expiresAt.toISOString() };
|
||||
};
|
||||
|
||||
const logout = async (token) => {
|
||||
|
||||
Reference in New Issue
Block a user