Back

Python - Flask Login does not work properly

  • 0
  • Self Hosted
  • Accounts
  • Users
  • Cloud
Gaddy
16 Jun, 2023, 20:28

Hello again, today I migrated to Appwrite and I ran into some issues. My Loginpanel wont redirect properly. The Code and Logs are provided in the next Message.

TL;DR
The Flask Login is not working properly. The user is not being redirected after logging in. The provided code and logs show that there is no `create_session()` function used in the login route. The solution is to replace `account.create_session()` with the appropriate function from the Appwrite library.
Gaddy
16 Jun, 2023, 20:28
TypeScript
from appwrite.client import Client
from appwrite.services.account import Account
from dotenv import load_dotenv
import os

app = Flask(__name__)
load_dotenv()

# Initialize the Appwrite client
client = Client()
client.set_endpoint(os.getenv('APPWRITE_ENDPOINT'))
client.set_project(os.getenv('APPWRITE_PROJECT_ID'))
client.set_key(os.getenv('APPWRITE_API_KEY_DevelopmentScopes'))

# Generate a random secret key for session management
secret_key = os.urandom(32)
app.secret_key = secret_key

@app.route('/')
def index():
    return render_template("index.html")

@app.route('/app')
def home():
    if 'user' in session:
        return render_template("start.html")
    else:
        return redirect(url_for('login'))

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        email = request.form['email']
        password = request.form['password']

        account = Account(client)
        try:
            response = account.create_session(email=email, password=password)
            session['user'] = response['userId']
            return redirect(url_for('app'))
        except Exception as e:
            # Handle login failure here
            return render_template('login.html', error='Invalid credentials')

    elif 'user' in session:
        return redirect(url_for('app'))
    else:
        return render_template('login.html')

@app.route('/logout')
def logout():
    if 'user' in session:
        account = Account(client)
        account.delete_session(session['user'])
        session.pop('user', None)

    return redirect(url_for('login'))

if __name__ == '__main__':
    app.run(debug=True, port=8080)
Gaddy
16 Jun, 2023, 20:28
TypeScript
127.0.0.1 - - [16/Jun/2023 22:22:05] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [16/Jun/2023 22:22:07] "GET /login HTTP/1.1" 200 -
127.0.0.1 - - [16/Jun/2023 22:22:08] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [16/Jun/2023 22:22:09] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [16/Jun/2023 22:22:11] "POST /login HTTP/1.1" 200 -
127.0.0.1 - - [16/Jun/2023 22:22:12] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [16/Jun/2023 22:22:12] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [16/Jun/2023 22:22:18] "POST /login HTTP/1.1" 200 -
127.0.0.1 - - [16/Jun/2023 22:22:18] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [16/Jun/2023 22:22:19] "GET /favicon.ico HTTP/1.1" 404 -```
Drake
16 Jun, 2023, 21:46

There is no create_session() function. If you checked the exception, it probably would have told you that

Gaddy
17 Jun, 2023, 09:27

There was no exeption, but thx for the advice. Will fix the Code and post again if it woked.

Gaddy
17 Jun, 2023, 11:22

nvm... forgot to print out the exeption 😅

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more