django-rest-auth icon indicating copy to clipboard operation
django-rest-auth copied to clipboard

Allow dynamic callback_url and client_class class attributes

Open michaeldel opened this issue 7 years ago • 4 comments

Callback urls require being absolute, which is pretty cumbersome espacially when dealing with differents environments (dev/staging/production for instance). Being able to provide dynamic callbacks url hence allows for things like this:

class GitHubLogin(SocialLoginView):
    adapter_class = github_views.GitHubOAuth2Adapter
    client_class = OAuth2Client

    # use the same callback url as defined in your GitHub app, this url must be
    # absolute
    def get_callback_url(self):
        return self.request.build_absolute_uri(reverse('github_callback'))

urlpatterns = [
    ...,
    path('auth/github/', GitHubLogin.as_view()),
    path('auth/github/callback/', github_callback, name='github_callback')
]

Which really comes in handy in multiple environments.

I also changed client_class the same way for code homegeneity concerns (even though I did not have to use it in my own projects).

The "basic way" of declaring these will of course still work:

class GitHubLogin(SocialLoginView):
    adapter_class = github_views.GitHubOAuth2Adapter
    client_class = OAuth2Client
    callback_url = 'http://localhost:8000/auth/github/callback/'

michaeldel avatar Jul 26 '18 15:07 michaeldel

Coverage Status

Coverage decreased (-0.2%) to 96.186% when pulling d7532b875ed77f9fda94863aa400b3d6d926a64a on michaeldel:master into 95fafe5e0f6716296a1c664c2b870876a6b4e0cc on Tivix:master.

coveralls avatar Jul 26 '18 15:07 coveralls

Hi, as a user of rest-auth, thanks for the contribution! This repo is not maintained anymore, so the development moved to dj-rest-auth. (reference: https://github.com/Tivix/django-rest-auth/issues/568) It may be best, if you move this PR there. (and upgrade to using dj_rest_auth)

new repo link: https://github.com/jazzband/dj-rest-auth (I'm not the upkeeper of that repo, it just makes sense for me to help you merge your PR)

Many Thanks, Barney

BarnabasSzabolcs avatar May 30 '20 01:05 BarnabasSzabolcs

@BarnabasSzabolcs thank you for suggesting it, I was not aware of this fork. PR has been submitted on this new fork here: https://github.com/jazzband/dj-rest-auth/pull/80

michaeldel avatar Jun 01 '20 06:06 michaeldel

Thanks Michael!

BarnabasSzabolcs avatar Jun 01 '20 13:06 BarnabasSzabolcs