ahopkins / sanic-jwt

Authentication, JWT, and permission scoping for Sanic
http://sanic-jwt.readthedocs.io
MIT License
242 stars 52 forks source link

How to redirect to login page when protected route is hitted? #176

Closed zwjat closed 4 years ago

zwjat commented 4 years ago

When a route is protected, request to it will result Unauthorized exception. But I want to redirect login page to fill username and password to authenticate. I am sorry I don't know where and how I can put code to inplement it. Could anyone help me ? Thank a lot.

ahopkins commented 4 years ago

For the sake of anyone else that stumbles upon this: see docs

Either on the endpoint:

@sanicjwt.protected(redirect_on_fail=True, redirect_url="/unprotected")
async def my_protected_static(request):
    return text("", status=200)

Or globally:

sanicjwt = Initialize(
    ..., login_redirect_url="/unprotected"
)