Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. ### SQL ###
  2.  
  3. mysql> DESCRIBE users;
  4. +----------+----------------+------+-----+---------+----------------+
  5. | Field | Type | Null | Key | Default | Extra |
  6. +----------+----------------+------+-----+---------+----------------+
  7. | uid | int(11) | NO | PRI | NULL | auto_increment |
  8. | username | varchar(20) | YES | | NULL | |
  9. | password | varchar(100) | YES | | NULL | |
  10. | email | varchar(50) | YES | | NULL | |
  11. | settings | varchar(32500) | YES | | NULL | |
  12. | tracking | varchar(32500) | YES | | NULL | |
  13. | rank | int(3) | YES | | NULL | |
  14. | code | varchar(60) | YES | | NULL | |
  15. +----------+----------------+------+-----+---------+----------------+
  16. 8 rows in set (0.00 sec)
  17.  
  18.  
  19. ### EMAIL ###
  20.  
  21. msg = Message("Your authentication code.",
  22. sender="sender@sender.com",
  23. recipients=["recipient@recipient.com"])
  24. msg.body = "Your authentication code is: {}".format(key)
  25. mail.send(msg)
  26.  
  27.  
  28. ### REGSITER AND LOGIN FROM __INIT__.PY ###
  29. @app.route('/login/', methods=["GET","POST"])
  30. def login_page():
  31. error = ''
  32. try:
  33. c, conn = connection()
  34. if request.method == "POST":
  35. data = c.execute("SELECT * FROM users WHERE username = '%s'" % thwart(request.form['username']))
  36. data = c.fetchone()[2]
  37.  
  38. if sha256_crypt.verify(request.form['password'], data):
  39. session['logged_in'] = True
  40. session['username'] = request.form['username']
  41.  
  42. flash("You are now logged in")
  43. return redirect(url_for("dashboard"))
  44.  
  45. else:
  46. error = "Invalid credentials, try again."
  47.  
  48. gc.collect()
  49.  
  50. return render_template("login.html", error=error)
  51.  
  52. except Exception as e:
  53. error = "Invalid credentials, try again."
  54. return render_template("login.html", error = error)
  55.  
  56. class RegistrationForm(Form):
  57. username = TextField('Username', [validators.Length(min=4, max=20)])
  58. email = TextField('Email Address', [validators.Length(min=6, max=50)])
  59. password = PasswordField('Password', [validators.Required(),
  60. validators.EqualTo('confirm', message="Passwords must match.")])
  61. confirm = PasswordField('Repeat Password')
  62.  
  63. accept_tos = BooleanField('I accept the <a href="/tos">Terms of Service</a> and the <a href="/privacy">Privacy Notice</a> (Last updated 23/02/2018)', [validators.Required()])
  64.  
  65. @app.route('/register/', methods=["GET","POST"])
  66. def register_page():
  67. try:
  68. form = RegistrationForm(request.form)
  69.  
  70. if request.method == "POST" and form.validate():
  71. username = form.username.data
  72. email = form.email.data
  73. password = sha256_crypt.encrypt((str(form.password.data)))
  74. c, conn = connection()
  75.  
  76. x = c.execute("SELECT * FROM users WHERE username = (%s)",
  77. [thwart(username,)])
  78.  
  79. if int(x) > 0:
  80. flash("That username is already taken, please choose another")
  81. return render_template('register.html', form=form)
  82.  
  83. else:
  84. c.execute("INSERT INTO users (username, password, email, tracking) VALUES (%s, %s, %s, %s)",
  85. (thwart(username), thwart(password), thwart(email), thwart("/introduction-to-python-programming/")))
  86.  
  87. conn.commit()
  88. flash("Thanks for registering!")
  89. c.close()
  90. conn.close()
  91. gc.collect()
  92.  
  93. session['logged_in'] = True
  94. session['username'] = username
  95.  
  96. return redirect(url_for('dashboard'))
  97.  
  98. return render_template("register.html", form=form)
  99.  
  100. except Exception as e:
  101. return(str(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement