refactor: create empty anonymous user

This commit is contained in:
cha0s 2021-01-17 21:08:00 -06:00
parent 3a769a850e
commit a0b506ccf4

View File

@ -22,12 +22,16 @@ export default {
}); });
}, },
'@latus/db/models': () => models, '@latus/db/models': () => models,
'@latus/http/request': () => (req, res, next) => { '@latus/http/request': (latus) => (req, res, next) => {
debug('@latus/http/request: passport.initialize()'); debug('@latus/http/request: passport.initialize()');
passport.initialize()(req, res, () => { passport.initialize()(req, res, () => {
debug('@latus/http/request: passport.session()'); debug('@latus/http/request: passport.session()');
passport.session()(req, res, () => { passport.session()(req, res, () => {
req.userId = req.user ? req.user.id : 0; if (!req.user) {
const {User} = ModelMap(latus);
req.user = new User();
req.user.id = 0;
}
next(); next();
}); });
}); });
@ -47,25 +51,29 @@ export default {
const {connected} = io.of('/'); const {connected} = io.of('/');
const here = sids.filter((sid) => !!connected[sid]); const here = sids.filter((sid) => !!connected[sid]);
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
const reduced = here.reduce((r, sid) => ({...r, [sid]: connected[sid].handshake.userId}), {}); const reduced = here.reduce((r, sid) => ({...r, [sid]: connected[sid].handshake.user.id}), {});
return reduced; return reduced;
}, },
}), }),
'@latus/socket/authenticate': () => (socket, next) => { '@latus/socket/authenticate': (latus) => (socket, next) => {
debug('@latus/socket/authenticate: passport.initialize()'); debug('@latus/socket/authenticate: passport.initialize()');
passport.initialize()(socket.handshake, undefined, () => { passport.initialize()(socket.handshake, undefined, () => {
debug('@latus/socket/authenticate: passport.session()'); debug('@latus/socket/authenticate: passport.session()');
passport.session()(socket.handshake, undefined, async () => { passport.session()(socket.handshake, undefined, async () => {
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
if (!socket.handshake.user) {
const {User} = ModelMap(latus);
socket.handshake.user = new User();
socket.handshake.user.id = 0;
}
socket.handshake.login = LogOps.logIn; socket.handshake.login = LogOps.logIn;
socket.handshake.logIn = LogOps.logIn; socket.handshake.logIn = LogOps.logIn;
socket.handshake.logout = LogOps.logOut; socket.handshake.logout = LogOps.logOut;
socket.handshake.logOut = LogOps.logOut; socket.handshake.logOut = LogOps.logOut;
socket.handshake.isAuthenticated = LogOps.isAuthenticated; socket.handshake.isAuthenticated = LogOps.isAuthenticated;
socket.handshake.isUnauthenticated = LogOps.isUnauthenticated; socket.handshake.isUnauthenticated = LogOps.isUnauthenticated;
socket.handshake.userId = socket.handshake.user ? socket.handshake.user.id : 0;
/* eslint-enable no-param-reassign */ /* eslint-enable no-param-reassign */
await socket.join(`/u/${socket.handshake.userId}`); await socket.join(`/u/${socket.handshake.user.id}`);
next(); next();
}); });
}); });