Automatically Assigning Role Profiles During User Sign-Up in Frappe 15

I’m currently working on a Learning Management System (LMS) using Frappe and I’m facing an issue with user sign-ups. I have two types of users in my system: ‘Teacher’ and ‘Student’. During the sign-up process, I want users to be able to select their user type, and based on this selection, I want to automatically assign them a Role Profile (‘Teacher’ or ‘Student’).

I have already created the Role Profiles and added the ‘User Type’ field to the sign-up form. I’ve also written a server-side script that is supposed to assign the Role Profile to the user when they sign up. However, the Role Profiles are not being assigned automatically as expected.

Here’s my on /home/azureuser/frappe-bench/apps/lms/lms/overrides:

def sign_up(email, full_name, verify_terms, user_type):
    if is_signup_disabled():
        frappe.throw(_("Sign Up is disabled"), _("Not Allowed"))

    if not user_type:
        return 0, _("User type is required")

    user = frappe.db.get("User", {"email": email})
    if user:
        if user.enabled:
            return 0, _("Already Registered")
            return 0, _("Registered but disabled")
        if frappe.db.get_creation_count("User", 60) > 300:
                _("Temporarily Disabled"),
                    "Too many users signed up recently, so the registration is disabled. Please try back in an hour"

    user = frappe.get_doc(
            "doctype": "User",
            "email": email,
            "first_name": escape_html(full_name),
            "verify_terms": verify_terms,
            "user_category": user_type,  # use the user_type from the form
            "country": "",
            "enabled": user_type != 'Teacher',  # disable the user account for Teacher
            "new_password": random_string(10),
            "user_type": "Website User",
            "role_profile_name": user_type,  # assign Role Profile based on user type
    user.flags.ignore_permissions = True
    user.flags.ignore_password_policy = True


    if user.flags.email_sent:
        return 1, _("Please check your email for verification")
        return 2, _("Please ask your administrator to verify your sign-up")

and here’s my signup.html on /home/azureuser/frappe-bench/apps/frappe/frappe/templates :

<form class="form-signin form-signup hide" role="form">
    <div class="page-card-body">
        <div class="form-group">
            <label class="form-label sr-only" for="signup_fullname">{{ _("Full Name") }}</label>
            <input type="text" id="signup_fullname" class="form-control" placeholder="{{ _('Jane Doe') }}"
                required autofocus autocomplete="name">
        <div class="form-group">
            <label class="form-label sr-only" for="signup_email">{{ _("Email") }}</label>
            <input type="email" id="signup_email" class="form-control"
                placeholder="{{ _('') }}" required autocomplete="username">

        <!-- New User Type Field -->
        <div class="form-group">
            <label class="form-label sr-only" for="signup_usertype">{{ _("User Type") }}</label>
            <select id="signup_usertype" class="form-control" required>
                <option value="">{{ _("Select User Type") }}</option>
                <option value="Student">{{ _("Student") }}</option>
                <option value="Teacher">{{ _("Teacher") }}</option>
    <div class="page-card-actions">
        <button class="btn btn-sm btn-primary btn-block btn-signup"
            type="submit">{{ _("Sign up") }}</button>

        <p class="text-center sign-up-message">
            <a href="#login" class="blue">{{ _("Have an account? Login") }}</a>