Created
April 10, 2024 22:23
-
-
Save dclobato/285e9f768801dc00d88525eb544605c2 to your computer and use it in GitHub Desktop.
Codigos aula 2024-04-10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from urllib.parse import urlsplit | |
from flask import Blueprint, redirect, url_for, render_template, flash, request | |
from flask_login import current_user, login_user, login_required, logout_user | |
from src.models.usuario import User | |
from src.modules import db | |
from src.forms.auth import LoginForm | |
bp = Blueprint('auth', __name__, url_prefix='/admin/user') | |
@bp.route('/login', methods=['GET', 'POST']) | |
def login(): | |
if current_user.is_authenticated: | |
return redirect(url_for('index')) | |
form = LoginForm() | |
if form.validate_on_submit(): | |
usuario = User.get_by_email(form.email.data) | |
if usuario is None or not usuario.check_password(form.password.data): | |
flash("Email ou senha incorretos", category='warning') | |
return redirect(url_for('auth.login')) | |
if not usuario.ativo: | |
flash("Usuário está impedido de acessar o sistema. Procure um adminstrador", | |
category='danger') | |
return redirect(url_for('auth.login')) | |
login_user(usuario, remember=form.remember_me.data) | |
db.session.commit() | |
flash(f"Usuario {usuario.email} logado", category='success') | |
next_page = request.args.get('next') | |
if not next_page or urlsplit(next_page).netloc != '': | |
next_page = url_for('index') | |
return redirect(next_page) | |
return render_template('auth/login.jinja2', | |
title="Dados de acesso", | |
form=form) | |
@bp.route('/logout') | |
@login_required | |
def logout(): | |
logout_user() | |
flash("Sessão encerrada", category='success') | |
return redirect(url_for('index')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends '_layout.jinja2' %} | |
{% from 'bootstrap5/form.html' import render_field %} | |
{% block content %} | |
<section class="h-100"> | |
<div class="container h-100"> | |
<div class="row justify-content-sm-center h-100"> | |
<div class="col-xxl-4 col-xl-5 col-lg-5 col-md-7 col-sm-9"> | |
<div class="card shadow-lg"> | |
<div class="card-body p-5"> | |
<h1 class="fs-4 card-title fw-bold mb-4">Login</h1> | |
<form action="" method="post" class="form" role="form" novalidate autocomplete="off"> | |
{{ form.csrf_token() }} | |
<div class="mb-3"> | |
{{ render_field(form.email) }} | |
</div> | |
<div class="mb-3"> | |
{{ render_field(form.password) }} | |
</div> | |
<div class="d-flex align-items-center"> | |
{{ render_field(form.remember_me) }} | |
{{ render_field(form.submit, class="btn btn-primary ms-auto") }} | |
</div> | |
</form> | |
</div> | |
<div class="card-footer py-3 border-0"> | |
<div class="text-center"> | |
Não tem uma conta? <a href="#" class="text-dark">Crie uma!</a> | |
</div> | |
<div class="text-center"> | |
<a href="#" class="text-dark">Esqueceu a senha?</a> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</section> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment