46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
from flask import Flask, render_template, redirect, url_for, request, flash
|
|
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
|
|
from app import app, db, login_manager
|
|
from models import User
|
|
|
|
|
|
@login_manager.user_loader
|
|
def load_user(user_id):
|
|
return User.query.get(int(user_id))
|
|
|
|
|
|
@app.route('/login', methods=['GET', 'POST'])
|
|
def login():
|
|
if request.method == 'POST':
|
|
username = request.form.get('username')
|
|
password = request.form.get('password')
|
|
user = User.query.filter_by(username=username).first()
|
|
if user and user.password == password:
|
|
login_user(user)
|
|
return redirect(url_for('dashboard'))
|
|
return render_template('login.html')
|
|
|
|
|
|
@app.route('/register', methods=['GET', 'POST'])
|
|
def register():
|
|
if request.method == 'POST':
|
|
username = request.form.get('username')
|
|
password = request.form.get('password')
|
|
email = request.form.get('email')
|
|
full_name = request.form.get('full_name')
|
|
phone_number = request.form.get('phone_number')
|
|
|
|
new_user = User(username=username, password=password, email=email, full_name=full_name,
|
|
phone_number=phone_number)
|
|
db.session.add(new_user)
|
|
db.session.commit()
|
|
return redirect(url_for('login'))
|
|
return render_template('register.html')
|
|
|
|
|
|
@app.route('/logout')
|
|
@login_required
|
|
def logout():
|
|
logout_user()
|
|
return redirect(url_for('login'))
|