Adding More than one User in flask_login in Python flask.
Tags: python, html, css, webdev
Published: 2022-04-21 20:27:52

For identifying user we take one extra input at the time of submission of html form.Following is step to login two user: -

- get a label of user 1 at the time of submit html form.

```
<form action="/login" method="post">
            {% include "messages.html" %}
            <div class="form-group">
                <input type="email" class="form-control mt-2" name="ulmail" placeholder="Enter E-mail" required>
            </div>

            <div class="form-group">
                <input type="password" class="form-control mt-2" name="ulpass" placeholder="Enter password" required>
            </div>

            <div class="form-group">
                <input type="text" class="form-control mt-2" name="ulabel" value="User" readonly>
            </div>

            <div class="text-center">
                <br>
                <button type="submit" class="btn btn-primary btn-rounded">Log In</button>
                
            </div>

            
        </form>
```
by the above form we are taking input from user 1 in last division which name = "label" and value = "User". and it is readonly.


- then take input from second user

```
<form action="/doctorlogin" method="post">
            {% include "messages.html" %}
            <div class="form-group">
                <input type="email" class="form-control mt-2" name="dlmail" placeholder="Enter E-mail" required>
            </div>

            <div class="form-group">
                <input type="text" class="form-control mt-2" name="dluprn" placeholder="Enter UPRN" required>
            </div>

            <div class="form-group">
                <input type="password" class="form-control mt-2" name="dlpass" placeholder="Enter password" required>
            </div>

            <div class="form-group">
                <input type="text" class="form-control mt-2" name="dlabel" value="Doctor" readonly>
            </div>

            <div class="text-center">
                <br>
                <button type="submit" class="btn btn-primary btn-rounded">Log In</button>
                
            </div>

            
        </form>
```
here name="dlabel" and value="Doctor".

- Now in your python file defile a global varaible. **Note: I am assuming that you have created two different Table in database for two users and imported successfully in your python file. **
here user 1= User and
     user 2= Doctor.

```
dl  = "nan"
@login_manager.user_loader
def load_user(user_id):
    if dl == "Doctor":
        return Doctor.query.get(int(user_id))
    elif dl == "User":
        return User.query.get(int(user_id))
```
 here I am using sqlalchemy module to connect mysql databse(XAMPP).

```
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://root:@127.0.0.2:3307/medserv"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app) 

```
"Doctor", and " User" are two different table in "medserv" Database. 

- Now for login user 1:

```
@app.route("/login", methods=['POST','GET'])
def login():
    if request.method == "POST":
        global dl
        email = request.form.get('ulmail') 
        upass = request.form.get('ulpass')
        dl  = request.form.get('ulabel')
        user = User.query.filter_by(umail = email).first_or_404(description='There is no data with {}'.format(email))

        if user and user.upass == upass:

            login_user(user)
            postsdata = Userdata.query.filter_by(email = email).all()
            flash("Login Successful", "success")
            return render_template("cusers.html", postsdata=postsdata)
        else:
            flash("Invalid Credential", "danger")
            return render_template("userlogin.html")
        
    return render_template("userlogin.html")
```
define dl as global in this class and store value of label you collected from form into "dl" variable and start login session. You will be login as user 1 because your dl=="User" so your login manager will be redirected to table 2 or we can say login for User table.

- Now for login user 2

```
@app.route("/doctorlogin", methods=['POST','GET'])
def doctorlogin():
    if request.method == "POST":
        global dl
        email = request.form.get('dlmail') 
        upass = request.form.get('dlpass')
        uprn = request.form.get('dluprn')
        dl = request.form.get('dlabel')
        user = Doctor.query.filter_by(dmail = email).first_or_404(description='There is no data with {}'.format(email))

        if user and user.dpass == upass and user.duprn==uprn:
            
            login_user(user)

            postsdata = Doctordata.query.filter_by(udcon = email).all()
            flash("Login Successful", "success")
            return render_template("docdash.html",postsdata=postsdata)
        else:
            flash("Invalid Credential", "danger")
            return render_template("doctorlogin.html")
        
    return render_template("doctorlogin.html")
```
Now dl=="Doctor" you will be login as user 2 so you will retrieve information from table corresponded to Doctor.