aboutsummaryrefslogtreecommitdiff
path: root/api/auth/signup.py
blob: d82105a7e674b8359b658c95db3376849e3ad955 (plain)
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
from flask import Blueprint, request
from main import cursor, connection
from randid import new_uuid
import auth.token as token
import passwords
import time
import json

signup = Blueprint('signup', __name__)

@signup.route('/signup', methods = ['POST'])
def index():
    data = request.get_json()

    username = data.get("username") or ""
    email = data.get("email") or ""
    password = data.get("password") or ""

    if not username or \
       not email or \
       not password:
           return "", 400

    if cursor.execute("select username from users where username = ?", [username]).fetchone():
        return {"error": "username_taken"}, 403

    if cursor.execute("select email from users where email = ?", [email]).fetchone():
        return {"error": "email_taken"}, 403

    user_id = new_uuid()
    password_salt = passwords.salt()
    password_hash = passwords.password_hash(password, password_salt)
    registered = int( time.time() * 1000 )

    cursor.execute("insert into users values (?, ?, ?, NULL, ?, ?, ?, \"[]\", FALSE, \"user\", \"{}\", NULL, \"online\") ",
            (user_id, username, email, password_salt, password_hash, registered))
    connection.commit()

    new_token = token.generate_token()
    token.add_token(user_id, token.hash_token(new_token))

    return new_token, 200