feat: join first favorite on login

This commit is contained in:
cha0s 2020-07-25 08:55:22 -05:00
parent 76ae6fdb1b
commit 662d8c4880

View File

@ -3,6 +3,8 @@ import {randomBytes} from 'crypto';
import passport from 'passport'; import passport from 'passport';
import {joinChannel} from '~/common/channel';
export default function userRoutes(app) { export default function userRoutes(app) {
app.get('/auth/reddit', (req, res, next) => { app.get('/auth/reddit', (req, res, next) => {
req.session.state = randomBytes(32).toString('hex'); req.session.state = randomBytes(32).toString('hex');
@ -16,9 +18,27 @@ export default function userRoutes(app) {
next(error); next(error);
return; return;
} }
passport.authenticate('reddit', { passport.authenticate('reddit', (error, user) => {
successRedirect: '/chat', if (error) {
failureRedirect: '/login', next(error);
return;
}
if (!user) {
res.redirect('/');
}
req.login(user, async (error) => {
if (error) {
next(error);
return;
}
const favorites = await user.favorites();
const channels = favorites.filter(({type}) => 'r' === type).map(joinChannel);
if (channels.length > 0) {
res.redirect(`/chat${channels[0]}`);
return;
}
res.redirect('/chat');
});
})(req, res, next); })(req, res, next);
}); });
} }