ACHO.pk devlog

[멋쟁이사자처럼10기] Django 영화 리뷰 사이트(소셜 로그인) 본문

멋쟁이사자처럼

[멋쟁이사자처럼10기] Django 영화 리뷰 사이트(소셜 로그인)

Acho 2022. 7. 19. 19:34

 

소셜 로그인 

 

카카오, 네이버, 구글 등을 이용해 소셜 로그인 하는 방법에 대해 알아보자.

먼저 , 소셜 로그인 기능을 사용하기 위해 패키지를 다운해야한다.

pip install django-allauth

https://django-allauth.readthedocs.io/en/latest/installation.html

 

Installation — django-allauth 0.43.0 documentation

Post-Installation In your Django root execute the command below to create your database tables: Now start your server, visit your admin pages (e.g. http://localhost:8000/admin/) and follow these steps: Add a Site for your domain, matching settings.SITE_ID

django-allauth.readthedocs.io

 

myproject/settings.py

INSTALLED_APPS = [

# The following apps are required:
    'django.contrib.auth',
    'django.contrib.messages',
    'django.contrib.sites',

    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    
    'allauth.socialaccount.providers.google',
    
]

SITE_ID = 1

 

urls.py

urlpatterns = [
    ...
    path('accounts/', include('allauth.urls')),
    ...
]

 

설정한 내용을 데이터 베이스에 반영한다.

 python manage.py migrate

 

사용자들에게 보이는 우리 사이트의 이름


사용할 수 있는 API나 사용자 인증 정보를 받아볼 수 있다.

https://console.cloud.google.com/

 

Google 클라우드 플랫폼

로그인 Google 클라우드 플랫폼으로 이동

accounts.google.com

 

1. API 및 서비스 > 프로젝트 만들기

2. 사용자 인증 정보 > 사용자 인증 정보 만들기 > OAuth 클라이언트 ID 만들기 > 동의 화면 구성 > 외부 > 저장 후 계속

3. 사용자 인증 정보 > 사용자 인증 정보 만들기 > OAuth 클라이언트 ID 만들기 > 어플리케이션 유형 - 웹 어플리케이션 > url 추가 > 승인된 리디렉션 URL - 'callback' url 적어주기 > 만들기

https://django-allauth.readthedocs.io/en/latest/providers.html

 

Providers — django-allauth 0.43.0 documentation

Most providers require you to sign up for a so called API client or app, containing a client ID and API secret. You must add a SocialApp record per provider via the Django admin containing these app credentials. When creating the OAuth app on the side of t

django-allauth.readthedocs.io

4. admin사이트 > Social applications에 등록

 

 

myproject/settings.py

  • 어떤 수단을 통해 로그인을 할 것인지..
  • 로그인에 성공했을 시 어디로 리다이렉션 시키는지..
AUTHENTICATION_BACKENDS = [
    ...
    # Needed to login by username in Django admin, regardless of `allauth`
    'django.contrib.auth.backends.ModelBackend',

    # `allauth` specific authentication methods, such as login by e-mail
    'allauth.account.auth_backends.AuthenticationBackend',
    ...
]

LOGIN_REDIRECT_URL = '/'

우리가 만든 로그인 페이지에서 구글 로그인 연결

 

accounts/login.html

<a href="{% provider_login_url 'google' %}" class="~~~~">
	<i class="~~~~~~"></i> 구글 계정으로 로그인
</a>
Comments