|
| 1 | +from flask import request, jsonify |
| 2 | +from .. import app |
| 3 | +from ..auth import register_user, authenticate_user, logout_user, delete_user, get_user_from_session, is_authenticated |
| 4 | + |
| 5 | +@app.route('/auth/register', methods=['POST']) |
| 6 | +def register(): |
| 7 | + """ |
| 8 | + Register a new user |
| 9 | + --- |
| 10 | + tags: |
| 11 | + - Authentication |
| 12 | + parameters: |
| 13 | + - name: body |
| 14 | + in: body |
| 15 | + required: true |
| 16 | + schema: |
| 17 | + type: object |
| 18 | + properties: |
| 19 | + username: |
| 20 | + type: string |
| 21 | + password: |
| 22 | + type: string |
| 23 | + responses: |
| 24 | + 200: |
| 25 | + description: User registered successfully |
| 26 | + 400: |
| 27 | + description: Registration failed |
| 28 | + """ |
| 29 | + data = request.json |
| 30 | + username = data.get('username') |
| 31 | + password = data.get('password') |
| 32 | + |
| 33 | + if not username or not password: |
| 34 | + return jsonify({"error": "Username and password are required"}), 400 |
| 35 | + |
| 36 | + success, message = register_user(username, password) |
| 37 | + |
| 38 | + if success: |
| 39 | + return jsonify({"message": message}), 200 |
| 40 | + else: |
| 41 | + return jsonify({"error": message}), 400 |
| 42 | + |
| 43 | +@app.route('/auth/login', methods=['POST']) |
| 44 | +def login(): |
| 45 | + """ |
| 46 | + Login a user |
| 47 | + --- |
| 48 | + tags: |
| 49 | + - Authentication |
| 50 | + parameters: |
| 51 | + - name: body |
| 52 | + in: body |
| 53 | + required: true |
| 54 | + schema: |
| 55 | + type: object |
| 56 | + properties: |
| 57 | + username: |
| 58 | + type: string |
| 59 | + password: |
| 60 | + type: string |
| 61 | + responses: |
| 62 | + 200: |
| 63 | + description: Login successful |
| 64 | + 401: |
| 65 | + description: Login failed |
| 66 | + """ |
| 67 | + data = request.json |
| 68 | + username = data.get('username') |
| 69 | + password = data.get('password') |
| 70 | + |
| 71 | + if not username or not password: |
| 72 | + return jsonify({"error": "Username and password are required"}), 400 |
| 73 | + |
| 74 | + success, result = authenticate_user(username, password) |
| 75 | + |
| 76 | + if success: |
| 77 | + return jsonify({ |
| 78 | + "message": "Login successful", |
| 79 | + "token": result, |
| 80 | + "username": username |
| 81 | + }), 200 |
| 82 | + else: |
| 83 | + return jsonify({"error": result}), 401 |
| 84 | + |
| 85 | +@app.route('/auth/logout', methods=['POST']) |
| 86 | +def logout(): |
| 87 | + """ |
| 88 | + Logout a user |
| 89 | + --- |
| 90 | + tags: |
| 91 | + - Authentication |
| 92 | + parameters: |
| 93 | + - name: Authorization |
| 94 | + in: header |
| 95 | + type: string |
| 96 | + required: true |
| 97 | + description: Bearer token |
| 98 | + responses: |
| 99 | + 200: |
| 100 | + description: Logout successful |
| 101 | + """ |
| 102 | + auth_header = request.headers.get('Authorization') |
| 103 | + |
| 104 | + if not auth_header or not auth_header.startswith('Bearer '): |
| 105 | + return jsonify({"message": "Logged out"}), 200 |
| 106 | + |
| 107 | + session_id = auth_header.split('Bearer ')[1] |
| 108 | + logout_user(session_id) |
| 109 | + |
| 110 | + return jsonify({"message": "Logged out successfully"}), 200 |
| 111 | + |
| 112 | +@app.route('/auth/user', methods=['GET']) |
| 113 | +def get_user(): |
| 114 | + """ |
| 115 | + Get current user information |
| 116 | + --- |
| 117 | + tags: |
| 118 | + - Authentication |
| 119 | + parameters: |
| 120 | + - name: Authorization |
| 121 | + in: header |
| 122 | + type: string |
| 123 | + required: true |
| 124 | + description: Bearer token |
| 125 | + responses: |
| 126 | + 200: |
| 127 | + description: User information |
| 128 | + 401: |
| 129 | + description: Not authenticated |
| 130 | + """ |
| 131 | + auth_header = request.headers.get('Authorization') |
| 132 | + |
| 133 | + if not auth_header or not auth_header.startswith('Bearer '): |
| 134 | + return jsonify({"error": "Not authenticated"}), 401 |
| 135 | + |
| 136 | + session_id = auth_header.split('Bearer ')[1] |
| 137 | + user = get_user_from_session(session_id) |
| 138 | + |
| 139 | + if not user: |
| 140 | + return jsonify({"error": "Not authenticated"}), 401 |
| 141 | + |
| 142 | + return jsonify({ |
| 143 | + "username": user["username"], |
| 144 | + "created_at": user["created_at"] |
| 145 | + }), 200 |
| 146 | + |
| 147 | +@app.route('/auth/delete-account', methods=['DELETE']) |
| 148 | +def delete_account(): |
| 149 | + """ |
| 150 | + Delete user account |
| 151 | + --- |
| 152 | + tags: |
| 153 | + - Authentication |
| 154 | + parameters: |
| 155 | + - name: Authorization |
| 156 | + in: header |
| 157 | + type: string |
| 158 | + required: true |
| 159 | + description: Bearer token |
| 160 | + responses: |
| 161 | + 200: |
| 162 | + description: Account deleted successfully |
| 163 | + 401: |
| 164 | + description: Not authenticated |
| 165 | + """ |
| 166 | + auth_header = request.headers.get('Authorization') |
| 167 | + |
| 168 | + if not auth_header or not auth_header.startswith('Bearer '): |
| 169 | + return jsonify({"error": "Not authenticated"}), 401 |
| 170 | + |
| 171 | + session_id = auth_header.split('Bearer ')[1] |
| 172 | + user = get_user_from_session(session_id) |
| 173 | + |
| 174 | + if not user: |
| 175 | + return jsonify({"error": "Not authenticated"}), 401 |
| 176 | + |
| 177 | + username = user["username"] |
| 178 | + if delete_user(username): |
| 179 | + return jsonify({"message": "Account deleted successfully"}), 200 |
| 180 | + else: |
| 181 | + return jsonify({"error": "Failed to delete account"}), 400 |
| 182 | + |
| 183 | +@app.route('/auth/check', methods=['GET']) |
| 184 | +def check_auth(): |
| 185 | + """ |
| 186 | + Check if user is authenticated |
| 187 | + --- |
| 188 | + tags: |
| 189 | + - Authentication |
| 190 | + parameters: |
| 191 | + - name: Authorization |
| 192 | + in: header |
| 193 | + type: string |
| 194 | + required: true |
| 195 | + description: Bearer token |
| 196 | + responses: |
| 197 | + 200: |
| 198 | + description: Authentication status |
| 199 | + """ |
| 200 | + auth_header = request.headers.get('Authorization') |
| 201 | + |
| 202 | + if not auth_header or not auth_header.startswith('Bearer '): |
| 203 | + return jsonify({"authenticated": False}), 200 |
| 204 | + |
| 205 | + session_id = auth_header.split('Bearer ')[1] |
| 206 | + authenticated = is_authenticated(session_id) |
| 207 | + |
| 208 | + return jsonify({"authenticated": authenticated}), 200 |
0 commit comments