mirror of
https://github.com/ReVanced/revanced-api.git
synced 2026-01-18 08:53:57 +00:00
feat: Add announcements endpoints (#91)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Alexandre Teles (afterSt0rm) <alexandre.teles@ufba.br>
This commit is contained in:
54
api/login.py
Normal file
54
api/login.py
Normal file
@@ -0,0 +1,54 @@
|
||||
"""
|
||||
This module provides a blueprint for the login endpoint.
|
||||
|
||||
Routes:
|
||||
- POST /login: Login to the API
|
||||
"""
|
||||
|
||||
from sanic import Blueprint, Request
|
||||
from sanic.response import JSONResponse, json
|
||||
from sanic_ext import openapi
|
||||
from sanic_beskar.exceptions import AuthenticationError
|
||||
|
||||
from auth import beskar
|
||||
from limiter import limiter
|
||||
|
||||
from config import api_version
|
||||
|
||||
|
||||
login: Blueprint = Blueprint("login", version=api_version)
|
||||
|
||||
|
||||
@login.post("/login")
|
||||
@openapi.definition(
|
||||
summary="Login to the API",
|
||||
)
|
||||
@limiter.limit("3 per hour")
|
||||
async def login_user(request: Request) -> JSONResponse:
|
||||
"""
|
||||
Login to the API.
|
||||
|
||||
**Args:**
|
||||
- username (str): The username of the user to login.
|
||||
- password (str): The password of the user to login.
|
||||
|
||||
**Returns:**
|
||||
- JSONResponse: A Sanic JSONResponse object containing the access token.
|
||||
"""
|
||||
|
||||
req = request.json
|
||||
username = req.get("username", None)
|
||||
password = req.get("password", None)
|
||||
if not username or not password:
|
||||
return json({"error": "Missing username or password"}, status=400)
|
||||
|
||||
try:
|
||||
user = await beskar.authenticate(username, password)
|
||||
except AuthenticationError:
|
||||
return json({"error": "Invalid username or password"}, status=403)
|
||||
|
||||
if not user:
|
||||
return json({"error": "Invalid username or password"}, status=403)
|
||||
|
||||
ret = {"access_token": await beskar.encode_token(user)}
|
||||
return json(ret, status=200)
|
||||
Reference in New Issue
Block a user