Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit ac31c1a

Browse files
authored
feat: Allow optional public user signup (#34)
1 parent 361e1f4 commit ac31c1a

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

app/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
ALLOWED_FILE_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
2424

2525
PRIVACY_POLICY_URL = os.getenv('PRIVACY_POLICY_URL')
26+
OPEN_REGISTRATION = os.getenv('OPEN_REGISTRATION', "False").lower() == "true"
2627

2728
DB_URL = URL.create(
2829
os.getenv('DB_DRIVER', "sqlite"),

app/controller/auth/auth_controller.py

+32-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from flask import jsonify, Blueprint, request
44
from flask_jwt_extended import current_user, jwt_required, get_jwt
55
from app.models import User, Token
6-
from app.errors import UnauthorizedRequest
7-
from .schemas import Login, CreateLongLivedToken
8-
from app.config import jwt
6+
from app.errors import UnauthorizedRequest, InvalidUsage
7+
from .schemas import Login, Signup, CreateLongLivedToken
8+
from app.config import jwt, OPEN_REGISTRATION
99

1010
auth = Blueprint('auth', __name__)
1111

@@ -63,6 +63,35 @@ def login(args):
6363
})
6464

6565

66+
if OPEN_REGISTRATION:
67+
@auth.route('signup', methods=['POST'])
68+
@validate_args(Signup)
69+
def signup(args):
70+
username = args['username'].strip().lower()
71+
user = User.find_by_username(username)
72+
if user:
73+
raise InvalidUsage()
74+
75+
user = User(username=username, name=args['name'].strip())
76+
user.set_password(args['password'])
77+
user.save()
78+
79+
device = "Unkown"
80+
if "device" in args:
81+
device = args['device']
82+
83+
# Create refresh token
84+
refreshToken, refreshModel = Token.create_refresh_token(user, device)
85+
86+
# Create first access token
87+
accesssToken, _ = Token.create_access_token(user, refreshModel)
88+
89+
return jsonify({
90+
'access_token': accesssToken,
91+
'refresh_token': refreshToken
92+
})
93+
94+
6695
@auth.route('/refresh', methods=['GET'])
6796
@jwt_required(refresh=True)
6897
def refresh():

app/controller/auth/schemas.py

+20
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ class Login(Schema):
1717
load_only=True,
1818
)
1919

20+
class Signup(Schema):
21+
username = fields.String(
22+
required=True,
23+
validate=lambda a: a and not a.isspace() and not "@" in a
24+
)
25+
name = fields.String(
26+
required=True,
27+
validate=lambda a: a and not a.isspace()
28+
)
29+
password = fields.String(
30+
required=True,
31+
validate=lambda a: a and not a.isspace(),
32+
load_only=True,
33+
)
34+
device = fields.String(
35+
required=False,
36+
validate=lambda a: a and not a.isspace(),
37+
load_only=True,
38+
)
39+
2040

2141
class CreateLongLivedToken(Schema):
2242
device = fields.String(

app/controller/health_controller.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from flask import jsonify, Blueprint
2-
from app.config import BACKEND_VERSION, MIN_FRONTEND_VERSION, PRIVACY_POLICY_URL
2+
from app.config import BACKEND_VERSION, MIN_FRONTEND_VERSION, PRIVACY_POLICY_URL, OPEN_REGISTRATION
33
from app.models import Settings
44
from app.config import SUPPORTED_LANGUAGES
55

@@ -15,6 +15,8 @@ def get_health():
1515
}
1616
if PRIVACY_POLICY_URL:
1717
info['privacy_policy'] = PRIVACY_POLICY_URL
18+
if OPEN_REGISTRATION:
19+
info['open_registration'] = True
1820
return jsonify(info)
1921

2022
@health.route('/supported-languages', methods=['GET'])

0 commit comments

Comments
 (0)