8000 support add authenticate method from `class_views` · Issue #239 · ahopkins/sanic-jwt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
support add authenticate method from class_views #239
Open
@miss85246

Description

@miss85246

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0