nativeauthenticator icon indicating copy to clipboard operation
nativeauthenticator copied to clipboard

Log user in automatically after sign-up

Open yuvipanda opened this issue 6 years ago • 2 comments

Right now, when a user signs up, they are then asked to manually go to the login page and log in.

If they don't need explicit admin approval, we should log them in automatically.

yuvipanda avatar Apr 17 '19 19:04 yuvipanda

I figure it out by reconstructing the code on signup, which looks like this:

class SignUpHandler(LocalBase): # Render the sign in page. async def get(self): if not self.authenticator.enable_signup: raise web.HTTPError(404)

    self._register_template_path()
    html = self.render_template(
        'signup.html',
        ask_email=self.authenticator.ask_email_on_signup,
        two_factor_auth=self.authenticator.allow_2fa,
    )
    self.finish(html)

def get_result_message(self, user, taken):
    alert = 'alert-info'
    message = 'Your information has been sent to the admin'

    # Always error if username is taken.
    if taken:
        alert = 'alert-danger'
        message = ("Something went wrong. It appears that this "
                   "username is already in use. Please try again "
                   "with a different username.")
    else:
        # Error if user creation was not successful.
        if not user:
            alert = 'alert-danger'
            pw_len = self.authenticator.minimum_password_length
            if pw_len:
                message = ("Something went wrong. Be sure your "
                           "password has at least {} characters, doesn't "
                           "have spaces or commas and is not too "
                           "common.").format(pw_len)
            else:
                message = ("Something went wrong. Be sure your password "
                           "doesn't have spaces or commas and is not too "
                           "common.")

        # If user creation went through & open-signup is enabled, success.
        elif self.authenticator.open_signup:
            alert = 'alert-success'
            message = ('The signup was successful. You can now go to '
                       'home page and log in the system')

    return alert, message

async def post(self):
    if not self.authenticator.enable_signup:
        raise web.HTTPError(404)

    user_info = {
        'username': self.get_body_argument('username', strip=False),
        'pw': self.get_body_argument('pw', strip=False),
        'email': self.get_body_argument('email', '', strip=False),
        'has_2fa': bool(self.get_body_argument('2fa', '', strip=False)),
    }
    taken = self.authenticator.user_exists(user_info['username'])
    user = self.authenticator.create_user(**user_info)

    alert, message = self.get_result_message(user, taken)        

    otp_secret, user_2fa = '', ''
    if user:
        otp_secret = user.otp_secret
        user_2fa = user.has_2fa
        
    # if alert == 'alert-success', enable user to login automatically,
    # otherwise, let them stay at the signup page and show them the error message.
    
    if alert == 'alert-success':
        username = user_info['username']
        password = user_info ['pw']
        data = {'username': username, 'password': password}
        user = await self.login_user(data)
        if user:
            self._jupyterhub_user = user
            self.redirect(self.get_next_url(user))
    
    # In this case,user can signup again according to the result message
    else:
        html = self.render_template(
            'signup.html',
            ask_email=self.authenticator.ask_email_on_signup,
            result_message=message,
            alert=alert,
            two_factor_auth=self.authenticator.allow_2fa,
            two_factor_auth_user=user_2fa,
            two_factor_auth_value=otp_secret,
        )

        self.finish(html)

I have tested the code and got the expected result that user can login automatically after signup.

xrdy511623 avatar Jul 10 '20 15:07 xrdy511623

Forgive me for I really can not edit format of the code correctly.

xrdy511623 avatar Jul 10 '20 16:07 xrdy511623