diff --git a/src/server/routes/user.js b/src/server/routes/user.js index b02813d..24c13a2 100644 --- a/src/server/routes/user.js +++ b/src/server/routes/user.js @@ -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); }); }