<filename>Server/app/views/account/auth.py
import requests

from flask import Blueprint, Response, abort, request
from flask_restful import Api
from flasgger import swag_from

from werkzeug.security import check_password_hash

from app.models.account import AccountModel, AccessTokenModel, RefreshTokenModel
from app.views import BaseResource, json_required

api = Api(Blueprint(__name__, __name__))
api.prefix = ''


@api.resource('/auth')
class Auth(BaseResource):
    @json_required({'email': str, 'pw': str})
    def post(self):
        """
        로그인
        """
        payload = request.json

        email = payload['email']
        pw = payload['pw']

        account = AccountModel.objects(id=email).first()

        if not account:
            abort(401)
        else:
            if check_password_hash(account.pw, pw):
                if not account.email_certified:
                    abort(403)

                if not account.nickname:
                    return Response('', 205)

                return {
                    'accessToken': AccessTokenModel.create_access_token(account, request.headers['USER_AGENT']),
                    'refreshToken': RefreshTokenModel.create_refresh_token(account, request.headers['USER_AGENT'])
                }
            else:
                abort(401)


@api.resource('/auth/facebook')
class FacebookAuth(BaseResource):
    FB_GRAPH_API_URL = 'https://graph.facebook.com/v2.6/{}?access_token=1925974487664670|D-wibfbjkOaHtINm_cwUSBx3<PASSWORD>'

    def get_name_through_fb_id(self, fb_id):
        resp = requests.get(self.FB_GRAPH_API_URL.format(fb_id))
        # 페이스북 graph api를 이용해 사용자 데이터 조회

        data = resp.json()

        return False if 'error' in data else data['name']

    @json_required({'id': str})
    def post(self):
        """
        페이스북 계정 로그인
        """
        payload = request.json

        id = payload['id']

        account = AccountModel.objects(id=id).first()

        if not account:
            # 사용자가 미존재, 회원가입을 시켜줌
            name = self.get_name_through_fb_id(id)

            if name:
                AccountModel(
                    id=id
                ).save()

                return {
                    'name': name
                }, 205
            else:
                abort(401)

        if not account.nickname:
            # 사용자는 존재하나 닉네임이 설정되어 있지 않은 경우
            return {
                'name': self.get_name_through_fb_id(id)
            }, 205

        return {
            'accessToken': AccessTokenModel.create_access_token(account, request.headers['USER_AGENT']),
            'refreshToken': RefreshTokenModel.create_refresh_token(account, request.headers['USER_AGENT'])
        }

<filename>Server/app/views/account/auth.py
import requests

from flask import Blueprint, Response, abort, request
from flask_restful import Api
from flasgger import swag_from

from werkzeug.security import check_password_hash

from app.models.account import AccountModel, AccessTokenModel, RefreshTokenModel
from app.views import BaseResource, json_required

api = Api(Blueprint(__name__, __name__))
api.prefix = ''


@api.resource('/auth')
class Auth(BaseResource):
    @json_required({'email': str, 'pw': str})
    def post(self):
        """
        로그인
        """
        payload = request.json

        email = payload['email']
        pw = payload['pw']

        account = AccountModel.objects(id=email).first()

        if not account:
            abort(401)
        else:
            if check_password_hash(account.pw, pw):
                if not account.email_certified:
                    abort(403)

                if not account.nickname:
                    return Response('', 205)

                return {
                    'accessToken': AccessTokenModel.create_access_token(account, request.headers['USER_AGENT']),
                    'refreshToken': RefreshTokenModel.create_refresh_token(account, request.headers['USER_AGENT'])
                }
            else:
                abort(401)


@api.resource('/auth/facebook')
class FacebookAuth(BaseResource):
    FB_GRAPH_API_URL = 'https://graph.facebook.com/v2.6/{}?access_token=1925974487664670|D-wibfbjkOaHtINm_cwUSBx3<PASSWORD>'

    def get_name_through_fb_id(self, fb_id):
        resp = requests.get(self.FB_GRAPH_API_URL.format(fb_id))
        # 페이스북 graph api를 이용해 사용자 데이터 조회

        data = resp.json()

        return False if 'error' in data else data['name']

    @json_required({'id': str})
    def post(self):
        """
        페이스북 계정 로그인
        """
        payload = request.json

        id = payload['id']

        account = AccountModel.objects(id=id).first()

        if not account:
            # 사용자가 미존재, 회원가입을 시켜줌
            name = self.get_name_through_fb_id(id)

            if name:
                AccountModel(
                    id=id
                ).save()

                return {
                    'name': name
                }, 205
            else:
                abort(401)

        if not account.nickname:
            # 사용자는 존재하나 닉네임이 설정되어 있지 않은 경우
            return {
                'name': self.get_name_through_fb_id(id)
            }, 205

        return {
            'accessToken': AccessTokenModel.create_access_token(account, request.headers['USER_AGENT']),
            'refreshToken': RefreshTokenModel.create_refresh_token(account, request.headers['USER_AGENT'])
        }

로그인 # 페이스북 graph api를 이용해 사용자 데이터 조회 페이스북 계정 로그인 # 사용자가 미존재, 회원가입을 시켜줌 # 사용자는 존재하나 닉네임이 설정되어 있지 않은 경우