This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
auth.py
89 lines (62 loc) · 2.34 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from flask_restx import Resource, Namespace, fields
from models import User
from werkzeug.security import generate_password_hash, check_password_hash
from flask_jwt_extended import (
JWTManager,
create_access_token,
create_refresh_token,
get_jwt_identity,
jwt_required,
)
from flask import Flask, request, jsonify, make_response
auth_ns = Namespace("auth", description="A namespace for our Authentication")
signup_model = auth_ns.model(
"SignUp",
{
"username": fields.String(),
"email": fields.String(),
"password": fields.String(),
},
)
login_model = auth_ns.model(
"Login", {"username": fields.String(), "password": fields.String()}
)
@auth_ns.route("/signup")
class SignUp(Resource):
@auth_ns.expect(signup_model)
def post(self):
data = request.get_json()
username = data.get("username")
db_user = User.query.filter_by(username=username).first()
if db_user is not None:
return jsonify({"message": f"User with username {username} already exists"})
new_user = User(
username=data.get("username"),
email=data.get("email"),
password=generate_password_hash(data.get("password")),
)
new_user.save()
return make_response(jsonify({"message": "User created successfuly"}), 201)
@auth_ns.route("/login")
class Login(Resource):
@auth_ns.expect(login_model)
def post(self):
data = request.get_json()
username = data.get("username")
password = data.get("password")
db_user = User.query.filter_by(username=username).first()
if db_user and check_password_hash(db_user.password, password):
access_token = create_access_token(identity=db_user.username)
refresh_token = create_refresh_token(identity=db_user.username)
return jsonify(
{"access_token": access_token, "refresh_token": refresh_token}
)
else:
return jsonify({"message": "Invalid username or password"})
@auth_ns.route("/refresh")
class RefreshResource(Resource):
@jwt_required(refresh=True)
def post(self):
current_user = get_jwt_identity()
new_access_token = create_access_token(identity=current_user)
return make_response(jsonify({"access_token": new_access_token}), 200)