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 {joinChannel} from '~/common/channel';
export default function userRoutes(app) {
app.get('/auth/reddit', (req, res, next) => {
req.session.state = randomBytes(32).toString('hex');
@ -16,9 +18,27 @@ export default function userRoutes(app) {
next(error);
return;
}
passport.authenticate('reddit', {
successRedirect: '/chat',
failureRedirect: '/login',
passport.authenticate('reddit', (error, user) => {
if (error) {
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);
});
}