Advertisement
ethan_cai1786

app.py

Jan 24th, 2022
838
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from flask import Flask, render_template, request, redirect, url_for, flash, make_response
  2.  
  3. import json
  4.  
  5. app = Flask(__name__)
  6.  
  7. class Account:
  8.     def __init__(self, email, username, password):
  9.         self.email = email
  10.         self.username = username
  11.         self.password =password
  12.  
  13. @app.route('/')
  14. def index():
  15.     url_for('logout')
  16.     return render_template('index.html')
  17. @app.route('/')
  18.  
  19. def getPasswordFromCookies():
  20.     print (request.cookies.get('password'))
  21.     return request.cookies.get('password')
  22. @app.route('/')
  23. def getPasswordFromDatabase():
  24.     acc = open("accounts/" + str(request.cookies.get('username')) + ".json", "r+")
  25.     print (json.loads(acc.read())['password'] )
  26.     return json.loads(acc.read())['password']
  27. @app.route('/')
  28. def getUsernameFromDatabase():
  29.     acc = open("accounts/" + str(request.cookies.get('username')) +".json", "r+")
  30.     print( json.loads(acc.read())['username'])
  31.     return json.loads(acc.read())['username']
  32. @app.route('/signup', methods = ['GET', 'POST'])
  33. def signup():
  34.     print("deez1")
  35.     if request.method == "POST":
  36.         print("Deez2")
  37.         email = request.form.get("email")
  38.         username = request.form.get("username")
  39.         password = request.form.get("pwd")
  40.         try:
  41.             open("accounts/" + str(username) + ".json", "r+")
  42.             #go to page that says you already have an account
  43.             #return redirect(url_for('index'))
  44.         except:
  45.             pass
  46.         accountinfo = open("accounts/" + str(username) + ".json", "w")
  47.        
  48.         accountinfo.write(json.dumps(Account(email, username, password).__dict__))
  49.         #return accountinfo.read()
  50.         return url_for('index')
  51.     return render_template('signup.html')
  52. @app.route('/login', methods = ['GET', 'POST'])
  53. def login():
  54.    
  55.     if request.method == "POST":
  56.         username = request.form.get('username')
  57.         password = request.form.get("pwd")
  58.         try:
  59.             account = open("accounts/" + str(username) + ".json", "r+")
  60.             if username == json.loads(account)['username'] and password == json.loads(account)['password']:
  61.                 resp = make_response(render_template(url_for('index')))
  62.                 resp.set_cookie('username', username)
  63.                 resp.set_cookie('password', password)
  64.                 return resp
  65.             else:
  66.                 print("nay")
  67.            
  68.         except:
  69.             return url_for('signup')
  70.         finally:
  71.             return redirect(url_for('index'))
  72.        
  73.     return render_template('login.html')
  74. @app.route('/')
  75. def logout():
  76.     resp = make_response(render_template(url_for('index')))
  77.     resp.set_cookie('username', '', expires=0)
  78.     resp.set_cookie('password', '', expires=0)
  79.     return resp
  80.  
  81.  
  82. @app.route('/')
  83. @app.context_processor
  84. def thingys():
  85.     def loginthingy():
  86.         if url_for('getPasswordFromCookies') == url_for('getPasswordFromDatabase'):
  87.             return "<button id = \"loginbutton\" >Logged in as {{url_for(\'getUsernameFromDatabase\')}}</button>"
  88.         else:
  89.             print("Access Denied")
  90.             return " <a href=\"{{url_for('login')}}\"><button id = \"loginbutton\" >Log In</button></a>"
  91.     def signupthingy():
  92.         if url_for('getPasswordFromCookies') == url_for('getPasswordFromDatabase'):
  93.             return "<a href=\"{{url_for('logout')}}\"><button id = \"signupbutton\" >Log Out</button></a>"
  94.         else:
  95.             print("Access Denied")
  96.             return "<a href=\"{{url_for('login')}}\"><button id = \"loginbutton\" >Log In</button></a>"
  97.     return dict(loginthingy=loginthingy(), signupthingy=signupthingy())
  98. #@app.route('/', methods = ['POST'])
  99.  
  100. #def loginaccount():
  101.    
  102.  
  103. # @app.route('/', methods = ['POST'])  
  104. # def makeaccount():
  105. #     print("Acount.Make")
  106. #     if request.method == "POST":
  107. #         email = request.form.get("email")
  108. #         username = request.form.get("username")
  109. #         password = request.form.get("pwd")
  110. #         try:
  111. #             open("accounts/" + str(email) + ".json", "r+")
  112. #             #go to page that says you already have an account
  113. #             #return redirect(url_for('index'))
  114. #         except:
  115. #             pass
  116. #         accountinfo = open("accounts/" + str(email) + ".json", "w")
  117.        
  118. #         accountinfo.write(json.dumps(Account(email, username, password).__dict__))
  119. #         #return accountinfo.read()
  120.    
  121. #     return "Deez"#redirect(url_for('index'))
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement