summaryrefslogtreecommitdiffstats
path: root/nfsw/auth.py
blob: 7eec2a7046cee077e4c4efa68a68a6dfc7f8010a (plain) (blame)
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
import functools

import os

from flask import (
    Blueprint, flash, g, redirect, render_template, request,
    session, url_for
)
from werkzeug.security import (
    check_password_hash, generate_password_hash
)

from nfsw.db import get_db


bp = Blueprint('auth', __name__)

def register(username):
    db = get_db()

    if db.execute('SELECT id FROM user where username=?', (username,)
    ).fetchone() is not None:
        return {
            'status': 'pass',
            'msg': 'Looks you\'ve registered before!'
            + ' Gimme your password. Pretty please.'
        }

    password = os.urandom(4).hex()

    r = db.execute('INSERT INTO user (username, password) VALUES (?, ?)',
               (username, generate_password_hash(password)))
    db.commit()

    print(r.fetchone)
    print(password)

    session.clear()
    session['newuser'] = True

    return {
        'status': 'ok',
        'url': url_for('hello')
    }