       return next err if err
       req.login req.user.set(r2.body), (err) ->
         return next err if err
-        res.redirect req.param('redirect-to')
+        res.redirect req.query['redirect-to'] or req.body['redirect-to'] or '/'
 
 headerLogin = (req, res, next) ->
   return next() if req.path is opts.logoutPath

  next()

loginBeforeTwitterLastStep = (req, res, next) ->
  passport.authenticate('twitter',
    callbackURL: "#{opts.APP_URL}#{opts.twitterLastStepPath}"
  )(req, res, next)

submitTwitterLastStep = (req, res, next) ->
  return next "No user" unless req.user
  return next "No email provided" unless req.param('email')?
  request.put("#{opts.SECURE_ARTSY_URL}/api/v1/me").send(
    email: req.param('email')
    email_confirmation: req.param('email')
    access_token: req.user.get('accessToken')
  ).end (r) ->
    err = r.error or r.body?.error_description or r.body?.error

  app.post opts.twitterLastStepPath, submitTwitterLastStep
  app.use headerLogin
  app.use addLocals
  app.get '*', ensureEmailFromTwitterSignup

ensureEmailFromTwitterSignup = (req, res, next) ->
  return next() if not req.user? or req.url is opts.twitterLastStepPath
  tmpSuffix = _.last(opts.twitterSignupTempEmail('').split('@'))
  if req.user.get('email').match tmpSuffix
    res.redirect opts.twitterLastStepPath
  else
    next()

localAuth = (req, res, next) ->
  passport.authenticate('local', (err, user, info) ->
