def user_login(): """ Login an user """ if 'user' in session: return redirect('/dashboard') form = LoginUserForm(csrf_enabled=False) if form.validate_on_submit(): email = form.email.data password = form.password.data user = User() result = user.find_one({'email': email}) if result is not None: if bcrypt.hashpw(password, result['password']) == result['password']: return auth.login(id=result['_id'], active=result['is_active']) else: flash('Password incorrecto', 'error') else: flash('El usuario no existe', 'error') return render_template('users/login.html', form=form)
def user_register(): """ Register an user """ if 'user' in session: return redirect('/dashboard/') form = RegisterUserForm(csrf_enabled=False) if form.validate_on_submit(): name = form.name.data email = form.email.data password = form.password.data user = User() result = user.find_one({'email': email}) if result is None: data = { 'name': name, 'email': email, 'password': bcrypt.hashpw(password, bcrypt.gensalt()), 'is_active': True } id = user.insert(data) return auth.login(id=id, active=data['is_active']) else: flash('El correo esta registrado, prueba con otro', 'error') return render_template('users/register.html', form=form)
def user_register(): """ Register an user """ if 'user' in session: return redirect('/dashboard/') form = RegisterUserForm(csrf_enabled=False) if form.validate_on_submit(): name = form.name.data email = form.email.data password = form.password.data user = User() result = user.find_one({'email': email}) if result is None: data = { 'name': name, 'email': email, 'password': bcrypt.hashpw(password, bcrypt.gensalt()), 'is_active':True } id = user.insert(data) return auth.login(id=id, active=data['is_active']) else: flash('El correo esta registrado, prueba con otro', 'error') return render_template('users/register.html', form=form)