A.Sasori
A.Sasori

Reputation: 405

How do I do a social media authentication in django

I want users to be able to register on my website through linkedin and it redirects them to a page with their instance of their username. e.g the redirect url will contain their username http://127.0.0.1:8000/the_username_from_linkedin_profile.

This is the url I want the user to be redirected to after registration with linkedin.

path(r'<str:username>/', views.dashboard, name='dashboard'),

This is the dashboard view.

@login_required
def dashboard(request, username):
    """Dashboard page for user"""

    if request.user.username != username:
        return redirect(reverse(
        'accounts:dashboard',
        args=(request.user.username,))
    )
    return render(request, 'accounts/dashboard.html')

This is the template that has the button for registration with linkedin.

<form id="registrationForm" class='white-popup-block mfp-hide' method='post' action="{% url 'accounts:register'%}">
    <div id="reg-errors"></div>
    <div class="form-group">
    {% csrf_token %}

{{ reg_form.fullname|add_class:'form-control input-upper my-3' }}
{{ reg_form.username|add_class:'form-control input-upper my-3' }}
{{ reg_form.email|add_class:'form-control input-upper my-3' }}
{{ reg_form.organization|add_class:'form-control input-upper my-3' }}
{{ reg_form.password1|add_class:'form-control input-upper my-3' }}
{{ reg_form.password2|add_class:'form-control input-upper my-3' }}
<small style="color:black" class="text-already"><b>By registering you agree to our</b><a href="{% url 'tos' %}"> terms and conditions</a></small>
<div class='text-center'>
  <img src='/static/images/loader.gif' id='regLoader' class='d-none' />
</div>
<div id="regSection">
  <button type="submit" class="btn btn-primary btn-block btn-signup-form">
    <i class="fa fa-user-plus fa-2x ml-10" aria-hidden="true"></i>
    SIGN UP
  </button>
  <a href="{% url 'social:begin' 'linkedin-oauth2' %}?next={{ next }}" class='btn btn-primary btn-block btn-sign-linkedin'> <i class="fab fa-linkedin-in"></i>  &nbsp; &nbsp;Register with Linkedin</i></a>
  {% comment %} <a href="{% url 'social:begin' 'linkedin-oauth2' %}?next={{ next }}" class='btn btn-primary btn-block btn-sign-linkedin'> <i class="fab fa-linkedin-in"></i>  &nbsp; &nbsp;Register with Linkedin</i></a> {% endcomment %}
  <p style="color:black" class="text-already d-none d-md-block"><b>Already have an account? </b><a id='sidebarCollapse1' href="#" >LOGIN</a></p>
  <p style="color:black" class="text-already d-md-none"><b>Already have an account? </b><a id='loginRedirect' href="#loginForm" >LOGIN</a></p>
</div>

Upvotes: 1

Views: 302

Answers (1)

Devang Padhiyar
Devang Padhiyar

Reputation: 3697

You could also use allauth package in django it given you very flexible way to implement social media authentication. Such as facebook, google, twitter and so on.

Find documentation from here

Upvotes: 2

Related Questions