Improvement of logout. Added Sacrital Service. Added website link for event places and direct link to other websites in worship overview

This commit is contained in:
Torsten Schulz
2024-09-06 16:34:17 +02:00
parent a869f2d16a
commit 5c6cfa41ab
14 changed files with 139 additions and 32 deletions

View File

@@ -13,7 +13,7 @@ const sequelize = new Sequelize('miriamgemeinde', 'miriam_user', 'qTCTTWwpEwy3vP
/SequelizeInvalidConnectionError/,
/SequelizeConnectionTimedOutError/
],
max: 5 // Maximal 5 Versuche
max: 5
},
pool: {
max: 5,

View File

@@ -1,18 +1,16 @@
const bcrypt = require('bcryptjs');
const { User } = require('../models');
const jwt = require('jsonwebtoken');
const { addTokenToBlacklist } = require('../utils/blacklist');
exports.register = async (req, res) => {
const { name, email, password } = req.body;
if (!name || !email || !password) {
return res.status(400).json({ message: 'Alle Felder sind erforderlich' });
}
try {
const hashedPassword = await bcrypt.hash(password, 10);
const user = await User.create({ name, email, password: hashedPassword, active: true });
res.status(201).json({ message: 'Benutzer erfolgreich registriert', user });
} catch (error) {
if (error.name === 'SequelizeUniqueConstraintError') {
@@ -27,28 +25,36 @@ exports.login = async (req, res) => {
if (!email || !password) {
return res.status(400).json({ message: 'Email und Passwort sind erforderlich' });
}
try {
const user = await User.findOne({ where: { email } });
if (!user) {
return res.status(401).json({ message: 'Ungültige Anmeldedaten' });
}
const validPassword = await bcrypt.compare(password, user.password);
if (!validPassword) {
return res.status(401).json({ message: 'Ungültige Anmeldedaten' });
}
if (!user.active) {
return res.status(403).json({ message: 'Benutzerkonto ist nicht aktiv' });
}
const token = jwt.sign({ id: user.id, name: user.name, email: user.email }, 'zTxVgptmPl9!_dr%xxx9999(dd)', { expiresIn: '1h' });
res.status(200).json({ message: 'Login erfolgreich', token, 'user': user });
} catch (error) {
res.status(500).json({ message: 'Ein Fehler ist aufgetreten' });
}
};
exports.logout = async (req, res) => {
const authHeader = req.header('Authorization');
if (!authHeader) {
return res.status(400).json({ message: 'Kein Token bereitgestellt' });
}
const token = authHeader.replace('Bearer ', '');
try {
addTokenToBlacklist(token);
res.status(200).json({ message: 'Logout erfolgreich' });
} catch (error) {
console.log(error);
res.status(500).json({ message: 'Ein Fehler ist beim Logout aufgetreten' });
}
};

View File

@@ -1,14 +1,40 @@
const { Worship, EventPlace, Sequelize } = require('../models');
const { Op, fn, literal } = require('sequelize'); // Importieren Sie die Operatoren von Sequelize
const { Worship, EventPlace, Sequelize, sequelize } = require('../models');
const { Op, fn, literal } = require('sequelize');
const jwt = require('jsonwebtoken');
const { isTokenBlacklisted, addTokenToBlacklist } = require('../utils/blacklist');
function isAuthorized(req) {
const authHeader = req.header('Authorization');
if (!authHeader) {
return false;
}
const token = authHeader.replace('Bearer ', '');
if (isTokenBlacklisted(token)) {
console.log('Token is blacklisted');
return false;
}
try {
const decoded = jwt.verify(token, 'zTxVgptmPl9!_dr%xxx9999(dd)');
req.user = decoded;
return true;
} catch (err) {
console.log('Token verification failed, adding to blacklist:', err.message);
addTokenToBlacklist(token);
return false;
}
}
exports.getAllWorships = async (req, res) => {
try {
const authorized = isAuthorized(req);
const worships = await Worship.findAll({
where: {
date: {
[Op.gt]: literal("DATE_SUB(NOW(), INTERVAL 4 WEEK)")
},
},
attributes: authorized ? undefined : { exclude: ['sacristanService'] },
order: [
['date', 'DESC']
],
@@ -69,14 +95,15 @@ exports.getFilteredWorships = async (req, res) => {
[Sequelize.Op.in]: locations
}
}
where.date = {
[Op.gte]: fn('CURDATE'),
};
try {
const authorized = isAuthorized(req);
console.log(authorized);
const worships = await Worship.findAll({
where,
attributes: authorized ? undefined : { exclude: ['sacristanService'] },
include: {
model: EventPlace,
as: 'eventPlace',

View File

@@ -1,12 +1,15 @@
const jwt = require('jsonwebtoken');
const { isTokenBlacklisted } = require('../utils/blacklist');
const authMiddleware = (req, res, next) => {
const authHeader = req.header('Authorization');
if (!authHeader) {
return res.status(401).json({ message: 'Zugriff verweigert. Kein Token vorhanden.' });
}
const token = authHeader.replace('Bearer ', '');
if (isTokenBlacklisted(token)) {
return res.status(401).json({ message: 'Token wurde gesperrt.' });
}
try {
const decoded = jwt.verify(token, 'zTxVgptmPl9!_dr%xxx9999(dd)');
req.user = decoded;

View File

@@ -21,7 +21,11 @@ module.exports = (sequelize) => {
backgroundColor: {
type: DataTypes.STRING,
allowNull: true
}
},
website: {
type: DataTypes.STRING,
allowNull: true
},
}, {
tableName: 'event_places'
});

View File

@@ -55,7 +55,12 @@ module.exports = (sequelize) => {
type: DataTypes.STRING,
defaultValue: '',
allowNull: false
}
},
sacristanService: {
type: DataTypes.STRING(100),
allowNull: true,
field: 'sacristan_service'
},
}, {
tableName: 'worships',
timestamps: true

View File

@@ -1,8 +1,10 @@
const express = require('express');
const router = express.Router();
const authController = require('../controllers/authController');
const authMiddleware = require('../middleware/authMiddleware');
router.post('/register', authController.register);
router.post('/login', authController.login);
router.post('/logout', authMiddleware, authController.logout);
module.exports = router;

View File

@@ -24,7 +24,7 @@ axios.interceptors.response.use(
},
error => {
if (error.response && error.response.status === 401) {
store.commit('logout');
store.dispatch('logout');
router.push('/');
}
return Promise.reject(error);

View File

@@ -21,8 +21,12 @@ export default {
},
methods: {
...mapActions(['logout']),
navigateToLogin() {
this.$router.push('/login');
async handleLogout() {
try {
await this.logout();
} catch (error) {
console.error('Fehler beim Logout:', error);
}
}
}
};

View File

@@ -1,24 +1,32 @@
<template>
<div>
<table v-if="worships.length" class="worships">
<tr v-for="worship in worships" :key="worship.id" :style="worship.eventPlace && worship.eventPlace.backgroundColor ? `background-color:${worship.eventPlace.backgroundColor}` : ''">
<tr v-for="worship in worships" :key="worship.id"
:style="worship.eventPlace && worship.eventPlace.backgroundColor ? `background-color:${worship.eventPlace.backgroundColor}` : ''">
<td>
<div>{{ formatDate(worship.date) }}</div>
<div>{{ worship.dayName }}</div>
</td>
<td>
<div v-if="worship.neighborInvitation" class="neighborhood-invitation">Einladung zum Gottesdienst im Nachbarschaftsraum:</div>
<div v-if="worship.neighborInvitation" class="neighborhood-invitation">Einladung zum Gottesdienst im
Nachbarschaftsraum:</div>
<h3>
<span :class="worship.highlightTime ? 'highlight-time' : ''">{{ formatTime(worship.time) }}</span>&nbsp;-&nbsp;
<span :class="worship.highlightTime ? 'highlight-time' : ''">{{ formatTime(worship.time)
}}</span>&nbsp;-&nbsp;
{{ !worship.neighborInvitation ? worship.title : `Gottesdienst in ${worship.eventPlace.name}` }}
</h3>
<div v-if="worship.organizer">Gestaltung: {{ worship.organizer }}</div>
<div v-if="worship.sacristanService" class="internal-information">Küsterdienst: {{ worship.sacristanService }}</div>
<div v-if="worship.collection">Kollekte: {{ worship.collection }}</div>
<div v-if="worship.address">{{ worship.address }}</div>
<div v-if="!worship.address && worship.eventPlace.id && worship.eventPlace.id">
Adresse: {{ worship.eventPlace.name }}, {{ worship.eventPlace.street }}, {{ worship.eventPlace.city }}
Adresse: {{ worship.eventPlace.name }}, {{ worship.eventPlace.street }}, {{
worship.eventPlace.city }}
</div>
<div v-if="worship.selfInformation" class="selfinformation">Bitte informieren Sie sich auch auf den
<a v-if="worship.eventPlace.website" :href="worship.eventPlace.website" target="_blank">Internetseiten dieser Gemeinde!</a><span
v-else>Internetseiten dieser Gemeinde!</span>
</div>
<div v-if="worship.selfInformation" class="selfinformation">Bitte informieren Sie sich auch auf den Internetseiten dieser Gemeinde!</div>
</td>
</tr>
</table>
@@ -85,4 +93,11 @@ table.worships td div{
font-weight: bold;
color: #0020e0;
}
a {
color: #0020e0;
}
.internal-information {
color: #e45;
font-style: italic;
}
</style>

View File

@@ -10,6 +10,8 @@
<input type="text" id="zipcode" v-model="newEventPlace.zipcode" placeholder="PLZ" required>
<label for="city">Stadt:</label>
<input type="text" id="city" v-model="newEventPlace.city" placeholder="Stadt" required>
<label for="city">Webseite:</label>
<input type="text" id="website" v-model="newEventPlace.website" placeholder="Webseite" required>
<label for="backgroundColor">Hintergrundfarbe:</label>
<input type="color" id="backgroundColor" v-model="newEventPlace.backgroundColor">
<button type="submit">Speichern</button>
@@ -47,7 +49,8 @@ export default {
street: '',
zipcode: '',
city: '',
backgroundColor: '#ffffff'
backgroundColor: '#ffffff',
website: '',
},
editMode: false,
editId: null
@@ -87,7 +90,8 @@ export default {
street: '',
zipcode: '',
city: '',
backgroundColor: '#ffffff'
backgroundColor: '#ffffff',
website: '',
};
this.editMode = false;
this.editId = null;

View File

@@ -21,6 +21,9 @@
<label for="organizer">Gestalter:</label>
<input type="text" id="organizer" v-model="worshipData.organizer">
<label for="sacristanService">Küsterdienst:</label>
<input type="text" id="sacristanService" v-model="worshipData.sacristanService">
<label for="collection">Kollekte:</label>
<input type="text" id="collection" v-model="worshipData.collection">
@@ -77,7 +80,9 @@ export default {
selfInformation: false,
highlightTime: false,
neighborInvitation: false,
introLine: ''
introLine: '',
sacristanService: '',
website: '',
},
selectedEventPlace: null,
editMode: false,

View File

@@ -94,9 +94,15 @@ export default createStore({
login({ commit }, { user, token }) {
commit('setLogin', { user, token });
},
logout({ commit }) {
async logout({ commit }) {
try {
await axios.post('/auth/logout');
} catch (error) {
console.error('Fehler beim Logout:', error);
} finally {
commit('logout');
}
}
},
getters: {
isLoggedIn: state => state.isLoggedIn,

26
utils/blacklist.js Normal file
View File

@@ -0,0 +1,26 @@
const blacklist = new Map();
const EXPIRATION_TIME = 24 * 60 * 60 * 1000; // 24 Stunden in Millisekunden
function cleanupBlacklist() {
const now = Date.now();
for (const [token, timestamp] of blacklist) {
if (now - timestamp > EXPIRATION_TIME) {
blacklist.delete(token);
}
}
}
function addTokenToBlacklist(token) {
cleanupBlacklist(); // Bereinige alte Einträge
blacklist.set(token, Date.now());
}
function isTokenBlacklisted(token) {
cleanupBlacklist(); // Bereinige alte Einträge
return blacklist.has(token);
}
module.exports = {
addTokenToBlacklist,
isTokenBlacklisted,
};