김대연

회원정보 수정 리팩토링

1 +import datetime
1 import json 2 import json
2 import jwt 3 import jwt
3 import uuid 4 import uuid
...@@ -88,7 +89,8 @@ def login(request): ...@@ -88,7 +89,8 @@ def login(request):
88 return {'result': False, 'error': '로그인에 실패하였습니다.'} 89 return {'result': False, 'error': '로그인에 실패하였습니다.'}
89 90
90 # Token Generate 91 # Token Generate
91 - token = jwt.encode({'id': user[0].id}, key=settings.SECRET_KEY, algorithm='HS256') 92 + token = jwt.encode({'id': user[0].id, 'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=6)},
93 + key=settings.SECRET_KEY, algorithm='HS256')
92 94
93 return {'result': True, 'token': token.decode('utf-8')} 95 return {'result': True, 'token': token.decode('utf-8')}
94 96
...@@ -119,4 +121,40 @@ def find_me(request): ...@@ -119,4 +121,40 @@ def find_me(request):
119 121
120 # 회원정보 수정 122 # 회원정보 수정
121 def update_me(request): 123 def update_me(request):
124 + # TODO: Auth
125 + request.user_id = 1
126 +
127 + # Load
128 + try:
129 + received = json.loads(request.body.decode('utf-8'))
130 + except json.decoder.JSONDecodeError:
131 + return {'result': False, 'error': '입력이 잘못되었습니다.'}
132 +
133 + # Validate
134 + if 'name' not in received \
135 + and ('old_password' not in received and 'password' not in received):
136 + return {'result': False, 'error': '입력이 누락되었습니다.'}
137 +
138 + # Query
139 + user = User.objects.filter(id=request.user_id)
140 +
141 + # Check Exists
142 + if len(user) != 1:
143 + return {'result': False, 'error': '잘못된 요청입니다.'}
144 +
145 + # Change Name
146 + if 'name' in received:
147 + user[0].name = received['name']
148 +
149 + # Change Password
150 + if 'old_password' in received and 'password' in received:
151 + if check_password(received['old_password'], user[0].password) is False:
152 + return {'result': False, 'error': '이전 비밀번호가 잘못되었습니다.'}
153 + if len(received['password']) < 8:
154 + return {'result': False, 'error': '비밀번호는 최소 8글자 입니다.'}
155 + user[0].password = make_password(received['password'])
156 +
157 + # Save
158 + user[0].save()
159 +
122 return {'result': True} 160 return {'result': True}
......