def register(): form = RegisterForm(request.form) if form.validate_on_submit(): user = User( country_code=form.country_code.data, phone=form.phone.data ) db.session.add(user) db.session.commit() # generate otp and add it to db otp_num = randint(1000, 9999) otp = OTP( otp_num = otp_num, expires_at = (datetime.now() + timedelta(minutes = 3)), user_id = user.id ) db.session.add(otp) db.session.commit() #send sms to = user.country_code + user.phone msg = str(otp_num) print(to) print(msg) nexmo.send_message({'from': '12076138822', 'to': to, 'text': msg}) flash('تم إرسال رمز التحقق لجوال رقم ({})'.format(user.country_code+ user.phone), 'info') print(user.id) return redirect(url_for('verify', user_id=user.id)) #login_user(user) #flash('Thank you for registering.', 'success') #return redirect(url_for('index')) return render_template('register.html', form=form)
def login(): email_form = LoginEmailForm() mobile_form = LoginMobileForm() if email_form.validate_on_submit(): user = User.query.filter_by(email=email_form.email.data).first() if user and bcrypt.check_password_hash(user.password_hash, email_form.password.data): login_user(user) flash('تم تسجيل دخولك بنجاح', 'success') return redirect(url_for('index')) else: flash('خطاء بتسجيل الدخول', 'danger') if mobile_form.validate_on_submit(): user = User.query.filter_by(phone=mobile_form.phone.data).first() if user: # generate otp and add it to db otp_num = randint(1000, 9999) otp = OTP( otp_num = otp_num, expires_at = (datetime.now() + timedelta(minutes = 3)), user_id = user.id ) db.session.add(otp) db.session.commit() #send sms to = user.country_code + user.phone msg = str(otp_num) print(to) print(msg) nexmo.send_message({'from': '12076138822', 'to': to, 'text': msg}) flash('تم إرسال رمز التحقق لجوال رقم ({})'.format(user.country_code+ user.phone), 'info') print(user.id) return redirect(url_for('verify', user_id=user.id)) else: flash('خطاء بتسجيل الدخول', 'danger') return render_template('login.html', email_form=email_form, mobile_form=mobile_form)