У меня проблемы с авторизацией по логину @login_required
. Я создаю маршруты: "/ login", "/ signup", "/ index", "/ profile" и добавляю @login_required
в "/ profile". Я также установил login_manager.login_view = '/index'
. Тогда redirect(url_for("/profile"))
всегда является маршрутом '/ index' независимо от того, войду я в систему или нет. Кажется, login_user(log_user, remember=remember)
не работает. Вот мой __init__.py
from flask_login import login_user, logout_user, current_user
from database.db import mongo
from database.models import User
from werkzeug.security import generate_password_hash, check_password_hash
auth = Blueprint('auth', __name__)
@auth.route('/login')
def login():
return render_template('login.html')
@auth.route('/signup')
def signup():
return render_template('signup.html')
@auth.route('/signup', methods=['POST'])
def signup_post():
email = request.form.get('email')
name = request.form.get('name')
password = generate_password_hash((request.form.get('password')))
user = mongo.db.users.find_one({'email': email})
if user is not None:
flash('Email address already exists')
return redirect(url_for('auth.signup'))
else:
new_user = {'name': name, 'email': email, 'password': password}
mongo.db.users.insert_one(new_user)
return redirect((url_for('auth.login')))
@auth.route('/login', methods=['POST'])
def login_post():
email = request.form.get('email')
password = request.form.get('password')
remember = True if request.form.get('remember') else False
user = mongo.db.users.find_one({'email': email})
if not user or not check_password_hash(user.get('password'), password):
flash('Please check your login details and try again.')
return redirect(url_for('auth.login'))
else:
log_user = User(user.get('name'), email, password, _id=user.get('_id'))
login_user(log_user, remember=remember)
print(current_user.is_authenticated)
print(current_user.is_active)
return redirect(url_for('main.profile'))
@auth.route('/logout')
def logout():
return redirect(url_for('main.index'))
auth.py
from flask import Blueprint, render_template, redirect, request, url_for, flash, abort
from flask_login import login_user, logout_user, current_user
from database.db import mongo
from database.models import User
from werkzeug.security import generate_password_hash, check_password_hash
auth = Blueprint('auth', __name__)
@auth.route('/login')
def login():
return render_template('login.html')
@auth.route('/signup')
def signup():
return render_template('signup.html')
@auth.route('/signup', methods=['POST'])
def signup_post():
email = request.form.get('email')
name = request.form.get('name')
password = generate_password_hash((request.form.get('password')))
user = mongo.db.users.find_one({'email': email})
if user is not None:
flash('Email address already exists')
return redirect(url_for('auth.signup'))
else:
new_user = {'name': name, 'email': email, 'password': password}
mongo.db.users.insert_one(new_user)
return redirect((url_for('auth.login')))
@auth.route('/login', methods=['POST'])
def login_post():
email = request.form.get('email')
password = request.form.get('password')
remember = True if request.form.get('remember') else False
user = mongo.db.users.find_one({'email': email})
if not user or not check_password_hash(user.get('password'), password):
flash('Please check your login details and try again.')
return redirect(url_for('auth.login'))
else:
log_user = User(user.get('name'), email, password, _id=user.get('_id'))
login_user(log_user, remember=remember)
print(current_user.is_authenticated)
print(current_user.is_active)
return redirect(url_for('main.profile'))
@auth.route('/logout')
def logout():
return redirect(url_for('main.index'))
main.py
from flask import Blueprint, render_template
from flask_login import login_required, current_user
main = Blueprint('main', __name__)
@main.route('/')
def index():
return render_template('index.html')
@main.route('/profile')
@login_required
def profile():
return render_template('profile.html')
db.py
from flask_pymongo import PyMongo
mongo = PyMongo()
И models.py
from flask_login import UserMixin
from database.db import mongo
import uuid
class User(UserMixin):
def __init__(self, name, email, password, _id=None):
self.name = name
self.email = email
self.password = password
self._id = uuid.uuid4().hex if _id is None else _id
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return str(self._id)
@classmethod
def get_by_id(cls, _id):
data = mongo.db.users.find_one({"_id": _id})
if data is not None:
return cls(**data)
Спасибо за все!