def login(): """The screen to log the user into the system.""" # Redirect the user if already logged in if current_user.is_authenticated: # Send admins and non-admins to different pages if current_user.admin: return redirect(url_for(default_admin_page)) else: return redirect(url_for(default_user_page)) form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data).first() if user is None or not user.check_password(form.password.data): flash("Invalid username or password") return redirect(url_for('login.login')) login_user(user) current_app.logger.info(f"Logged in {user}") # If the user was redirected here, send the user back to the original page next_page = request.args.get('next') if not next_page or url_parse(next_page).netloc != '': # If no next page given, default to these pages if user.admin: next_page = url_for(default_admin_page) else: next_page = url_for(default_user_page) return redirect(next_page) nav_bar_title = "Login" return render_template('login/login.html', title='Sign in', form=form, nav_bar_title=nav_bar_title)
def signin(): form = LoginForm() if current_user.is_authenticated: return redirect(url_for('home.index')) if form.validate_on_submit(): # Check if such a user exist user = User.query.filter_by(username=form.username.data).first() # If the username doesn't exist or the password is wrong. checkPassword is a function defined in my User models class. if user is None or not user.checkPassword(form.password.data): flash('Invalid username or password!', 'error') return redirect(url_for('login.signin')) # If all is good, log the user in. flash("Logged in! Welcome!") # Logs in the user. user in this case refers to the user object created by the SQL query. Remember me is handled via cookies. login_user(user, remember=form.rememberMe.data) # In the URL exists a get parameter that has the route to be redirected to. # For example, if the user were to access /upload without logging in; next=upload would be in the URL next_page = request.args.get('next') # If there is no 'next' get parameter or if its network location part is somehow empty, redirect to index # <scheme>://<netloc>/<path>;<params>?<query>#<fragment> is the format for a URL # 192.168.1.100/home The IP address is your netloc. This is done to determine if the URL is relative or absolute, for security. if not next_page or url_parse(next_page).netloc != '': next_page = url_for('index') return redirect(next_page) return render_template('login.html', form=form)
def login(): loginForm = LoginForm() if loginForm.validate_on_submit(): print(loginForm.username.data) print(loginForm.password.data) if loginForm.remember_me.data == 1: print('ok') else: print('no') else: return render_template('404.html') return render_template('login/login.html', loginForm=loginForm)
def recuperarAlterar(): forms = LoginForm() if forms.validate_on_submit(): email = User.query.filter_by(email=forms.email.data).first() if email is not None: email.senha = generate_password_hash(forms.senha.data) db.session.commit() # print(email.senha, email, email.email, forms.senha.data) flash("Senha alterada com sucesso!!!", "success") return redirect(url_for('form.index')) else: flash("Erro ao alterar a senha", "warning") return redirect(url_for("form.index")) return render_template('recuperarAlterar.html', form=forms)
def index(): forms = LoginForm() if forms.validate_on_submit(): print(forms.email.data, forms.senha.data) user = User.query.filter_by(email=forms.email.data).first() if user is None or not user.verifica_senha(forms.senha.data): flash("Verifique email ou senha!!!", 'danger') return redirect(url_for('.index')) print(user.nome) login_user(user, remember=True) return redirect(url_for('form.listar')) else: print(forms.errors) return render_template('login.html', form=forms)
def login(): if current_user.is_authenticated: return redirect(url_for('home')) form = LoginForm() #print(form.validate_on_submit()) #print(form.errors) if (form.validate_on_submit()): user = User.query.filter_by(Usuario=form.Usuario.data).first() if user is None or not user.check_password(form.Clave.data): flash('Usuario o Clave invalida') #return redirect(url_for('login')) return render_template('/Login-Register-Page/login.html', form=form) login_user(user, remember=form.recordar.data) return redirect(url_for('home')) return render_template('/Login-Register-Page/login.html', form=form)
def login(): """Login""" if current_user.is_authenticated: return redirect(url_for('main.index')) form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user is None or not user.check_password(form.password.data): flash('Invalid username or password') return redirect(url_for('login.login')) login_user(user, remember=form.remember_me.data) next_page = request.args.get('next') if not next_page or url_parse(next_page).netloc != '': next_page = url_for('main.index') return redirect(next_page) return render_template('login/loginpage.html', title='Sign in', form=form)
def login(): logger = logging.getLogger("LOGIN") # При входе на сайт, пользователь сразу переходит к странице # авторизации. # Он вводит свой логин и пароль, а так как пользователь у нас один, # то проверяем введенные данные с данными, которые у нас есть. # Если были введены верные данные, то переводим его на основную # страницу. # Иначе просим его ввести данные снова. user = User("admin", "admin") # Проверяем, если пользователь уже зашел, # то отправляем его на основную страницу. if current_user.is_authenticated: logger.info("User already autheticated") return redirect(url_for("index.index")) # Создаем объект form, который содержит в себе веб-формы для # авторизации form = LoginForm() # Если пришел POST запрос от браузера if form.validate_on_submit(): logger.debug("L0gin page submitted") if user.username != form.username.data or user.check_password( form.password.data) is False: logger.info("Invalid username or password") flash('Invalid username or password') return redirect(url_for('login')) # Иначе загружаем пользователя # и переходим к основной странице login_user(user, remember=form.remember.data) logger.info("User" + user.username + " signed in") # Проверяем, если в строке был указан аргумент next, # значит пользователь пытался перейти на страницу для # авторизованных пользователей, но так как он не авторизовалься, # его перенаправили сюда. Тогда после того, как он # авторизовался, переходим на стрницу указанную страницу. next_page = request.args.get("next") # Если аргумента next нет, то переходим на главную страницу if next_page is None or url_parse(next_page).netloc != '': next_page = url_for("index.index") return redirect(next_page) # Отображаем страницу авторизиции logger.debug("Rendering login page") return render_template("login.html", title="Login", form=form)
def login(): """The screen to log the user into the system.""" if current_user.is_authenticated: return redirect(url_for('default.home')) form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data).first() if user is None or not user.check_password(form.password.data): flash("Invalid username or password", category="top") return redirect(url_for('login.login')) login_user(user) next_page = request.args.get('next') if not next_page or url_parse(next_page).netloc != '': next_page = url_for('default.home') return redirect(next_page) nav_bar_title = "Login" return render_template('login.html', title='Sign in', form=form, nav_bar_title=nav_bar_title)
def log_user_in(): form = LoginForm(request.form) if form.validate_on_submit(): user = User.query.filter(User.username == form.username.data).first() if user is not None and user.check_password(form.password.data): login_user(user) return redirect(url_for('index')) else: if user is None: flash('User\'s name not found', 'login') else: flash('Incorrect Password', 'login') return render_template('login.html', form=form) else: return render_template('login.html', form=form)
def login(): if current_user.is_authenticated: return redirect(url_for('index.index')) form = LoginForm() if request.method == 'POST' and form.validate_on_submit(): next_page = request.args.get('next') user = User(form.username.data) if user.username and user.check_password(form.password.data): login_user(user) if not next_page or url_parse(next_page).netloc != '': next_page = url_for('index.home') else: flash('Incorrect username/password.') next_page = request.referrer return redirect(next_page) return render_template('login.html', form=form)