Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support add authenticate method from class_views #239

Open
miss85246 opened this issue Jun 28, 2023 · 0 comments
Open

support add authenticate method from class_views #239

miss85246 opened this issue Jun 28, 2023 · 0 comments

Comments

@miss85246
Copy link

miss85246 commented Jun 28, 2023

Support add authenticate method from class_views.

The current situation is when we Initialize, we must give the authenticate argument, or else raise AuthenticateNotImplemented Error:

sanic_jwt.exceptions.AuthenticateNotImplemented: Sanic JWT initialized without providing an authenticate method.

Sometimes, to maintain the consistency of the entrance, we will define all endpoints in one file, and export a class_views tuple. all of the endpoints will in class_views tuple, include authenticate method. for example:

# define all endpoints in authentication_view.py

from sanic_ext import validate
from sanic_jwt import BaseEndpoint

from schema.schemaUser import UserLogin, UserRegister
from services.serviceAuthorization import ServiceAuthorization
from utils import json_response


class AuthenticateEndpoint(BaseEndpoint):

    @validate(json=UserLogin)
    async def post(self, request, body: UserLogin, *args, **kwargs):
        pass


class RegisterEndpoint(BaseEndpoint):

    @validate(json=UserRegister)
    async def post(self, request, *args, **kwargs):
        pass

sanic_jwt_views = (
    ('/auth/register', RegisterEndpoint),
    ('/auth/login', AuthenticateEndpoint),
)

now we must initialize it in app.py like this:

# app.py

import ...

sanic_jwt_views = (
    ('/auth/register', RegisterEndpoint),
)

app = Sanic("my_jwt_server", config=config)
Initialize(server, class_views=authorization_views, authenticate=AuthenticateEndpoint.post, **config.jwt)

or else it will raise RouteExists Exception.

sanic_jwt_views = (
    ('/auth/register', RegisterEndpoint),
    ('/auth/login', AuthenticateEndpoint),
)

app = Sanic("my_jwt_server", config=config)
Initialize(server, class_views=authorization_views, authenticate=None, **config.jwt)
sanic_routing.exceptions.RouteExists: Route already registered: auth/auth/login [OPTIONS,POST]

I saw the class Initialize, in __add_endpoints function, we didn't do duplicate detection, and it will lead to RouteExists Error.

perhaps we need do duplicate detection in __add_endpoints function or __add_class_views function, and let authenticate to be a not require argument. to make our code more elegant and clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant