add modified function login and add new function logout
This commit is contained in:
parent
d35f64b87e
commit
72dd0a13a2
|
@ -42,7 +42,8 @@ exports.register = async (req, res) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Penyimpanan sesi login (in-memory)
|
// Penyimpanan sesi login (in-memory)
|
||||||
const activeSessions = {}; // key: user.id, value: true/false
|
const activeSessions = {};
|
||||||
|
const sessionTimeouts = {};
|
||||||
|
|
||||||
// Login
|
// Login
|
||||||
exports.login = async (req, res) => {
|
exports.login = async (req, res) => {
|
||||||
|
@ -76,9 +77,19 @@ exports.login = async (req, res) => {
|
||||||
// 🔹 Tandai user sedang login (aktif)
|
// 🔹 Tandai user sedang login (aktif)
|
||||||
activeSessions[user.id] = true;
|
activeSessions[user.id] = true;
|
||||||
|
|
||||||
|
// 🔹 Atur timer logout otomatis setelah 5 menit (300000 ms)
|
||||||
|
if (sessionTimeouts[user.id]) {
|
||||||
|
clearTimeout(sessionTimeouts[user.id]); // Bersihkan timer lama jika ada
|
||||||
|
}
|
||||||
|
sessionTimeouts[user.id] = setTimeout(() => {
|
||||||
|
delete activeSessions[user.id];
|
||||||
|
delete sessionTimeouts[user.id];
|
||||||
|
console.log(`User ID ${user.id} otomatis logout karena timeout`);
|
||||||
|
}, 5 * 60 * 1000); // 5 menit
|
||||||
|
|
||||||
console.log("User ID dari backend:", user.id);
|
console.log("User ID dari backend:", user.id);
|
||||||
|
|
||||||
// 🔹 Kirim response dengan token dan role
|
// 🔹 Kirim response
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
message: "Login berhasil",
|
message: "Login berhasil",
|
||||||
token,
|
token,
|
||||||
|
@ -90,15 +101,19 @@ exports.login = async (req, res) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.logout = (req, res) => {
|
//logout
|
||||||
const userId = req.user.id; // Ambil dari JWT yang sudah diverifikasi
|
exports.logout = async (req, res) => {
|
||||||
|
const userId = req.user?.id;
|
||||||
// Hapus sesi aktif
|
if (userId && activeSessions[userId]) {
|
||||||
delete activeSessions[userId];
|
delete activeSessions[userId];
|
||||||
|
clearTimeout(sessionTimeouts[userId]);
|
||||||
res.status(200).json({ message: "Logout berhasil" });
|
delete sessionTimeouts[userId];
|
||||||
|
return res.status(200).json({ message: "Logout berhasil" });
|
||||||
|
}
|
||||||
|
res.status(400).json({ message: "Tidak ada sesi login aktif" });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Buat transporter Nodemailer dengan Gmail
|
// Buat transporter Nodemailer dengan Gmail
|
||||||
const createGmailTransporter = () => {
|
const createGmailTransporter = () => {
|
||||||
return nodemailer.createTransport({
|
return nodemailer.createTransport({
|
||||||
|
|
Loading…
Reference in New Issue