mercredi 22 juin 2016

Flask on login redirection issue


I am sending out a confirmation token to users after they register to confirm their email. The issue I have is that I cant get to the confirm method as it requires the user to login and once they go to the login method I cant work out how to direct them back to the confirm method.

Note the code is heavily based on the (very good) book 'Flask Web Development' by Grinberg 2014.

We start here with a new user signing up:

@auth.route('/signup', methods=['GET', 'POST'])
def signup():
        #validate form and create user
        ...
        token = user.generate_confirmation_token()
        send_email(user.email, 'Please Confirm Email Address',
               'email/confirm', user=user, token=token)
        return redirect(url_for('auth.login'))
    return render_template('auth/register.html', form=form)

The new user is sent and email, when the user clicks on the link in the email they are sent to this route:

@auth.route('/confirm/<token>')
@login_required
def confirm(token):
    if current_user.confirmed:
        return redirect(url_for('main.index'))
    if current_user.confirm(token):
        #do something here
    else:
        #message
    return redirect(url_for('main.index'))

As login is required to get to the confirm end point, the user is directed here first:

@auth.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user is not None and user.verify_password(form.password.data):
            login_user(user)
            return redirect(request.args.get('next') or url_for('main.index'))
    return render_template('auth/login.html', form=form)

The issue I have is that after the user logs in they are not redirected to the confirm route and I can't work out how to redirect to confirm for this case where the user is just logging in so that they can respond to the signup email link. Any ideas?


Aucun commentaire:

Enregistrer un commentaire