All Articles

์žฅ๊ณ ์—์„œ ํŽ˜์ด์Šค๋ถ ๊ธฐ๋ฐ˜ ํ† ํฐ ์ธ์ฆ ์‚ฌ์šฉํ•˜๊ธฐ

์˜ˆ์ „๋ถ€ํ„ฐ ์žฅ๊ณ ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋”ฉ์„ ๊ฑฐ์˜ ํ•˜์ง€ ์•Š๊ณ ์„œ๋„ ์ธ์ฆ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒํ•˜๋Š” ํ™˜์ƒ์ด ์žˆ์—ˆ๋Š”๋ฐ, ๋งˆ์นจ ์žฅ๊ณ ๋ฅผ ์ด์šฉํ•˜๋Š” ๋„๊ตฌ์— ์ธ์ฆ์„ ๋ถ™์ผ ์ผ์ด ์ƒ๊ฒจ์„œ ๊ทธ ํ™˜์ƒ์„ ์‹คํ˜„ํ•  ๊ธฐํšŒ๋ฅผ ์–ป์—ˆ๋‹ค.

๊ฒฐ๋ก ๋ถ€ํ„ฐ ๋งํ•˜์ž๋ฉด ์ฝ”๋”ฉ์€ ์ •๋ง ํ•œ ์ค„๋„ ํ•˜์ง€ ์•Š์•˜๋‹ค. ๋Œ€์‹  ํ‰์†Œ ์žฅ๊ณ ์— ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ ์—ฌ๋Ÿฌ ํŠœํ† ๋ฆฌ์–ผ๊ณผ ์งˆ๋ฌธ/๋‹ต๋ณ€์„ ์˜ฎ๊ฒจ๋‹ค๋‹ˆ๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ์ฐธ์กฐํ–ˆ๊ธฐ์— ๊ทธ ๊ณผ์ •์„ ๋ฏธ๋ž˜์˜ ๋‚˜๋ฅผ ์œ„ํ•ด ํ•˜๋‚˜์˜ ํŠœํ† ๋ฆฌ์–ผ๋กœ ๋‚จ๊ฒจ๋ณธ๋‹ค. ๊ฐ ๋‹จ๊ณ„๋ณ„๋กœ ๋งํฌ๋œ ํŠœํ† ๋ฆฌ์–ผ์„ ๋”ฐ๋ผ ํ–ˆ๋‹ค๋Š” ์ „์ œํ•˜์—, ๋‚ด๊ฐ€ ๋น ๋œจ๋ ธ๋˜ ๋ถ€๋ถ„์„ ์ฒจ์–ธํ•˜๋Š” ์‹์œผ๋กœ ํŠœํ† ๋ฆฌ์–ผ์„ ๊ตฌ์„ฑํ•ด๋ณด์•˜๋‹ค.

1. Django REST framework (์ดํ•˜ DRF) ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ - ํŠœํ† ๋ฆฌ์–ผ ์ฐธ์กฐ.

๋ญ”๊ฐ€ RESTful ํ•˜๊ฒŒ ๋ณด์—ฌ์ค„ ๊ฒƒ์ด ์žˆ์–ด์•ผํ•˜๋‹ˆ๊นŒ ์žฅ๊ณ  ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๋ฉด ์ƒ๊ธฐ๋Š” User ๋ฅผ ์œ„ํ•œ Serializer ๋ฅผ ๋งŒ๋“ ๋‹ค. ์ญ‰ ๋”ฐ๋ผ๊ฐ€๋‹ค๊ฐ€ URL ๋ถ€๋ถ„์—์„œ ์•ฝ๊ฐ„ ์ฐจ์ด์ ์ด ์ƒ๊ธด๋‹ค. DRF ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” admin ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ๋‚˜์ค‘์— django-allauth ์— ํŽ˜์ด์Šค๋ถ ์•ฑ์„ ์„ค์ •ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— admin ์„ ๋‚จ๊ฒจ์ค˜์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์›๋ž˜ ์ฝ”๋“œ์— ๋‘ ์ค„ ์ถ”๊ฐ€ํ•ด์„œ

from django.contrib import admin #์–ด๋“œ๋ฏผ
from django.conf.urls import url, include
from rest_framework import routers
from tutorial.quickstart import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^admin/', admin.site.urls), #์–ด๋“œ๋ฏผ
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

url.py๋ฅผ ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค. ๊ณ„์† ํŠœํ† ๋ฆฌ์–ผ์„ ๋”ฐ๋ผ๊ฐ€๋ณธ๋‹ค.

2. django-rest-auth ๋ถ™์ด๊ธฐ

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํŠœํ† ๋ฆฌ์–ผ ์„ ๋”ฐ๋ผ๊ฐ„๋‹ค. ์‹œ๊ฐ„์„ ๋‹ค์†Œ ๋งŽ์ด ์“ด ๋ถ€๋ถ„์€ Social Authentication ๋ถ€๋ถ„์˜ Add Social Application in django admin panel ๋ถ€๋ถ„์ด์—ˆ๋Š”๋ฐ, ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์˜์กดํ•˜๊ณ  ์žˆ๋Š” django-allauth ๋ฅผ ํ•œ ๋ฒˆ๋„ ์‚ฌ์šฉํ•œ ์ ์ด ์—†์–ด์„œ ๋งŽ์ด ํ—ค๋งธ๋‹ค. ๋ฌด์„œ์›Œํ•˜์ง€๋ง๊ณ  django-allauth ์˜ ํŠœํ† ๋ฆฌ์–ผ์„ ๋”ฐ๋ผ ๊ฐ€๋ณธ๋‹ค. ์šฐ๋ฆฌ๋Š” ํ† ํฐ ๊ธฐ๋ฐ˜์˜ ์ธ์ฆ์„ ์‚ฌ์šฉํ•  ํ…Œ๋‹ˆ settings.py์—

REST_SESSION_LOGIN = False

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    )
}

์ด๋Ÿฐ ์‹์œผ๋กœ ์„ค์ •ํ•ด์ค€๋‹ค.

3. django-allauth ์—์„œ ์„ค์ •ํ•˜๊ธฐ

๋‹ค์‹œ ๋˜ ํŠœํ† ๋ฆฌ์–ผ ์„ ๋”ฐ๋ผ๊ฐ„๋‹ค. ํŽ˜์ด์Šค๋ถ ์ฝ˜์†”์—์„œ ์•ฑ ๋“ฑ๋กํ•˜๋Š” ๋ถ€๋ถ„์€ ํŒจ์Šค. SITE_ID๋ผ๋Š” ๊ฐœ๋…์ด ์ƒ์†Œํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์–ด์ฐจํ”ผ ์ง€๊ธˆ์€ ์‚ฌ์ดํŠธ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ˆ ์–ด๋“œ๋ฏผ์—์„œ sites ๋กœ ๋“ค์–ด๊ฐ€ ์ฒ˜์Œ์— ๋“ค์–ด์žˆ๋Š” โ€˜example.comโ€™์„ ์‚ฌ์šฉํ•  hostname ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค.

4. django-allauth ์—์„œ ํŽ˜์ด์Šค๋ถ ์„ค์ •ํ•˜๊ธฐ ๋งํฌ

django-allauth ๋Š” ๋‹ค์–‘ํ•œ ์„œ๋น„์Šค์˜ ๋กœ๊ทธ์ธ์„ ์ง€์›ํ•˜๋Š”๋ฐ, ์šฐ๋ฆฌ๋Š” ๊ทธ ์ค‘ ํŽ˜์ด์Šค๋ถ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ๋งํฌ๋ฅผ ๋”ฐ๋ผ settings.py์— ์„ค์ •์„ ๋„ฃ์–ด์ค€๋‹ค. ์˜ˆ์ œ์—๋Š” ํ•ญ๋ชฉ์ด ๋งŽ์ด ๋‚˜์™€์žˆ๋Š”๋ฐ ํ˜„์žฌ ์šฉ๋„์—์„œ๋Š”

SOCIALACCOUNT_PROVIDERS = {
    'facebook': {
        'SCOPE': ['email'],
        'METHOD': 'oauth2'
    }
}

์ด์ •๋„๋งŒ ์จ์ค˜๋„ ๋ฌด๋ฆฌ๊ฐ€ ์—†๋‹ค.

5. ์จ๋ณด๊ธฐ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ Facebook ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋งŒ๋“  ๋‹ค์Œ์— (React ์—์„œ๋Š” React Facebook Login์ด ๊ฐ„ํŽธํ•˜๋‹ค) ๋กœ๊ทธ์ธ ์„ฑ๊ณต์‹œ ๋ฐ›์€ ํ† ํฐ์œผ๋กœ rest-auth/facebook/ ์— POST ์š”์ฒญ์„ ๋‚ ๋ ค์ฃผ๋ฉด ๋œ๋‹ค. ํ—ค๋”์˜ Authroization ๋ž€์— ํ† ํฐ์„ ๋„ฃ์„ ๋•Œ์—๋Š” โ€œToken XXXโ€ (๊ณต๋ฐฑ ํฌํ•จ) ์‹์œผ๋กœ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค.

์ด๊ฑด ๋ง ๊ทธ๋Œ€๋กœ ๊ฒ‰ํ•ฅ๊ธฐ๊ณ , ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋ฌด์–ธ๊ฐ€๋ฅผ ๋งŒ๋“œ๋ ค๋ฉด ์žฅ๊ณ ์˜ ์œ ์ € ๋ชจ๋ธ๊ณผ ์ธ์ฆ์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€๋ฅผ ํ›จ์”ฌ ๋” ๋งŽ์ด ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ๋ž˜๋„ ์ผ๋‹จ ํ•œ ๋ฒˆ ์ต์ˆ™ํ•ด์ง€๋ฉด ๊ฐœ๋ฐœ ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ˆ ์žฅ๊ณ ๋ฅผ ์ฃผ๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๋ถ„๋“ค์€ ์ƒ์‚ฐ์„ฑ์ด ์ •๋ง ์–ด๋งˆ์–ด๋งˆํ•˜์‹œ๊ฒ ๊ตฌ๋‚˜ ์‹ถ๋‹ค.

Published Aug 1, 2017

If I keep marking the dots, someday they will ๐Ÿ”—๐Ÿ”—