-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
65 lines (52 loc) · 2.22 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Blueprint, render_template, redirect, url_for, request, flash, abort, Response, jsonify
from flask_expects_json import expects_json
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import login_user, logout_user, login_required, current_user
from auth_schemas import signup_schema, login_schema
from helpers import return_as_json
from orm import User
from app import db, app
import logging
logging.basicConfig(filename='error.log', level=logging.DEBUG,
format=f'%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s')
auth = Blueprint('auth', __name__)
@auth.route('/login', methods=['POST'])
@expects_json(login_schema)
def login_post():
email = request.json['email']
password = request.json['password']
user = User.query.filter_by(email=email).first()
if not user or not check_password_hash(user.password, password):
return abort(401, Response("Nope."))
login_user(user, remember=True)
return return_as_json(user.to_dict())
@auth.route('/signup', methods=['POST'])
@expects_json(signup_schema)
def signup_post():
email = request.json['email']
name = request.json['name']
password = request.json['password']
try:
user = User.query.filter_by(
email=email).first() # if this returns a user, then the email already exists in database
except Exception as e:
app.logger.error(e)
return abort(500, Response("Your request generated an internal error. Please contact the Admin and check "
"auth.log"))
if user:
return abort(401, Response("Please sign in, user exists"))
try:
new_user = User(email=email, name=name, password=generate_password_hash(password, method='sha256'))
db.session.add(new_user)
db.session.commit()
db.session.flush()
except Exception as e:
app.logger.error(e)
return abort(500, Response("Your request generated an internal error. Please contact the Admin and check "
"auth.log"))
return return_as_json(new_user.to_dict())
@auth.route('/logout')
@login_required
def logout():
logout_user()
return return_as_json({})