diff --git a/accounts.py b/accounts.py index b4f43d6..f2e2915 100644 --- a/accounts.py +++ b/accounts.py @@ -74,4 +74,21 @@ def logout(token): db.update_tokens(user['id'], user['tokens']) - return {'success': True, 'message': 'Logged out'} \ No newline at end of file + return {'success': True, 'message': 'Logged out'} + +def login(email,password): + # Verify email + search = db.search_users(email) + if search == []: + return {'success': False, 'message': 'Invalid email'} + user = convert_db_users(search[0]) + # Verify password + if not verify_password(password, user['password']): + return {'success': False, 'message': 'Invalid password'} + + # Create a cookie + token = generate_cookie() + user['tokens'].append(token) + # Update user + db.update_tokens(user['id'], user['tokens']) + return {'success': True, 'message': 'Logged in', 'token': token} \ No newline at end of file diff --git a/main.py b/main.py index 95e8071..eaf9743 100644 --- a/main.py +++ b/main.py @@ -48,7 +48,6 @@ def signup(): email = request.form['email'] domain = request.form['domain'] password = request.form['password'] - print("New signup for " + email + " | " + domain) try: valid = validate_email(email) email = valid.email @@ -63,6 +62,20 @@ def signup(): except EmailNotValidError as e: return jsonify({'success': False, 'message': 'Invalid email'}), 400 + +@app.route('/login', methods=['POST']) +def login(): + email=request.form['email'] + password=request.form['password'] + user = accounts.login(email,password) + if not user['success']: + return error(user['message']) + # Redirect to dashboard with cookie + resp = make_response(redirect('/edit')) + resp.set_cookie('token', user['token']) + return resp + + @app.route('/logout') def logout(): @@ -77,13 +90,29 @@ def logout(): @app.route('/') def catch_all(path): + account = "Login" + account_link = "login" + site = "Null" + if 'token' in request.cookies: + token = request.cookies['token'] + # Verify token + user = accounts.validate_token(token) + if not user: + # Remove cookie + resp = make_response(redirect('/')) + resp.set_cookie('token', '', expires=0) + return resp + account = user['email'] + account_link = "account" + site = user['domain'] + ".exampledomainnathan1" + # If file exists, load it if os.path.isfile('templates/' + path): - return render_template(path) + return render_template(path,account=account,account_link=account_link,site=site) # Try with .html if os.path.isfile('templates/' + path + '.html'): - return render_template(path + '.html') + return render_template(path + '.html',account=account,account_link=account_link,site=site) return redirect('/') # 404 catch all # 404 catch all diff --git a/templates/account.html b/templates/account.html new file mode 100644 index 0000000..1d7d623 --- /dev/null +++ b/templates/account.html @@ -0,0 +1,34 @@ + + + + + + + shakecities + + + + + + +
+

Email: {{account}}
Site: {{site}}

Logout +
+ + + + \ No newline at end of file diff --git a/templates/edit.html b/templates/edit.html new file mode 100644 index 0000000..3c42d8f --- /dev/null +++ b/templates/edit.html @@ -0,0 +1,31 @@ + + + + + + + shakecities + + + + + + + + + + \ No newline at end of file diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..3e4b58c --- /dev/null +++ b/templates/login.html @@ -0,0 +1,38 @@ + + + + + + + shakecities + + + + + + +
+
+
+

Sign in to manage your page

+
+
+
+
+ + + + \ No newline at end of file