code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
์—ฌ๊ธฐ์„œ ์ด๋ ‡๊ฒŒ else ๋กœ ์žก์•„์ฃผ์‹œ๊ธฐ ๋ณด๋‹ค๋Š” checkpw() ์˜ ๊ฐ’์ด Falsy ํ•˜๋‹ค๋ฉด 401 ์„ ๋ฆฌํ„ดํ•ด์ฃผ์‹œ๊ณ , ๋„˜์–ด์™€์„œ ์ง€๊ธˆ ์œ„์— ๋ณด์‹œ๋Š” If ๋ฌธ์˜ ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•ด์ฃผ์‹ ๋‹ค๋ฉด ๋ถˆํ•„์š”ํ•œ else ๋ฌธ์ด ์—†์–ด์ง€๊ฒ ์ฃ ?
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
ํ† ํฐ์—์„œ ์œ ์ €์ •๋ณด๋ฅผ ์ฐพ์•„์™€์„œ ํ•„ํ„ฐํ•˜๋Š” ๊ธฐ๋Šฅ๊นŒ์ง€! ๐Ÿ‘ ๐Ÿ‘ ๐Ÿ‘ ๋‹ค๋ฏผ๋‹˜ ์ฒ˜์Œ์—๋Š” ์ดํ•ดํ•˜๋Š”๋ฐ ์–ด๋ ค์›Œํ•˜์‹œ๋Š” ๊ฒƒ ๊ฐ™์•„ ๊ฑฑ์ •์„ ์กฐ๊ธˆ ํ–ˆ์—ˆ๋Š”๋ฐ, ์ง€๊ธˆ๋ณด๋ฉด ๋ฌธ์ œ ํ•ด๊ฒฐ๋Šฅ๋ ฅ์ด ์ •๋ง ์ข‹์œผ์‹  ๊ฒƒ ๊ฐ™์•„์š”! ๋„ˆ๋ฌด ์ž˜ํ•˜์…จ์Šต๋‹ˆ๋‹คใ…Žใ…Ž
@@ -0,0 +1,12 @@ +from django.db import models + +class User(models.Model): + + email = models.EmailField(max_length=50, unique=True) + name = models.CharField(max_length=50, unique=True) + phone = models.CharField(max_length=50, unique=True) + password= models.CharField(max_length=500) + + class Meta: + db_table='users' +
Python
- email ์€ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด, ๊ทธ๋ฆฌ๊ณ  ์ถ”ํ›„ ์žฅ๊ณ  Form ์ด ์ œ๊ณตํ•˜๋Š” validator ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์„ ๊ฐ์•ˆํ•ด์„œ EmailField ๋กœ ์„ ์–ธํ•ด์ฃผ์„ธ์š”! - ๊ทธ๋ฆฌ๊ณ  email ์€ ๊ณ ์œ ๊ฐ’์ด์ฃ ? ์ด๋Ÿด๋•Œ ์ถ”๊ฐ€ํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค! ์ฐพ์•„์„œ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”~
@@ -0,0 +1,12 @@ +from django.db import models + +class User(models.Model): + + email = models.EmailField(max_length=50, unique=True) + name = models.CharField(max_length=50, unique=True) + phone = models.CharField(max_length=50, unique=True) + password= models.CharField(max_length=500) + + class Meta: + db_table='users' +
Python
๋ณดํ†ต ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ปฌ๋Ÿผ๋ช…์€ ์ค„์—ฌ์“ฐ์ง€์•Š๊ณ  ์ตœ๋Œ€ํ•œ ๋ช…ํ™•ํ•˜๊ฒŒ ํ’€์–ด์„œ ์„ ์–ธํ•ด์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๋Š” password ๋กœ ํ’€์–ด์ฃผ์„ธ์š”~ ๊ทธ๋ฆฌ๊ณ  ์ด์ œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ๊ณผ์ •์„ ์ง„ํ–‰ํ•˜๋Š”๋งŒํผ ์ตœ๋Œ€ ๊ธ€์ž์ˆ˜๋ฅผ ์กฐ๊ธˆ ๋” ๋„‰๋„‰ํ•˜๊ฒŒ ์žก์•„์ฃผ์‹œ๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
- exists ๋ฉ”์†Œ๋“œ ๋„ˆ๋ฌด ์ž˜ ํ™œ์šฉํ•˜์…จ๋„ค์š”!! ๐Ÿ‘ - ์—ฌ๊ธฐ์„œ exists ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋”ฐ๋กœ ๋ณ€์ˆ˜์— ์ €์žฅํ•ด์ค„๊ฒƒ ์—†์ด ์•„๋ž˜ if ๋ฌธ์— ๋ฐ”๋กœ ์ ์šฉํ•ด์ฃผ์‹ ๋‹ค๋ฉด ๋ถˆํ•„์š”ํ•œ ๋ณ€์ˆ˜ ์„ ์–ธ์„ ์—†์•จ ์ˆ˜ ์žˆ๊ฒ ์ฃ ?!
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
or, not, in ํ™œ์šฉ ๋„ˆ๋ฌด ์ข‹์Šต๋‹ˆ๋‹ค ํ˜ธ์—ด๋‹˜!!! ๊ทธ๋Ÿฐ๋ฐ email ์„ list ๋กœ ํ˜•๋ณ€ํ™˜ํ•ด์ฃผ์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
์œ„์— ๋ฆฌ๋ทฐ ๋‚ด์šฉ ๋ฐ˜์˜ํ•ด์ฃผ์‹œ๋ฉด ์ด ๋ถ€๋ถ„์— ์ˆ˜์ •์‚ฌํ•ญ์ด ์ƒ๊ธฐ๊ฒ ์ฃ !?
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
`or` ๊ณผ `is` ์ค‘์— ์–ด๋–ค๊ฒŒ ์šฐ์„ ์ˆœ์œ„์ผ๊นŒ์š”? ์กฐ๊ฑด๋ฌธ์—์„œ operator precedence ์— ๋”ฐ๋ผ ์กฐ๊ฑด์ด ์™„์ „ํžˆ ๋ฐ”๋€” ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž˜ ํŒŒ์•…ํ•˜๊ณ  ์‚ฌ์šฉํ•˜์‹œ๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
์ด ๋ถ€๋ถ„๋“ค์—์„œ๋Š” ํŒŒ์ด์ฌ์˜ exception ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Exception handling ์— ๋Œ€ํ•ด์„œ repl.it ์—์„œ ๋ณด์‹ ์ ์ด ์žˆ์œผ์‹ ๋ฐ์š”! ์‹ค์ œ๋กœ view ์—์„œ ์ •๋ง์ •๋ง ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•˜๋Š” ๋งŒํผ ๊ผญ ์ถ”๊ฐ€๋˜์–ด์•ผ ํ•˜๋Š” ๋กœ์ง์ž…๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๋“ค์—์„œ ์–ด๋–ค exception ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์„์ง€ ํ™•์ธํ•ด๋ณด์‹œ๊ณ  ํ•ธ๋“ค๋ง ๋กœ์ง ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
์ด์ œ bcrypt ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ๋กœ์ง ์ถ”๊ฐ€ํ•ด์„œ ์ €์žฅํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
CREATED ๋ฅผ ์˜๋ฏธํ•˜๋Š” 201 ์‚ฌ์šฉ ๋„ˆ๋ฌด ์ž˜ํ•˜์…จ์Šต๋‹ˆ๋‹ค! ๐Ÿ˜‰
@@ -0,0 +1,12 @@ +from django.db import models + +class User(models.Model): + + email = models.EmailField(max_length=50, unique=True) + name = models.CharField(max_length=50, unique=True) + phone = models.CharField(max_length=50, unique=True) + password= models.CharField(max_length=500) + + class Meta: + db_table='users' +
Python
ํœด๋Œ€ํฐ ๋ฒˆํ˜ธ๋„ ๊ณ ์œ ๊ฐ’์ด๋‹ˆ ์œ„์ฒ˜๋Ÿผ ๊ณ ์œ ๊ฐ’์„ ๋œปํ•˜๋Š” ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
string / list ์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ๋ถ€์กฑํ–ˆ์Šต๋‹ˆ๋‹ค. - [x] Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one: - [x] if '.' not in email or '@' not in email:
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
๋ถˆํ•„์š”ํ•œ ์ฃผ์„ ์ œ์™ธํ•˜๊ณ  ์˜ฌ๋ ค์ฃผ์„ธ์š”.
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
else ๋ถˆํ•„์š”
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
์„ฑ๊ณต ์ผ€์ด์Šค์—์„œ ์“ธ ๋กœ์ง์ด๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜๋กœ ์œ„์น˜์‹œ์ผœ์ฃผ์„ธ์š”.
@@ -0,0 +1,145 @@ +""" +Django settings for westagram project. + +Generated by 'django-admin startproject' using Django 3.1.7. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.1/ref/settings/ +""" + +from pathlib import Path +from my_settings import SECRET_KEY, DATABASES + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = SECRET_KEY + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['*'] + + +# Application definition + +INSTALLED_APPS = [ +# 'django.contrib.admin', +# 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'corsheaders', + 'user', + 'posting' +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', +# 'django.middleware.csrf.CsrfViewMiddleware', +# 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'corsheaders.middleware.CorsMiddleware', +] + +ROOT_URLCONF = 'westagram.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'westagram.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.1/ref/settings/#databases + +DATABASES = DATABASES + + +# Password validation +# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.1/howto/static-files/ + +STATIC_URL = '/static/' + +##CORS +CORS_ORIGIN_ALLOW_ALL=True +CORS_ALLOW_CREDENTIALS = True + +CORS_ALLOW_METHODS = ( + 'DELETE', + 'GET', + 'OPTIONS', + 'PATCH', + 'POST', + 'PUT', +) + +CORS_ALLOW_HEADERS = ( + 'accept', + 'accept-encoding', + 'authorization', + 'content-type', + 'dnt', + 'origin', + 'user-agent', + 'x-csrftoken', + 'x-requested-with', +)
Python
์™„๋ฒฝํ•ฉ๋‹ˆ๋‹ค ๊ตญํ˜„๋‹˜! ๐Ÿ’ฏ
@@ -0,0 +1,145 @@ +""" +Django settings for westagram project. + +Generated by 'django-admin startproject' using Django 3.1.7. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.1/ref/settings/ +""" + +from pathlib import Path +from my_settings import SECRET_KEY, DATABASES + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = SECRET_KEY + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['*'] + + +# Application definition + +INSTALLED_APPS = [ +# 'django.contrib.admin', +# 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'corsheaders', + 'user', + 'posting' +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', +# 'django.middleware.csrf.CsrfViewMiddleware', +# 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'corsheaders.middleware.CorsMiddleware', +] + +ROOT_URLCONF = 'westagram.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'westagram.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.1/ref/settings/#databases + +DATABASES = DATABASES + + +# Password validation +# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.1/howto/static-files/ + +STATIC_URL = '/static/' + +##CORS +CORS_ORIGIN_ALLOW_ALL=True +CORS_ALLOW_CREDENTIALS = True + +CORS_ALLOW_METHODS = ( + 'DELETE', + 'GET', + 'OPTIONS', + 'PATCH', + 'POST', + 'PUT', +) + +CORS_ALLOW_HEADERS = ( + 'accept', + 'accept-encoding', + 'authorization', + 'content-type', + 'dnt', + 'origin', + 'user-agent', + 'x-csrftoken', + 'x-requested-with', +)
Python
๋„ต๋„ต ํ™•์ธ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค ์Šนํ˜„๋‹˜ ใ…Žใ…Ž ๐Ÿ˜Š๐Ÿ˜Š ๋ฏธ์…˜2 ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค~~~!!๐Ÿ‘จโ€๐Ÿ’ป
@@ -0,0 +1,12 @@ +from django.db import models + +class User(models.Model): + email = models.EmailField(max_length=50, unique=True) + phone = models.CharField(max_length=11, null=True, unique=True) + full_name = models.CharField(max_length=40, null=True) + user_name = models.CharField(max_length=20, null=True, unique=True) + password = models.CharField(max_length=70) + date_of_birth = models.DateField(null=True) + + class Meta: + db_table = 'users'
Python
๋ถˆํ•„์š”ํ•œ ์ฃผ์„ ์—†์• ์ฃผ์„ธ์š”!
@@ -0,0 +1,12 @@ +from django.db import models + +class User(models.Model): + email = models.EmailField(max_length=50, unique=True) + phone = models.CharField(max_length=11, null=True, unique=True) + full_name = models.CharField(max_length=40, null=True) + user_name = models.CharField(max_length=20, null=True, unique=True) + password = models.CharField(max_length=70) + date_of_birth = models.DateField(null=True) + + class Meta: + db_table = 'users'
Python
์ž‘์„ฑํ•˜์‹  ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํ•„๋“œ๋“ค ์ค‘ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๋ฉด ์•ˆ๋˜๋Š” ํ•„๋“œ๋“ค์ด ๋ณด์ด๋Š”๋ฐ ๊ทธ๋Ÿด๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค! ์ฐพ์•„์„œ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š” ๐Ÿ˜‰
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
๊น”๋”ํ•ฉ๋‹ˆ๋‹ค!! ๐Ÿ’ฏ ๐Ÿ’ฏ ๐Ÿ’ฏ
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
ํ•„์ˆ˜ํ•„๋“œ, `NULL` ํ—ˆ์šฉํ•„๋“œ์— ๋”ฐ๋ฅธ ์ ‘๊ทผ ๋ฐฉ์‹ ๋‹ค๋ฅด๊ฒŒ ์ ์šฉ ์ž˜ํ•˜์…จ์Šต๋‹ˆ๋‹ค!! ๐Ÿ‘๐Ÿ‘๐Ÿ‘ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ์กฐ๊ธˆ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”! ์•„๋ž˜์™€ ๊ฐ™์ด์š”ใ…Žใ…Ž ```suggestion email = data['email'] password = data['password'] phone = data.get('phone', None) full_name = data.get('full_name', None) user_name = data.get('user_name', None) date_of_birth = data.get('date_of_birth', None) ```
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์ž…๋ ฅ๋ฐ›์€ ํฐ๋ฒˆํ˜ธ์— `-` ๊ฐ€ ํฌํ•จ๋˜์–ด ๋“ค์–ด์˜ฌ ์‹œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋„ฃ๊ณ ์‹ถ์€ ํ˜•์‹์— ๋งž์ถฐ์ฃผ์‹œ๋Š” ๋กœ์ง ์ข‹์Šต๋‹ˆ๋‹ค! ์•ž์œผ๋กœ ํ”„๋ก ํŠธ์—”๋“œ๋ถ„๋“ค๊ณผ ํ˜‘์—…์„ ํ•˜๋ฉด์„œ ๊ฒฝํ—˜ํ•˜์‹œ๊ฒ ์ง€๋งŒ, ์ด๋Ÿฐ ๋ถ€๋ถ„์€ ๊ผญ ํ”„๋ก ํŠธ/๋ฐฑ ๊ฐ„์˜ ์ƒ์˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ์  ์ธ์ง€ํ•ด์ฃผ์‹œ๊ณ  ๋‹ค์Œ ์ฃผ ํ”„/๋ฐฑ ํ†ต์‹  ๋“ค์–ด๊ฐ€์‹œ๋ฉด ๋„์›€์ด ๋˜์‹ค๊ฑฐ์—์š”! ใ…Žใ…Ž
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์ •๊ทœํ‘œํ˜„์‹์œผ๋กœ email ๊ณผ password validate ํ•ด์ฃผ์‹œ๋Š” ๋กœ์ง์ธ๋ฐ ์กฐ๊ธˆ ์ž์ž˜ํ•˜๊ฒŒ ๋‚˜๋ˆ„์–ด์„œ ๋น„๊ตํ•˜์‹œ๋ ค๋‹ค๋ณด๋‹ˆ ์กฐ๊ธˆ ๋ณต์žกํ•ด๋ณด์ด๋„ค์š”! ์ฐจ๋ผ๋ฆฌ ์ด๋ฉ”์ผ ํ˜•์‹์— ๋งž๋Š” ์ •๊ทœํ‘œํ˜„์‹์„ ํ†ต์งธ๋กœ ๊ฐ€์ ธ์™€ ์ €์žฅํ•ด๋‘๊ณ  `re.match()` ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ•œ๋ฒˆ์— ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `password` ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๊ตฌ์š”! ๊ทธ๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์‹œ๋ฉด ์กฐ๊ธˆ ๋” ๊น”๋”ํ•œ ์กฐ๊ฑด๋ฌธ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค ๐Ÿ˜‰
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์ •๋ณด์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋กœ์ง์„ ์ž‘์„ฑํ•ด์ฃผ์…จ๋Š”๋ฐ ์ž‘์„ฑํ•ด์ฃผ์‹  ๋กœ์ง์„ ํ๋ฆ„๋Œ€๋กœ ๊ธ€๋กœ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ์ž…๋ ฅ๋ฐ›์€ `email` ๊ฐ’์œผ๋กœ ์œ ์ € ํ…Œ์ด๋ธ”์— filter ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค ๋ฐ˜ํ™˜๋ฐ›์€ QuerySet ๋ฆฌ์ŠคํŠธ์— ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด `phone_number` ๊ณผ `user_name` ์œผ๋กœ filter ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค ์ถ”๊ฐ€ ํ™•์ธ์„ ํ•œ๋‹ค. 2. ์กด์žฌํ•œ๋‹ค๋ฉด `400` ์„ ๋ฆฌํ„ดํ•ด์ค€๋‹ค. ํ•˜์ง€๋งŒ ํ˜„์žฌ `SignUp` View ์—์„œ๋Š” `email` ์„ ํ•„์ˆ˜๊ฐ’์œผ๋กœ ๋ฐ›์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์‹ค์ƒ `email` ์กด์žฌ ์œ ๋ฌด๋งŒ ํ™•์ธํ•ด๋„ ๋“ฑ๋ก๋œ ์œ ์ €์ธ์ง€ ์•„๋‹Œ์ง€๋Š” ์•Œ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. `phone_number` ์™€ `user_name` ์€ ๋ถ€๊ฐ€์ •๋ณด๋“ค์ด๋‹ˆ๊นŒ์š”! ๊ทธ๋Ÿฐ๋ฐ `email` ๋กœ ์กด์žฌ์œ ๋ฌด๋Š” ํ™•์ธํ•˜๋”๋ผ๋„ `phone_number` ์™€ `user_name` ์˜ ์ค‘๋ณต ๊ฐ€๋Šฅ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์ถ”๊ฐ€ ๋กœ์ง์€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ๋ชจ๋ธ์ชฝ์— ๋‚จ๊ฒจ๋“œ๋ฆฐ ๋ฆฌ๋ทฐ๋ฅผ ํ™•์ธํ•ด๋ณด์‹  ํ›„ ์ƒ๊ฐํ•˜์‹  ๊ตฌ์กฐ์— ๋”ฐ๋ผ ์ˆ˜์ •ํ•ด์ฃผ์‹  ๋‹ค์Œ ๋‹ค์‹œํ•œ๋ฒˆ ์ด์ชฝ ๋ถ€๋ถ„์„ ํ™•์ธํ•ด๋ณด์„ธ์š”!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
๊น”๋”ํ•ฉ๋‹ˆ๋‹ค~ ๐Ÿ’ฏ
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
๊ตฌ๋ถ„์„ ์œ„ํ•ด ์œ„์•„๋ž˜๋กœ ํ•œ์ค„์”ฉ ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹์•„์ง€๊ฒ ๋„ค์š”! ๐Ÿ˜Ž
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์ด๋Ÿฐ์‹์œผ๋กœ exception ์„ ์ฒ˜๋ฆฌํ•˜์‹œ๊ฒŒ ๋˜๋ฉด ์–ด๋–ค exception ์ด ๋ฌด์—‡๋•Œ๋ฌธ์— `raise` ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ exception ํ•ธ๋“ค๋Ÿฌ ์ถ”๊ฐ€ํ•ด์ฃผ์‹ค๋•Œ๋Š” ์œ„์—์„œ `KeyError` ์ฒ˜๋ฆฌํ•ด์ฃผ์‹ ๊ฒƒ๊ณผ ๊ฐ™์ด ๋ช…ํ™•ํ•˜๊ฒŒ ์ง€์ •ํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ด์ฃผ์…”์•ผํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ง€๊ธˆ ๋‹จ๊ณ„์—์„œ ์–ด๋–ค exception ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋ชจ๋ฅด๋Š”๊ฒŒ ๋‹น์—ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿด๋•Œ๋Š” ๊ทธ๋ƒฅ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ˜ธ์ถœํ•ด๋ณด๋ฉด์„œ ๊ฒฝํ—˜ํ•˜์—ฌ ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๋‹ค๋ณด๋ฉด ๋‚˜์ค‘์—๋Š” ๋ฉ”์†Œ๋“œ ํ•˜๋‚˜๋ฅผ ์ถ”๊ฐ€ํ• ๋•Œ ๊ด€๋ จํ•œ exception ์„ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ๋กœ์ง์„ ๋ฐ”๋กœ ํ•จ๊ป˜ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์‹ค๊ฑฐ์—์š”! ๐Ÿ˜
@@ -0,0 +1,12 @@ +from django.db import models + +class User(models.Model): + email = models.EmailField(max_length=50, unique=True) + phone = models.CharField(max_length=11, null=True, unique=True) + full_name = models.CharField(max_length=40, null=True) + user_name = models.CharField(max_length=20, null=True, unique=True) + password = models.CharField(max_length=70) + date_of_birth = models.DateField(null=True) + + class Meta: + db_table = 'users'
Python
๋„ค๋„ต!!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์•„ํ•˜ ๋„ค๋„ต!! ๊ถ๊ธˆํ•œ ์  ํ•ด๊ฒฐ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž!!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์Šนํ˜„๋‹˜, ๋ง์”€ํ•˜์‹ ๋Œ€๋กœ, ์ด๋ฉ”์ผ ์ฃผ์†Œ์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ •๊ทœ์‹ ๊ฒ€์‚ฌ์— ํ•„์š”ํ•œ ๊ฐ’์„ REGEX_EMAIL, REGEX_PASSWORD์—๋‹ค๊ฐ€ ์ €์žฅํ•˜์—ฌ ํ™œ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค..! ๋ณด๋‹ค ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ ธ์„œ ์•„์ฃผ ์ข‹๋„ค์š” ใ…Žใ…Žใ…Ž
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์Šนํ˜„๋‹˜, ์•„๊นŒ ํ•ด์ฃผ์‹  ๋ง์”€ ๋“ฃ๊ณ  User ํด๋ž˜์Šค์— ํŠน์ • ์ปฌ๋Ÿผ์€ ๊ณ ์œ ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๋„๋ก unique ์˜ต์…˜์„ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค..! ๊ทธ๋ฆฌ๊ณ  ๋ง์”€ํ•ด์ฃผ์‹  q ํด๋ž˜์Šค๋„ ๊ณต๋ถ€ํ•˜๊ณ  ๋‹ค์Œ ์†Œ์Šค์—๋„ ์ ์šฉํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
Truthy, Falsy values ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์‹œ๊ณ  ํ•ด๋‹น ์กฐ๊ฑด๋ฌธ๋“ค์„ ์–ด๋–ป๊ฒŒ ๋” ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์„์ง€ ๊ณ ๋ฏผํ•ด๋ณด์‹œ๊ณ  ์ ์šฉํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์ด์ œ bcrypt ๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•”ํ˜ธํ™”ํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ๋กœ์ง์„ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์—ฌ๊ธฐ์„œ๋„ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” exception ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค! exception handling ๋กœ์ง์— ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”!!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
`=` ๊ธฐ์ค€ ๊ฐ„๊ฒฉ ์ •๋ ฌํ•ด์ฃผ์„ธ์š” ๊ตญํ˜„๋‹˜!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
- `|` ์‚ฌ์ด ๊ฐ„๊ฒฉ๋„ ํ†ต์ผํ•ด์ฃผ์„ธ์š”~ - ์ด ๋ถ€๋ถ„ ๋กœ์ง์ด ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๋˜๋Š”๋ฐ ์ „๋ถ€ user_id ๋ฅผ ๋Œ€์ž…ํ•˜์—ฌ ํ™•์ธํ•˜๊ณ  ์‹ถ์€๊ฒŒ ๋งž์œผ์‹ ๊ฐ€์š”?!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
๋„ต๋„ต!! ์ˆ˜์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค~~
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
๋„ต ์Šนํ˜„๋‹˜! ์ €๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์ธํ•  ๋•Œ ์ด๋ฉ”์ผ, ํœด๋Œ€ํฐ, ์œ ์ €๋„ค์ž„ ์ด ์„ธ๊ฐœ์ค‘์— ํ•˜๋‚˜๋กœ ๋กœ๊ทธ์ธํ• ๊บผ๋ผ๊ณ  ์ƒ๊ฐ์„ ํ•ด์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ์•„์˜ˆ user_id๋กœ ๊ฐ’์„ ๋ฐ›์œผ๋ ค ํ–ˆ์Šต๋‹ˆ๋‹ค..! ๊ทธ๋ž˜์„œ ์ด user_id๊ฐ’์„ ๊ฐ€์ง€๊ณ  ํ•˜๋‚˜๋ผ๋„ ์ด๋ฉ”์ผ, ํœด๋Œ€ํฐ, ์œ ์ €๋„ค์ž„๊ณผ ๊ฐ™์€๊ฒŒ ์žˆ์œผ๋ฉด ์œ ํšจํ•œ ์•„์ด๋””๊ฐ’์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค..!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์Šนํ˜„๋‹˜ ํ˜น์‹œ json ๊ด€๋ จ exception ์ด JSONDecodeError ์ธ๊ฒƒ์ผ๊นŒ์š”..?
@@ -1,26 +1,39 @@ package nextstep.app; +import jakarta.servlet.http.HttpServletRequest; +import java.util.ArrayList; import nextstep.app.domain.Member; import nextstep.app.domain.MemberRepository; import nextstep.security.authentication.AuthenticationException; import nextstep.security.authentication.BasicAuthenticationFilter; import nextstep.security.authentication.UsernamePasswordAuthenticationFilter; -import nextstep.security.authorization.CheckAuthenticationFilter; -import nextstep.security.authorization.SecuredAspect; +import nextstep.security.authorization.AuthorizationFilter; +import nextstep.security.authorization.AuthorizationManager; import nextstep.security.authorization.SecuredMethodInterceptor; +import nextstep.security.authorization.method.SecuredAuthorizationManager; +import nextstep.security.authorization.web.AuthenticatedAuthorizationManager; +import nextstep.security.authorization.web.AuthorityAuthorizationManager; +import nextstep.security.authorization.web.DenyAllAuthorizationManager; +import nextstep.security.authorization.web.RequestMatcherDelegatingAuthorizationManager; import nextstep.security.config.DefaultSecurityFilterChain; import nextstep.security.config.DelegatingFilterProxy; import nextstep.security.config.FilterChainProxy; import nextstep.security.config.SecurityFilterChain; import nextstep.security.context.SecurityContextHolderFilter; import nextstep.security.userdetails.UserDetails; import nextstep.security.userdetails.UserDetailsService; +import nextstep.security.util.AnyRequestMatcher; +import nextstep.security.util.MvcRequestMatcher; +import nextstep.security.util.RequestMatcherEntry; +import nextstep.security.authorization.web.PermitAllAuthorizationManager; +import org.aopalliance.intercept.MethodInvocation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; import java.util.List; import java.util.Set; +import org.springframework.http.HttpMethod; @EnableAspectJAutoProxy @Configuration @@ -44,12 +57,26 @@ public FilterChainProxy filterChainProxy(List<SecurityFilterChain> securityFilte @Bean public SecuredMethodInterceptor securedMethodInterceptor() { - return new SecuredMethodInterceptor(); + return new SecuredMethodInterceptor(securedAuthorizationManager()); + } + + @Bean + public AuthorizationManager<MethodInvocation> securedAuthorizationManager() { + return new SecuredAuthorizationManager(); + } + + @Bean + public AuthorizationManager<HttpServletRequest> requestAuthorizationManager() { + List<RequestMatcherEntry<AuthorizationManager>> mappings = new ArrayList<>(); + mappings.add(new RequestMatcherEntry<>(new MvcRequestMatcher(HttpMethod.GET, "/members"), + new AuthorityAuthorizationManager<HttpServletRequest>(Set.of("ADMIN")))); + mappings.add(new RequestMatcherEntry<>(new MvcRequestMatcher(HttpMethod.GET, "/members/me"), + new AuthenticatedAuthorizationManager())); + mappings.add(new RequestMatcherEntry<>(new MvcRequestMatcher(HttpMethod.GET, "/search"), + new PermitAllAuthorizationManager())); + mappings.add(new RequestMatcherEntry<>(AnyRequestMatcher.INSTANCE, new DenyAllAuthorizationManager())); + return new RequestMatcherDelegatingAuthorizationManager(mappings); } -// @Bean -// public SecuredAspect securedAspect() { -// return new SecuredAspect(); -// } @Bean public SecurityFilterChain securityFilterChain() { @@ -58,7 +85,7 @@ public SecurityFilterChain securityFilterChain() { new SecurityContextHolderFilter(), new UsernamePasswordAuthenticationFilter(userDetailsService()), new BasicAuthenticationFilter(userDetailsService()), - new CheckAuthenticationFilter() + new AuthorizationFilter(requestAuthorizationManager()) ) ); }
Java
์ž˜ ์ถ”๊ฐ€ํ•ด์ฃผ์…จ๋„ค์š” ๐Ÿ‘
@@ -2,7 +2,10 @@ import nextstep.app.domain.Member; import nextstep.app.domain.MemberRepository; +import nextstep.security.authentication.Authentication; +import nextstep.security.authentication.AuthenticationException; import nextstep.security.authorization.Secured; +import nextstep.security.context.SecurityContextHolder; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @@ -30,4 +33,15 @@ public ResponseEntity<List<Member>> search() { List<Member> members = memberRepository.findAll(); return ResponseEntity.ok(members); } + + @GetMapping("/members/me") + public ResponseEntity<Member> me() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + + String email = authentication.getPrincipal().toString(); + Member member = memberRepository.findByEmail(email) + .orElseThrow(RuntimeException::new); + + return ResponseEntity.ok(member); + } }
Java
์ด๋ฏธ Filter์—์„œ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋Š”๋ฐ ๋ถˆํ•„์š”ํ•œ ๋กœ์ง์ด ์•„๋‹๊นŒ์š”?
@@ -0,0 +1,20 @@ +package nextstep.security.util; + +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.http.HttpMethod; + +public class MvcRequestMatcher implements RequestMatcher { + + private final HttpMethod method; + private final String pattern; + + public MvcRequestMatcher(HttpMethod method, String pattern) { + this.method = method; + this.pattern = pattern; + } + + @Override + public boolean matches(HttpServletRequest request) { + return this.method.equals(HttpMethod.valueOf(request.getMethod())) && pattern.equals(request.getRequestURI()); + } +}
Java
```suggestion return this.method.equals(HttpMethod.valueOf(request.getMethod())) && pattern.equals(request.getRequestURI()); ``` null-safe ๊ด€์ ์—์„œ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋“ค์–ด์˜ค๋Š” `request.getRequestURI()`๋Š” null์ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„๋กœ null ์ฒดํฌ๋ฅผ ํ•˜์ง€ ์•Š๋Š” ์ด์ƒ ์œ„์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด npe๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ์•ˆ์ „ํ•œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,14 @@ +package nextstep.security.authorization; + +import nextstep.security.authentication.Authentication; +import nextstep.security.authorization.web.AuthorizationResult; + +@FunctionalInterface +public interface AuthorizationManager<T> { + @Deprecated + AuthorizationDecision check(Authentication authentication, T object); + + default AuthorizationResult authorize(Authentication authentication, T object) { + return check(authentication, object); + } +}
Java
ํ˜„์žฌ spring security๋ฅผ ํ™•์ธํ•ด๋ณด์‹œ๋ฉด `check`๋Š” deprecated๋˜์—ˆ๋Š”๋ฐ์š”. `AuthorizationDecision`์ด๋ผ๋Š” ํด๋ž˜์Šค๋Š” ๊ตฌํ˜„ํ•ด์ฃผ์‹  ๊ฒƒ์ฒ˜๋Ÿผ ๊ตฌํ˜„์ฒด๋กœ ๋˜์–ด์žˆ๊ณ , ๋ณดํ†ต์˜ ํ”„๋ ˆ์ž„์›Œํฌ๋“ค์€ ๊ทœ๋ชจ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ๋งŒ๋“ค์–ด๋‘” ๊ตฌํ˜„์ฒด๋“ค์„ ์ถ”์ƒํ™”ํ•˜๋Š” ํ˜•ํƒœ๋กœ ๊ฐœ์„ ํ•ด๋‚˜๊ฐ‘๋‹ˆ๋‹ค. `check`๊ฐ€ deprecated๋จ์— ๋”ฐ๋ผ ํ•ด๋‹น ๊ธฐ๋Šฅ์ด ๋ง‰ํžŒ ๊ฒƒ์€ ์•„๋‹ˆ๊ณ  ์ด๋ฅผ ์ถ”์ƒํ™”ํ•œ `AuthorizationResult`๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” :) https://github.com/franticticktick/spring-security/blob/main/core/src/main/java/org/springframework/security/authorization/AuthorizationManager.java https://github.com/spring-projects/spring-security/pull/14712 https://github.com/spring-projects/spring-security/pull/14846
@@ -0,0 +1,14 @@ +package nextstep.security.authorization; + +import nextstep.security.authentication.Authentication; +import nextstep.security.authorization.web.AuthorizationResult; + +@FunctionalInterface +public interface AuthorizationManager<T> { + @Deprecated + AuthorizationDecision check(Authentication authentication, T object); + + default AuthorizationResult authorize(Authentication authentication, T object) { + return check(authentication, object); + } +}
Java
์ค€ํ˜•๋‹˜์ด ์ƒ๊ฐํ•˜์‹œ๊ธฐ์— ์„ ํƒ์‚ฌํ•ญ์œผ๋กœ ์ฃผ์–ด์ง„ `verfiy`๋Š” `check`์™€ ๋น„๊ตํ•˜์—ฌ ์–ด๋–ค ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์œผ์‹ ๊ฐ€์š”?
@@ -0,0 +1,22 @@ +package nextstep.security.authorization; + +import nextstep.security.authorization.web.AuthorizationResult; + +public class AuthorizationDecision implements AuthorizationResult { + public static final AuthorizationDecision ALLOW = new AuthorizationDecision(true); + public static final AuthorizationDecision DENY = new AuthorizationDecision(false); + + private final boolean granted; + + public AuthorizationDecision(boolean granted) { + this.granted = granted; + } + + public boolean isGranted() { + return granted; + } + + public static AuthorizationDecision of(boolean granted) { + return granted ? ALLOW : DENY; + } +}
Java
true ํ˜น์€ false๋งŒ ๊ฐ€์ง€๋Š” `AuthorizationDecision`์ด ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š”๋ฐ ์‚ฌ์šฉ๋ ๋•Œ๋งˆ๋‹ค ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ธฐ๋ณด๋‹ค๋Š” ๋ถˆ๋ณ€์ž„์„ ํ™œ์šฉํ•˜์—ฌ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด์ค€ ์ƒ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์œ ๋„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”.
@@ -0,0 +1,38 @@ +package nextstep.security.authorization.web; + +import java.util.Collection; +import nextstep.security.authentication.Authentication; +import nextstep.security.authentication.AuthenticationException; +import nextstep.security.authorization.AuthorizationDecision; +import nextstep.security.authorization.AuthorizationManager; + +public class AuthorityAuthorizationManager<T> implements AuthorizationManager<T> { + + private final Collection<String> authorities; + + public AuthorityAuthorizationManager(Collection<String> authorities) { + this.authorities = authorities; + } + + + @Override + public AuthorizationDecision check(Authentication authentication, T object) { + if (authentication == null) { + throw new AuthenticationException(); + } + + boolean hasAuthority = isAuthorized(authentication, authorities); + + return AuthorizationDecision.of(hasAuthority); + } + + + private boolean isAuthorized(Authentication authentication, Collection<String> authorities) { + for (String authority : authentication.getAuthorities()) { + if (authorities.contains(authority)) { + return true; + } + } + return false; + } +}
Java
์‹ค์ œ spring security์—์„œ๋Š” ์„ฑ๋Šฅ์˜ ๋ฌธ์ œ๋กœ ์ธํ•ด stream ์‚ฌ์šฉ์„ ์ œํ•œํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. https://github.com/spring-projects/spring-security/issues/7154
@@ -0,0 +1,28 @@ +package nextstep.security.authorization.web; + +import jakarta.servlet.http.HttpServletRequest; +import java.util.List; +import nextstep.security.authentication.Authentication; +import nextstep.security.authorization.AuthorizationDecision; +import nextstep.security.authorization.AuthorizationManager; +import nextstep.security.util.RequestMatcher; +import nextstep.security.util.RequestMatcherEntry; + +public class RequestMatcherDelegatingAuthorizationManager implements AuthorizationManager<HttpServletRequest> { + + private final List<RequestMatcherEntry<AuthorizationManager>> mappings; + + public RequestMatcherDelegatingAuthorizationManager(List<RequestMatcherEntry<AuthorizationManager>> mappings) { + this.mappings = mappings; + } + + @Override + public AuthorizationDecision check(Authentication authentication, HttpServletRequest request) { + for (RequestMatcherEntry<AuthorizationManager> mapping : mappings) { + if (mapping.getRequestMatcher().matches(request)) { + return mapping.getEntry().check(authentication, request); + } + } + return AuthorizationDecision.DENY; + } +}
Java
getter๋กœ ํ˜ธ์ถœํ•˜์—ฌ ์ผ์น˜ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” `mapping`์—์„œ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์ˆ˜์ •ํ•˜๋ฉด ์ฑ…์ž„์ด ๋ช…ํ™•ํ•˜๊ฒŒ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ฒ ๋„ค์š” :)
@@ -0,0 +1,43 @@ +package nextstep.security.authorization.method; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.Collections; +import java.util.Set; +import nextstep.security.authentication.Authentication; +import nextstep.security.authorization.AuthorizationDecision; +import nextstep.security.authorization.AuthorizationManager; +import nextstep.security.authorization.Secured; +import nextstep.security.authorization.web.AuthorityAuthorizationManager; +import org.aopalliance.intercept.MethodInvocation; + +public class SecuredAuthorizationManager implements AuthorizationManager<MethodInvocation> { + + private AuthorityAuthorizationManager<Collection<String>> authorityAuthorizationManager; + + public void setAuthorityAuthorizationManager(Collection<String> authorities) { + authorityAuthorizationManager = new AuthorityAuthorizationManager<>(authorities); + } + + @Override + public AuthorizationDecision check(Authentication authentication, MethodInvocation invocation) { + Collection<String> authorities = getAuthorities(invocation); + + if (authorities.isEmpty()) { + return null; + } + setAuthorityAuthorizationManager(authorities); + return authorities.isEmpty() ? null : authorityAuthorizationManager.check(authentication, authorities); + } + + private Collection<String> getAuthorities(MethodInvocation invocation) { + Method method = invocation.getMethod(); + + if (!method.isAnnotationPresent(Secured.class)) { + return Collections.emptySet(); + } + + Secured secured = method.getAnnotation(Secured.class); + return Set.of(secured.value()); + } +}
Java
`AuthorityAuthorizationManager`์˜ ๋กœ์ง๊ณผ ํ•จ๊ป˜๋ณด๋ฉด ```java boolean hasAuthority = authentication.getAuthorities().stream() .anyMatch(authorities::contains); return new AuthorizationDecision(hasAuthority); ``` ๋ถ€๋ถ„์ด ๋™์ผํ•œ ๊ฒƒ์„ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ์–ด์š”. ๊ฐ `AuthorizatinManager`๋Š” ๋‹จ์ผ์—์„œ ๊ฐ๊ฐ ๋ณธ์ธ์˜ ๊ฒƒ์„ ๋ชจ๋‘ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์„œ๋กœ ์œ ๊ธฐ์ ์œผ๋กœ ๊ฒฐํ•ฉ๋˜์–ด ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•˜๋Š”๋ฐ์š”. ์ฆ‰, `SecuredAuthorizationManager`๋Š” `@Secured`์— ์žˆ๋Š” ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์ฒ˜๋ฆฌํ•˜๋ฉฐ, authority์— ๋Œ€ํ•œ ๊ถŒํ•œ์ฒดํฌ๋Š” `AuthorizatinManager`๊ฐ€ ์˜จ์ „ํžˆ ๋‹ด๋‹นํ•˜๋Š”๊ฑฐ์ฃ . https://github.com/spring-projects/spring-security/blob/main/core/src/main/java/org/springframework/security/authorization/method/SecuredAuthorizationManager.java ์‹ค์ œ๋กœ๋Š” ๊ถŒํ•œ์ฒด๊ณ„๊ฐ€ ๋‹ค์–‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— `AuthoritiesAuthorizationManager`๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ๋Š” ํ•˜์ง€๋งŒ ๊ด€๋ จํ•˜์—ฌ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ์ดํ•ด์— ๋„์›€์ด ๋˜์‹ค ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,18 @@ +package nextstep.security.fixture; + +import java.util.Base64; +import java.util.Set; +import nextstep.app.domain.Member; + +public class MemberTestFixture { + public static final Member TEST_ADMIN_MEMBER = new Member("[email protected]", "password", "a", "", Set.of("ADMIN")); + public static final Member TEST_USER_MEMBER = new Member("[email protected]", "password", "b", "", Set.of("USER")); + + public static String createAdminToken(){ + return Base64.getEncoder().encodeToString((TEST_ADMIN_MEMBER.getEmail() + ":" + TEST_ADMIN_MEMBER.getPassword()).getBytes()); + } + + public static String createMemberToken(){ + return Base64.getEncoder().encodeToString((TEST_USER_MEMBER.getEmail() + ":" + TEST_USER_MEMBER.getPassword()).getBytes()); + } +}
Java
(๋ฐ˜์˜ํ•˜์ง€ ์•Š์œผ์…”๋„ ๋ฉ๋‹ˆ๋‹ค.) ๊ฐœ์ธ์ ์œผ๋กœ๋Š” enum์—์„œ ์ง€์›๋˜๋Š” ๋ฉ”์†Œ๋“œ๋“ค์„ ํ™œ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ์•„ ํŠน์ • ๋„๋ฉ”์ธ์˜ fixture๋Š” enum ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค ๐Ÿ˜„
@@ -2,7 +2,10 @@ import nextstep.app.domain.Member; import nextstep.app.domain.MemberRepository; +import nextstep.security.authentication.Authentication; +import nextstep.security.authentication.AuthenticationException; import nextstep.security.authorization.Secured; +import nextstep.security.context.SecurityContextHolder; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @@ -30,4 +33,15 @@ public ResponseEntity<List<Member>> search() { List<Member> members = memberRepository.findAll(); return ResponseEntity.ok(members); } + + @GetMapping("/members/me") + public ResponseEntity<Member> me() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + + String email = authentication.getPrincipal().toString(); + Member member = memberRepository.findByEmail(email) + .orElseThrow(RuntimeException::new); + + return ResponseEntity.ok(member); + } }
Java
์•— ํ•ด๋‹น appํŒจํ‚ค์ง€์— ์ปจํŠธ๋กค๋Ÿฌ๋Š” ๊ฐ•์˜์‹ค์—์„œ ์‹ค์Šต์ดํ›„ ์ฒด๋ฆฌํ”ฝ ํ•ด์˜จ๊ฒƒ์ด๋ผ ์—ด์–ด๋ณผ ์ƒ๊ฐ์„ ๋ชปํ–ˆ๋„ค์š”. ์ˆ˜์ •ํ•ด๋‘๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,20 @@ +package nextstep.security.util; + +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.http.HttpMethod; + +public class MvcRequestMatcher implements RequestMatcher { + + private final HttpMethod method; + private final String pattern; + + public MvcRequestMatcher(HttpMethod method, String pattern) { + this.method = method; + this.pattern = pattern; + } + + @Override + public boolean matches(HttpServletRequest request) { + return this.method.equals(HttpMethod.valueOf(request.getMethod())) && pattern.equals(request.getRequestURI()); + } +}
Java
๊ฐ„๊ณผํ•˜๊ณ  ์žˆ์—ˆ๋„ค์š”. ์ˆ˜์ •ํ•ด๋‘๊ฒ ์Šต๋‹ˆ๋‹ค! ๐Ÿ‘
@@ -0,0 +1,14 @@ +package nextstep.security.authorization; + +import nextstep.security.authentication.Authentication; +import nextstep.security.authorization.web.AuthorizationResult; + +@FunctionalInterface +public interface AuthorizationManager<T> { + @Deprecated + AuthorizationDecision check(Authentication authentication, T object); + + default AuthorizationResult authorize(Authentication authentication, T object) { + return check(authentication, object); + } +}
Java
์˜ค.. ๋ถˆ๊ณผ 4๋‹ฌ์ „์— ์—…๋ฐ์ดํŠธ๋œ ๊ธฐ๋Šฅ์ด๊ตฐ์š”! ํ”ผ๋“œ๋ฐฑ ์ฃผ์‹  ๋Œ€๋กœ ๋ฐ˜์˜ํ•ด๋ณด๋ฉด์„œ ์–ด๋–ค ์‹์œผ๋กœ ์˜คํ”ˆ์†Œ์Šค๊ฐ€ ๊ฐœ์„ ๋˜์–ด ๋‚˜๊ฐ€๋Š”์ง€ ์ฒดํ—˜ ํ•ด ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋„ค์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,22 @@ +package nextstep.security.authorization; + +import nextstep.security.authorization.web.AuthorizationResult; + +public class AuthorizationDecision implements AuthorizationResult { + public static final AuthorizationDecision ALLOW = new AuthorizationDecision(true); + public static final AuthorizationDecision DENY = new AuthorizationDecision(false); + + private final boolean granted; + + public AuthorizationDecision(boolean granted) { + this.granted = granted; + } + + public boolean isGranted() { + return granted; + } + + public static AuthorizationDecision of(boolean granted) { + return granted ? ALLOW : DENY; + } +}
Java
ํ˜„ ์ƒํ™ฉ์—์„œ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ์„ ๋” ๋†’์ผ ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”!
@@ -0,0 +1,38 @@ +package nextstep.security.authorization.web; + +import java.util.Collection; +import nextstep.security.authentication.Authentication; +import nextstep.security.authentication.AuthenticationException; +import nextstep.security.authorization.AuthorizationDecision; +import nextstep.security.authorization.AuthorizationManager; + +public class AuthorityAuthorizationManager<T> implements AuthorizationManager<T> { + + private final Collection<String> authorities; + + public AuthorityAuthorizationManager(Collection<String> authorities) { + this.authorities = authorities; + } + + + @Override + public AuthorizationDecision check(Authentication authentication, T object) { + if (authentication == null) { + throw new AuthenticationException(); + } + + boolean hasAuthority = isAuthorized(authentication, authorities); + + return AuthorizationDecision.of(hasAuthority); + } + + + private boolean isAuthorized(Authentication authentication, Collection<String> authorities) { + for (String authority : authentication.getAuthorities()) { + if (authorities.contains(authority)) { + return true; + } + } + return false; + } +}
Java
๊ฐ€๋…์„ฑ ๋•Œ๋ฌธ์— stream์„ ์„ ํ˜ธํ•˜๋Š” ํŽธ์ธ๋ฐ, spring security๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์„ฑ๋Šฅ์ด ์ค‘์š”ํ•˜๋‹ค๋ณด๋‹ˆ ๊ทธ๋ ‡๊ฒŒ ๋œ๊ฑฐ๊ตฐ์š”.. ๊ตฌํ˜„์ฒด ์ฝ”๋“œ ๋ณด๋ฉด์„œ ๊ฐ€๋…์„ฑ์ด ์ƒ๊ฐ๋งŒํผ์€ ์•„๋‹Œ๊ฒƒ ๊ฐ™์•˜๋Š”๋ฐ, ๊ทธ ์œ„ํ™”๊ฐ์ด ์—ฌ๊ธฐ์„œ ๊ธฐ์ธํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,28 @@ +package nextstep.security.authorization.web; + +import jakarta.servlet.http.HttpServletRequest; +import java.util.List; +import nextstep.security.authentication.Authentication; +import nextstep.security.authorization.AuthorizationDecision; +import nextstep.security.authorization.AuthorizationManager; +import nextstep.security.util.RequestMatcher; +import nextstep.security.util.RequestMatcherEntry; + +public class RequestMatcherDelegatingAuthorizationManager implements AuthorizationManager<HttpServletRequest> { + + private final List<RequestMatcherEntry<AuthorizationManager>> mappings; + + public RequestMatcherDelegatingAuthorizationManager(List<RequestMatcherEntry<AuthorizationManager>> mappings) { + this.mappings = mappings; + } + + @Override + public AuthorizationDecision check(Authentication authentication, HttpServletRequest request) { + for (RequestMatcherEntry<AuthorizationManager> mapping : mappings) { + if (mapping.getRequestMatcher().matches(request)) { + return mapping.getEntry().check(authentication, request); + } + } + return AuthorizationDecision.DENY; + } +}
Java
์Œ.. ์ œ ์ƒ๊ฐ์œผ๋กœ๋Š” `mapping`์ธ `RequestMatcherEntry`๋Š” `RequestMatcher`๋ž‘ `AuthorizationManager`๋ฅผ ๋‹จ์ˆœํžˆ ๋ฌถ์–ด์ฃผ๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ, ํ•ด๋‹น ๊ฐ์ฒด์— `matches()`์˜ ๋กœ์ง๊นŒ์ง€ ์žˆ์œผ๋ฉด ์˜คํžˆ๋ ค ์ฑ…์ž„์ด ๊ณผํ•ด์ง€๊ณ , ์›๋ž˜ RequestMatcher์˜ ์—ญํ• ์ด ํ๋ ค์งˆ๊ฒƒ ๊ฐ™์•„ getter๋กœ ํ˜ธ์ถœํ•ด์„œ ์ผ์น˜ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์ œ๋Œ€๋กœ ์ดํ•ดํ•œ๊ฑฐ๋ผ๋ฉด ์ง„์˜๋‹˜ ์ƒ๊ฐ์ด ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,18 @@ +package nextstep.security.fixture; + +import java.util.Base64; +import java.util.Set; +import nextstep.app.domain.Member; + +public class MemberTestFixture { + public static final Member TEST_ADMIN_MEMBER = new Member("[email protected]", "password", "a", "", Set.of("ADMIN")); + public static final Member TEST_USER_MEMBER = new Member("[email protected]", "password", "b", "", Set.of("USER")); + + public static String createAdminToken(){ + return Base64.getEncoder().encodeToString((TEST_ADMIN_MEMBER.getEmail() + ":" + TEST_ADMIN_MEMBER.getPassword()).getBytes()); + } + + public static String createMemberToken(){ + return Base64.getEncoder().encodeToString((TEST_USER_MEMBER.getEmail() + ":" + TEST_USER_MEMBER.getPassword()).getBytes()); + } +}
Java
3๋‹จ๊ณ„ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ๋ณ€๊ฒฝํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ๐Ÿ˜„
@@ -0,0 +1,43 @@ +package nextstep.security.authorization.method; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.Collections; +import java.util.Set; +import nextstep.security.authentication.Authentication; +import nextstep.security.authorization.AuthorizationDecision; +import nextstep.security.authorization.AuthorizationManager; +import nextstep.security.authorization.Secured; +import nextstep.security.authorization.web.AuthorityAuthorizationManager; +import org.aopalliance.intercept.MethodInvocation; + +public class SecuredAuthorizationManager implements AuthorizationManager<MethodInvocation> { + + private AuthorityAuthorizationManager<Collection<String>> authorityAuthorizationManager; + + public void setAuthorityAuthorizationManager(Collection<String> authorities) { + authorityAuthorizationManager = new AuthorityAuthorizationManager<>(authorities); + } + + @Override + public AuthorizationDecision check(Authentication authentication, MethodInvocation invocation) { + Collection<String> authorities = getAuthorities(invocation); + + if (authorities.isEmpty()) { + return null; + } + setAuthorityAuthorizationManager(authorities); + return authorities.isEmpty() ? null : authorityAuthorizationManager.check(authentication, authorities); + } + + private Collection<String> getAuthorities(MethodInvocation invocation) { + Method method = invocation.getMethod(); + + if (!method.isAnnotationPresent(Secured.class)) { + return Collections.emptySet(); + } + + Secured secured = method.getAnnotation(Secured.class); + return Set.of(secured.value()); + } +}
Java
[83a4efd](https://github.com/next-step/spring-security-authorization/pull/15/commits/83a4efd95662b7e9d2590f72ee5a56173a16477d) ์œผ๋กœ ๋ฐ˜์˜ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค! ๊ฐ์ž์˜ Manager๊ฐ€ ๋‹จ์ผ ์ฑ…์ž„์ด ์•„๋‹Œ ๊ฒฐํ•ฉํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•˜๋Š”๊ตฐ์š”! ์ €๋„ ์ž‘์—…ํ•˜๋ฉด์„œ ๊ฐ™์€ ๋กœ์ง์ด ๋“ค์–ด๊ฐ„๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ๋Š”๋ฐ, ์ด๋Ÿฐ์‹์œผ๋กœ๋„ ์—ญํ• ๊ณผ ์ฑ…์ž„ ๋ถ„๋ฐฐ๋ฅผ ๊ฐ€์ ธ๊ฐˆ ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”! ํ˜„์žฌ ๋ง์”€ํ•ด์ฃผ์‹  `AuthoritiesAuthorizationManager` ์ฒ˜๋Ÿผ ๋‹ค์–‘ํ•œ ๊ถŒํ•œ ์ฒด๊ณ„๋Š” ์—†๊ธฐ ๋–„๋ฌธ์— ๊ธฐ์กด `SecuredAuthorizationManager`์—์„œ `AuthorityAuthorizationManager`๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๋ณ€๊ฒฝํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,14 @@ +package nextstep.security.authorization; + +import nextstep.security.authentication.Authentication; +import nextstep.security.authorization.web.AuthorizationResult; + +@FunctionalInterface +public interface AuthorizationManager<T> { + @Deprecated + AuthorizationDecision check(Authentication authentication, T object); + + default AuthorizationResult authorize(Authentication authentication, T object) { + return check(authentication, object); + } +}
Java
์Œ.. ๊ฒฐ๊ตญ์—” **์ธ๊ฐ€์‹คํŒจ์‹œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.** ๊ฐ€ ํ•ต์‹ฌ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์š”. check๋กœ๋Š” null์ด ์˜ฌ ์ˆ˜๋„ ์žˆ๊ณ , ๊ถŒํ•œ์ด ์—†๋‹ค ํ•  ์ง€์–ด๋„ ์—๋Ÿฌ์ฝ”๋“œ ๋“ฑ์—์„œ ๊ฐœ๋ฐœ์ž ๋งˆ์Œ๋Œ€๋กœ ํ•ธ๋“ค๋ง ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋ฉด์—, verify๋Š” ์ธ๊ฐ€์— ์‹คํŒจํ•œ ๊ฒฝ์šฐ ๋™์ผํ•œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ๋œ๋‹ค. ๋ผ๋Š” ์ ์—์„œ ์ถ”์ ์— ์šฉ์ดํ•  ๊ฒƒ ๊ฐ™๊ณ , ์ธ์ ์˜ค๋ฅ˜๋ฅผ ์ตœ์†Œํ™” ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“œ๋„ค์š”! ๋”ฐ๋ผ์„œ ๊ด€๋ฆฌ์ž ์ „์šฉ API๋“ฑ ๊ถŒํ•œ์ด ์—†์œผ๋ฉด ์ถ”๊ฐ€์ ์ธ ๋กœ์ง ์—†์ด ๋ฐ”๋กœ ์˜ˆ์™ธ๋ฅผ ๋˜์งˆ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค~
@@ -0,0 +1,14 @@ +package nextstep.security.authorization; + +import nextstep.security.authentication.Authentication; +import nextstep.security.authorization.web.AuthorizationResult; + +@FunctionalInterface +public interface AuthorizationManager<T> { + @Deprecated + AuthorizationDecision check(Authentication authentication, T object); + + default AuthorizationResult authorize(Authentication authentication, T object) { + return check(authentication, object); + } +}
Java
๋„ค ์‚ฌ์‹ค `verify`๋ฅผ ๋‹จ์ผ๋กœ ์“ฐ๋ฉด ์ €ํฌ๊ฐ€ ํ”ํžˆ ์•Œ๊ณ  ์žˆ๋“ฏ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๊ณณ์—์„œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฒƒ์ด ๋งž๊ธฐ๋Š” ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ `AuthorizationManager`์˜ ์›๋ž˜ ์ฑ…์ž„์€ ์ธ๊ฐ€๊ฐ€ ๋œ ์œ ์ €์ธ์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด๊ณ  ์ด๊ฑด ์ธ๊ฐ€๊ฐ€ ์‹คํŒจ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์ด ์—๋Ÿฌ ์ƒํ™ฉ์ด ์•„๋‹Œ ์ •์ƒ์ ์ธ ๋น„์ฆˆ๋‹ˆ์Šค์˜ ํ๋ฆ„์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์–ด์š”. ์ธ๊ฐ€๊ฐ€ ์‹คํŒจํ•œ ๊ฒƒ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์˜ˆ์™ธ๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์€ `AuthorizationFilter`๊ฐ€ ์ธ๊ฐ€ ์‹คํŒจ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์˜ˆ์™ธ์ด๋‹ค๋ผ๋Š” ๊ฒƒ์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ผ๋ฟ์ธ ๊ฒƒ์ด๊ตฌ์š”. ์‹ค์ œ `AuthorizationManager`๋“ค์˜ ๊ตฌํ˜„์ฒด๋ฅผ ๋ณด์‹œ๋ฉด ์ด๋Ÿฐ ์ด์œ ๋“ค๋„ ๊ฒน์ณ์„œ `check`์•ˆ์—์„œ ๊ฑฐ์˜ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.
@@ -0,0 +1,28 @@ +package nextstep.security.authorization.web; + +import jakarta.servlet.http.HttpServletRequest; +import java.util.List; +import nextstep.security.authentication.Authentication; +import nextstep.security.authorization.AuthorizationDecision; +import nextstep.security.authorization.AuthorizationManager; +import nextstep.security.util.RequestMatcher; +import nextstep.security.util.RequestMatcherEntry; + +public class RequestMatcherDelegatingAuthorizationManager implements AuthorizationManager<HttpServletRequest> { + + private final List<RequestMatcherEntry<AuthorizationManager>> mappings; + + public RequestMatcherDelegatingAuthorizationManager(List<RequestMatcherEntry<AuthorizationManager>> mappings) { + this.mappings = mappings; + } + + @Override + public AuthorizationDecision check(Authentication authentication, HttpServletRequest request) { + for (RequestMatcherEntry<AuthorizationManager> mapping : mappings) { + if (mapping.getRequestMatcher().matches(request)) { + return mapping.getEntry().check(authentication, request); + } + } + return AuthorizationDecision.DENY; + } +}
Java
```suggestion if (mapping.matches(request)) { ``` ์ œ๊ฐ€ ์˜๋„ ๋“œ๋ ธ๋˜ ๋‚ด์šฉ์€ getter๋ฅผ ๊บผ๋‚ด์™€์„œ ๋‹ค์‹œ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ํ•ด๋‹น ๊ฐ์ฒด ๋‚ด์—์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ๋˜์ง€๋Š” ํ˜•ํƒœ๋ฅผ ๋ง์”€๋“œ๋ฆฐ ๊ฒƒ์ด์—ˆ์–ด์š”. getter๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ฉ”์†Œ๋“œ ์ฒด์ด๋‹์ด ๋˜๋Š” ๊ฒƒ์€ ๊ฐ์ฒด์˜ ์—ญํ• ์ด ์žˆ๋‹ค๊ธฐ๋ณด๋‹จ ๋‹จ์ˆœํžˆ ๋ž˜ํ•‘ํด๋ž˜์Šค๊ฐ€ ๋˜์–ด๋ฒ„๋ ค ์ฑ…์ž„๊ณผ ์—ญํ• ์ด ๋ถˆ๋ถ„๋ช…ํ•ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ```java public boolean matches(HttpServletRequest request) { return requestMatcher.matches(request); } ```
@@ -0,0 +1,13 @@ +package nextstep.security.authorization.web; + +import jakarta.servlet.http.HttpServletRequest; +import nextstep.security.authentication.Authentication; +import nextstep.security.authorization.AuthorizationDecision; +import nextstep.security.authorization.AuthorizationManager; + +public class DenyAllAuthorizationManager implements AuthorizationManager<HttpServletRequest> { + @Override + public AuthorizationDecision check(Authentication authentication, HttpServletRequest object) { + return AuthorizationDecision.DENY; + } +}
Java
```suggestion public AuthorizationResult check(Authentication authentication, HttpServletRequest object) { ``` ๋ฐ˜ํ™˜ ํƒ€์ž…๋“ค๋„ ์ž˜ ์ถ”์ƒํ™”ํ•ด์ฃผ์‹  ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด์ฃผ๋ฉด ์ข‹๊ฒ ๋„ค์š” :)
@@ -0,0 +1,22 @@ +package nextstep.security.authorization; + +import nextstep.security.authorization.web.AuthorizationResult; + +public class AuthorizationDecision implements AuthorizationResult { + public static final AuthorizationDecision ALLOW = new AuthorizationDecision(true); + public static final AuthorizationDecision DENY = new AuthorizationDecision(false); + + private final boolean granted; + + public AuthorizationDecision(boolean granted) { + this.granted = granted; + } + + public boolean isGranted() { + return granted; + } + + public static AuthorizationDecision of(boolean granted) { + return granted ? ALLOW : DENY; + } +}
Java
```suggestion public static AuthorizationDecision from(boolean granted) { ``` ๊ด€์Šต์ ์œผ๋กœ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ํ•˜๋‚˜์ผ๋•Œ from, ์—ฌ๋Ÿฌ๊ฐœ์ผ๋•Œ of๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,37 @@ +[ + { + "id": 1, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + }, + { + "id": 2, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + }, + { + "id": 3, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + }, + { + "id": 4, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + }, + { + "id": 5, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + }, + { + "id": 6, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + }, + { + "id": 7, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + } +]
Unknown
์• ๊ตญ์ž ์œคํฌ๋‹˜์˜ ๋œป ์ž˜ ์•Œ๊ฒ ์Šต๋‹ˆ๋‹ค.
@@ -1,9 +1,23 @@ import React from 'react'; +import './Login.scss'; +import LoginForm from './LoginForm'; -class Login extends React.Component { +class LoginYoonHee extends React.Component { render() { - return null; + return ( + <article className="login-art"> + <div className="log-in__main"> + <h1 className="main-name">westagram</h1> + <div className="log-in"> + <LoginForm /> + </div> + <a className="find-ps" href="#!"> + ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”? + </a> + </div> + </article> + ); } } -export default Login; +export default LoginYoonHee;
JavaScript
props์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด ์ž˜ ์ดํ•ดํ•˜์…จ๋„ค์š”! ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ•  ํ•„์š” ์—†์ด, `<LoginForm>` ์ปดํฌ๋„ŒํŠธ์—์„œ withRouter importํ•ด์„œ ๊ฐ์‹ธ์ฃผ์‹œ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•˜์‹œ๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š”!
@@ -0,0 +1,75 @@ +.login-art { + display: flex; + justify-content: center; + align-items: center; + height: 100vh; + background-color: var(--color-boxgray); + + .log-in__main { + display: flex; + flex-direction: column; + justify-content: space-evenly; + align-items: center; + width: 300px; + height: 350px; + background-color: white; + border: 1px solid var(--color-boxborder); + } + + .main-name { + font-size: 40px; + font-weight: lighter; + font-family: 'Lobster', cursive; + } + + .log-in { + display: flex; + flex-direction: column; + align-items: center; + } + + .log-in__id, + .log-in__ps { + width: 230px; + height: 35px; + margin: 3px 0; + padding: 3px 10px; + border-radius: 5px; + border: 1px solid var(--color-boxborder); + color: var(--color-textgray); + background-color: var(--color-boxgray); + } + + .find-ps { + text-decoration: none; + font-size: 13px; + } + + .log-in__btn { + width: 230px; + height: 30px; + margin-top: 10px; + margin-bottom: 80px; + border-radius: 5px; + background-color: var(--color--btn-text); + border: none; + color: white; + font-weight: bold; + } + .disabled { + width: 230px; + height: 30px; + margin-top: 10px; + margin-bottom: 80px; + border-radius: 5px; + background-color: var(--color--btn-text-yet); + border: none; + color: white; + font-weight: bold; + } + + .log-in > form { + display: flex; + flex-direction: column; + } +}
Unknown
- ํ•˜๋‚˜์˜ ์š”์†Œ์— ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์†์„ฑ์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒฝ์šฐ ์ค‘์š”๋„, ๊ด€๋ จ๋„์— ๋”ฐ๋ผ์„œ ๋‚˜๋ฆ„์˜ convention์„ ์ง€์ผœ์„œ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. - ์ผ๋ฐ˜์ ์ธ convention ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆœ์„œ ์ ์šฉํ•ด์ฃผ์„ธ์š”. [CSS property ์ˆœ์„œ] - Layout Properties (position, float, clear, display) - Box Model Properties (width, height, margin, padding) - Visual Properties (color, background, border, box-shadow) - Typography Properties (font-size, font-family, text-align, text-transform) - Misc Properties (cursor, overflow, z-index) - ๋ฆฌ๋ทฐํ•˜์ง€ ์•Š์€ ๋ถ€๋ถ„๋„ ์ฐธ๊ณ ํ•ด์„œ ์ˆ˜์ •ํ•ด์ฃผ์„ธ์š”-!
@@ -0,0 +1,51 @@ +import React from 'react'; +import { withRouter } from 'react-router-dom'; + +class LoginForm extends React.Component { + constructor() { + super(); + this.state = { id: '', ps: '' }; + } + + goToMain = e => { + this.props.history.push('/main-yoonhee'); + }; + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + render() { + const { id, ps } = this.state; + const isAble = id.includes('@') && ps.length >= 5; + return ( + <form> + <input + name="id" + className="log-in__id" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={this.handleInput} + /> + + <input + name="ps" + className="log-in__ps" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={this.handleInput} + /> + <button + type="button" + className={`log-in__btn ${isAble ? '' : 'disabled'}`} + onClick={this.goToMain} + disabled={!isAble} + > + ๋กœ๊ทธ์ธ + </button> + </form> + ); + } +} + +export default withRouter(LoginForm);
JavaScript
๊ณ„์‚ฐ๋œ ์†์„ฑ๋ช… ์ž˜ ํ™œ์šฉํ•ด์ฃผ์…จ๋„ค์š”! :)
@@ -0,0 +1,10 @@ +import React from 'react'; + +class Comment extends React.Component { + render() { + const { innerText } = this.props; + return <li>{innerText}</li>; + } +} + +export default Comment;
JavaScript
map ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๋ถ€๋ถ„์—์„œ key prop ๋ถ€์—ฌํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
isKeyEnter๋ผ๋Š” ํ•จ์ˆ˜๋ช…์€ key๊ฐ€ enter์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” boolean ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์ ํ•ฉํ•œ ๊ฒƒ ๊ฐ™๋„ค์š”. ํ•จ์ˆ˜์˜ ๋™์ž‘์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ์ง๊ด€์ ์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋™์‚ฌํ˜•์œผ๋กœ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”! ex) addCommentByEnter
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
๋ฆฌ๋ทฐํ•˜์ง€ ์•Š์€ ๋ถ€๋ถ„๋„ ์ˆ˜์ •ํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
- `newfeedComment`๋ผ๊ณ  ์ƒˆ๋กœ ์„ ์–ธํ•˜๊ณ  ํ• ๋‹นํ•  ํ•„์š” ์—†์ด, concat ๋ฉ”์„œ๋“œ์— feedComment๋ผ๋Š” state๋ฅผ ์ง์ ‘ ๋„ฃ์–ด์ฃผ์…”๋„ ๋  ๊ฒƒ ๊ฐ™๋„ค์š”. - concat ์ž˜ ์‚ฌ์šฉํ•ด์ฃผ์…จ๋Š”๋ฐ, concat ๋Œ€์‹ ์— spread operator๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋™์ผํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด๋ณด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฐ์Šตํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๊ณ  ์ฐพ์•„์„œ ๊ตฌํ˜„ํ•ด๋ณด์„ธ์š”! ```suggestion const { feedComment, commentList } = this.state; this.setState({ commentList: commentList.concat(feedComment), feedComment: '', }); ```
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
์ด ์ปดํฌ๋„ŒํŠธ์˜ tag๋“ค์—์„œ id์™€ className์„ ๊ฐ™์ด ๋ถ€์—ฌํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”??
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
์œ„์— ๋ฆฌ๋ทฐ๋“œ๋ฆฐ ๋‚ด์šฉ์ด๋„ค์š”! index๋ฅผ props๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ, ์ด ๋ถ€๋ถ„์—์„œ key={index}๋กœ ๋ถ€์—ฌํ•ด์ฃผ์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
๊ทธ๋ฆฌ๊ณ  cur์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ช…์€ ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ๋ช…ํ™•ํ•˜์ง€ ์•Š์€๋ฐ, ํ•ด๋‹น ๋ณ€์ˆ˜๊ฐ€ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋‚ด์šฉ์ด ์ข€ ๋” ์ง๊ด€์ ์œผ๋กœ ๋“œ๋Ÿฌ๋‚  ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์ •ํ•ด์ฃผ์„ธ์š”! ex) comment
@@ -0,0 +1,32 @@ +import React from 'react'; +import Feed from './Feed'; + +class Feeds extends React.Component { + constructor(props) { + super(props); + this.state = { + feeds: [], + }; + } + + componentDidMount() { + fetch('/data/feed.json') + .then(res => res.json()) + .then(data => { + this.setState({ feeds: data }); + }); + } + + render() { + const { feeds } = this.state; + return ( + <div className="feeds"> + {feeds.map(feed => ( + <Feed key={feed.id} img={feed.img} text={feed.text} /> + ))} + </div> + ); + } +} + +export default Feeds;
JavaScript
- method ์ž˜ ์ƒ๋žตํ•ด์ฃผ์…จ๋„ค์š”! ๐Ÿ‘ - ์ถ”๊ฐ€์ ์œผ๋กœ, `http://localhost:3000` ๋ถ€๋ถ„๋„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํฌํŠธ ๋ฒˆํ˜ธ๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ๊ทธ๋•Œ๊ทธ๋•Œ ์ˆ˜์ •ํ•ด์ค˜์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ๋žตํ•ด์„œ ์‚ฌ์šฉํ•ด์ฃผ์„ธ์š”! ```suggestion fetch('/data/feed.json') ```
@@ -0,0 +1,32 @@ +import React from 'react'; +import Feed from './Feed'; + +class Feeds extends React.Component { + constructor(props) { + super(props); + this.state = { + feeds: [], + }; + } + + componentDidMount() { + fetch('/data/feed.json') + .then(res => res.json()) + .then(data => { + this.setState({ feeds: data }); + }); + } + + render() { + const { feeds } = this.state; + return ( + <div className="feeds"> + {feeds.map(feed => ( + <Feed key={feed.id} img={feed.img} text={feed.text} /> + ))} + </div> + ); + } +} + +export default Feeds;
JavaScript
์˜ค์ž‰ ์—ฌ๊ธฐ์—๋Š” key prop ์ž˜ ๋ถ€์—ฌํ•ด์ฃผ์…จ๋„ค์š”! ์‚ด์ง ํ ์„ ์žก์ž๋ฉด,, ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋Š” ๊ฐ ํ”ผ๋“œ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— feeds -> feed๊ฐ€ ๋” ์ ์ ˆํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -1,9 +1,22 @@ +// eslint-disable-next-line import React from 'react'; +import Nav from './Nav'; +import Feeds from './Feeds'; +import MainR from './MainR'; +import './Main.scss'; -class Main extends React.Component { +class MainYoonHee extends React.Component { render() { - return null; + return ( + <div className="main-body"> + <Nav /> + <main> + <Feeds /> + <MainR /> + </main> + </div> + ); } } -export default Main; +export default MainYoonHee;
JavaScript
import ์ˆœ์„œ ์ˆ˜์ •ํ•ด์ฃผ์„ธ์š”! ์ผ๋ฐ˜์ ์ธ convention์„ ๋”ฐ๋ผ ์ˆœ์„œ๋งŒ ์ž˜ ์ง€์ผœ์ฃผ์…”๋„ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง‘๋‹ˆ๋‹ค. ์•„๋ž˜ ์ˆœ์„œ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”. - React โ†’ Library(Package) โ†’ Component โ†’ ๋ณ€์ˆ˜ / ์ด๋ฏธ์ง€ โ†’ css ํŒŒ์ผ(scss ํŒŒ์ผ)
@@ -0,0 +1,25 @@ +import React from 'react'; + +class Recommend extends React.Component { + render() { + const { nickname, img } = this.props; + return ( + <li className="user main-right__user2"> + <div className="user-and-botton"> + <img + className="user__img user__img--brder-red" + alt={nickname} + src={img} + /> + <div className="user-id2"> + <div className="user__id">{nickname}</div> + <div className="text--gray">ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ด</div> + </div> + </div> + <button className="btn btn--hover nnn">ํŒ”๋กœ์šฐ</button> + </li> + ); + } +} + +export default Recommend;
JavaScript
`<li>`ํƒœ๊ทธ, `<div>`ํƒœ๊ทธ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋กœ๋งŒ ๊ฐ์‹ธ์ฃผ์…”๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,22 @@ +import React from 'react'; + +class Story extends React.Component { + render() { + const { nickname, img } = this.props; + return ( + <li className="user main-right__user"> + <img + className="user__img user__img--brder-red" + alt={nickname} + src={img} + /> + <div className="user-id2"> + <div className="user__id">{nickname}</div> + <div className="text--gray">2์‹œ๊ฐ„ ์ „</div> + </div> + </li> + ); + } +} + +export default Story;
JavaScript
์—ฌ๊ธฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€-!
@@ -0,0 +1,281 @@ +/*------๊ณตํ†ต-----*/ +/*------๊ณตํ†ต-----*/ + +.main-body { + background-color: var(--color-boxgray); +} + +main { + display: flex; + margin-left: 100px; +} + +ul > li { + margin: 10px 0; +} + +a { + text-decoration: none; + color: inherit; +} + +.box { + background-color: white; + border: 1px solid var(--color-boxborder); +} +.box-padding { + padding: 10px; +} + +.text { + font-size: 12px; +} + +.text--gray { + margin-top: 3px; + font-size: 13px; + color: var(--color-textgray); +} + +.icon { + border: no; +} + +.btn { + padding: 5px 0px; + border: none; + border-radius: 5px; + background-color: transparent; + color: var(--color--btn-text); + font-weight: bold; +} +.btn--hover:hover { + color: white; + background-color: var(--color--btn-text); +} + +.westagram { + font-family: 'Lobster', cursive; + font-size: 25px; +} +.user { + display: flex; + align-items: center; + justify-content: flex-start; +} + +.user__img { + width: 30px; + height: 30px; + margin-right: 7px; + border-radius: 50%; + border: 1px solid var(--color-textgray); +} + +.user__img--brder-red { + border: 1px solid red; + border-spacing: 3px; +} + +.user__img--small { + width: 20px; + height: 18px; + margin-right: 5px; +} + +.user__img--big { + width: 40px; + height: 40px; +} + +.user__id { + font-size: 14px; + font-weight: bolder; +} + +.user-text2 { + display: flex; + flex-direction: column; +} + +.footer { + margin-top: 15px; +} + +/*--------nav--------*/ +/*--------nav--------*/ +/*--------nav--------*/ + +.nav { + display: flex; + justify-content: space-around; + align-items: center; + padding: 20px 0; + border-bottom: 1px solid var(--color-boxborder); + background-color: white; + + .nav__border { + height: 30px; + border-left: 2px solid black; + } + + .nav-left { + display: flex; + align-items: center; + justify-content: space-between; + width: 18%; + } + + .nav-center { + width: 20%; + } + + .nav-center > .search-bar { + height: 25px; + width: 100%; + border: 1px solid var(--color-boxborder); + background-color: var(--color-boxgray); + text-align: center; + } + .search-bar[value] { + color: var(--color-textgray); + } + + .nav-right { + display: flex; + justify-content: space-between; + width: 12%; + } +} + +/*----------feed----------*/ +/*----------feed----------*/ +/*----------feed----------*/ + +.feeds { + display: flex; + flex-direction: column; + width: 50%; + padding-top: 65px; + + .feed { + display: flex; + align-items: center; + flex-direction: column; + width: 100%; + margin-bottom: 50px; + border: 1px solid var(--color-boxborder); + background-color: white; + } + + .feed__head { + display: flex; + align-items: center; + justify-content: space-between; + width: 95%; + margin: 15px 20px; + } + + .feed__head > .user { + width: 22%; + } + + .feed__img { + width: 100%; + } + + .feed__icons { + display: flex; + justify-content: space-between; + width: 95%; + margin: 12px 0px; + } + + .feed__icons__left { + display: flex; + justify-content: space-between; + width: 18%; + } + + .feed__likes { + display: flex; + align-items: center; + width: 95%; + } + + .feed__text { + width: 95%; + margin-top: 15px; + } + + .feed__comment-box { + width: 100%; + height: 35px; + margin-top: 5px; + border-top: 1px solid var(--color-boxborder); + border-bottom: 1px solid var(--color-boxborder); + } + + .comment__input { + width: 90%; + height: 100%; + padding-left: 14px; + border: none; + } + + .comment__input[placehilder] { + color: var(--color-textgray); + } + + .comment__btn { + width: 8%; + height: 80; + } + + .feed__comment { + width: 100%; + } + .feed__comment-list { + width: 95%; + margin: 10px 0; + padding: 5px 14px; + } + + .feed__comment-list > li { + font-size: 13px; + } +} + +/*-------------main-right------------*/ +/*-------------main-right------------*/ +/*-------------main-right------------*/ + +.main-right { + position: fixed; + right: 40px; + width: 25%; + margin-top: 65px; + + .main-right__header { + display: flex; + align-items: flex-end; + justify-content: space-between; + margin-bottom: 15px; + padding: 0 5px; + } + + .user-and-botton { + width: 70%; + display: flex; + } + + .main-right__story > .box { + margin-top: 15px; + } + + .main-right__user2 { + display: flex; + justify-content: space-between; + margin: 2px; + } +}
Unknown
๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์†์„ฑ์€ ํŒ€์›๋“ค๊ณผ ์ƒ์˜ํ•˜์—ฌ common.scss ํŒŒ์ผ๋กœ ์˜ฎ๊ฒจ์ฃผ์„ธ์š”!
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
๋ฆฌ์•กํŠธ๋กœ ํŒŒ์ผ ์˜ฎ๊ฒจ์˜ค๊ธฐ์ „ jsํŒŒ์ผ๋กœ๋งŒ ์ž‘์—…ํ•  ๋•Œ ๋”์œผ๋กœ ์ ‘๊ทผํ•˜๋Š” ์šฉ๋„๋กœ ์“ฐ๊ณ  ์ง€์šฐ๋Š”๊ฑธ ๊นœ๋นกํ–ˆ๋„ค์š” โ€ฆ;;;ใ… ใ… ์ง€์› ์Šต๋‹ˆ๋‹ค!
@@ -1,9 +1,23 @@ import React from 'react'; +import './Login.scss'; +import LoginForm from './LoginForm'; -class Login extends React.Component { +class LoginYoonHee extends React.Component { render() { - return null; + return ( + <article className="login-art"> + <div className="log-in__main"> + <h1 className="main-name">westagram</h1> + <div className="log-in"> + <LoginForm /> + </div> + <a className="find-ps" href="#!"> + ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”? + </a> + </div> + </article> + ); } } -export default Login; +export default LoginYoonHee;
JavaScript
์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹น! ๋•๋ถ„์— ๋ผ์šฐํ„ฐ์— ๋Œ€ํ•ด์„œ ์ข€ ๋” ์•Œ๊ฒŒ๋๋„ค์šฉ ใ…Žใ…Ž
@@ -0,0 +1,10 @@ +import React from 'react'; + +class Comment extends React.Component { + render() { + const { innerText } = this.props; + return <li>{innerText}</li>; + } +} + +export default Comment;
JavaScript
์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค-! key๊ฐ’ ๋„ฃ๋Š”๊ณณ์„ ์ •ํ™•ํžˆ ๋ชจ๋ฅด๊ณ ์žˆ์—ˆ๋Š”๋ฐ ์ด์ œ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ๊ฑฐ๊ฐ™์•„์š”!
@@ -0,0 +1,32 @@ +import React from 'react'; +import Feed from './Feed'; + +class Feeds extends React.Component { + constructor(props) { + super(props); + this.state = { + feeds: [], + }; + } + + componentDidMount() { + fetch('/data/feed.json') + .then(res => res.json()) + .then(data => { + this.setState({ feeds: data }); + }); + } + + render() { + const { feeds } = this.state; + return ( + <div className="feeds"> + {feeds.map(feed => ( + <Feed key={feed.id} img={feed.img} text={feed.text} /> + ))} + </div> + ); + } +} + +export default Feeds;
JavaScript
์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค-! ํฌํŠธ๋ฒˆํ˜ธ๋Š” ์ƒ๋žต์œผ๋ฃจ! ํŒ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹น!
@@ -0,0 +1,32 @@ +import React from 'react'; +import Feed from './Feed'; + +class Feeds extends React.Component { + constructor(props) { + super(props); + this.state = { + feeds: [], + }; + } + + componentDidMount() { + fetch('/data/feed.json') + .then(res => res.json()) + .then(data => { + this.setState({ feeds: data }); + }); + } + + render() { + const { feeds } = this.state; + return ( + <div className="feeds"> + {feeds.map(feed => ( + <Feed key={feed.id} img={feed.img} text={feed.text} /> + ))} + </div> + ); + } +} + +export default Feeds;
JavaScript
feed๋กœ ์ˆ˜์ •์™„๋ฃŒ ํ–ˆ์Šต๋‹ˆ๋‹น ๐Ÿ˜Š
@@ -1,9 +1,22 @@ +// eslint-disable-next-line import React from 'react'; +import Nav from './Nav'; +import Feeds from './Feeds'; +import MainR from './MainR'; +import './Main.scss'; -class Main extends React.Component { +class MainYoonHee extends React.Component { render() { - return null; + return ( + <div className="main-body"> + <Nav /> + <main> + <Feeds /> + <MainR /> + </main> + </div> + ); } } -export default Main; +export default MainYoonHee;
JavaScript
scssํŒŒ์ผ ์•„๋ž˜๋กœ ์œ„์น˜์ˆ˜์ • ํ–ˆ์Šต๋‹ˆ๋‹น !
@@ -1,9 +1,22 @@ +// eslint-disable-next-line import React from 'react'; +import Nav from './Nav'; +import Feeds from './Feeds'; +import MainR from './MainR'; +import './Main.scss'; -class Main extends React.Component { +class MainYoonHee extends React.Component { render() { - return null; + return ( + <div className="main-body"> + <Nav /> + <main> + <Feeds /> + <MainR /> + </main> + </div> + ); } } -export default Main; +export default MainYoonHee;
JavaScript
๊ทธ ๋•Œ ๋ง์”€ํ•ด์ฃผ์…จ๋˜ ๋ถ€๋ถ„์ด๋„ค์šฉ ใ…Žใ…Ž.. <div>๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹น.. ์ฝ˜์†”์ฐฝ์— ๋œจ๋˜ ์•Œ์ˆ˜์—†๋Š” ๋นจ๊ฐ„์ƒ‰ ๊ธ€์”จ ์˜ค๋ฅ˜๊ฐ€ ์ด๊ฒƒ๋–„๋ฌธ์ด์˜€๊ตฐ์šฉ ใ… ใ…  index.html์—์žˆ๋Š” body๋ถ€๋ถ„๊ณผ ์ถฉ๋Œํ•ด์„œ์š”; ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ๋ฅผ ์ƒ๊ฐํ•˜๋ฉด ๋‹น์—ฐํ•œ๊ฑด๋ฐ ์ œ๊ฐ€ ๋„ˆ๋ฌด ์•ˆ์ผํ–ˆ๋„ค์š”..
@@ -0,0 +1,25 @@ +import React from 'react'; + +class Recommend extends React.Component { + render() { + const { nickname, img } = this.props; + return ( + <li className="user main-right__user2"> + <div className="user-and-botton"> + <img + className="user__img user__img--brder-red" + alt={nickname} + src={img} + /> + <div className="user-id2"> + <div className="user__id">{nickname}</div> + <div className="text--gray">ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ด</div> + </div> + </div> + <button className="btn btn--hover nnn">ํŒ”๋กœ์šฐ</button> + </li> + ); + } +} + +export default Recommend;
JavaScript
์ˆ˜์ •ํ–ˆ์ˆจ๋ฏธ๋‹น~!๐Ÿ˜‡
@@ -0,0 +1,22 @@ +import React from 'react'; + +class Story extends React.Component { + render() { + const { nickname, img } = this.props; + return ( + <li className="user main-right__user"> + <img + className="user__img user__img--brder-red" + alt={nickname} + src={img} + /> + <div className="user-id2"> + <div className="user__id">{nickname}</div> + <div className="text--gray">2์‹œ๊ฐ„ ์ „</div> + </div> + </li> + ); + } +} + +export default Story;
JavaScript
์—ฌ๊ธฐ๋‘์šฉ~
@@ -0,0 +1,281 @@ +/*------๊ณตํ†ต-----*/ +/*------๊ณตํ†ต-----*/ + +.main-body { + background-color: var(--color-boxgray); +} + +main { + display: flex; + margin-left: 100px; +} + +ul > li { + margin: 10px 0; +} + +a { + text-decoration: none; + color: inherit; +} + +.box { + background-color: white; + border: 1px solid var(--color-boxborder); +} +.box-padding { + padding: 10px; +} + +.text { + font-size: 12px; +} + +.text--gray { + margin-top: 3px; + font-size: 13px; + color: var(--color-textgray); +} + +.icon { + border: no; +} + +.btn { + padding: 5px 0px; + border: none; + border-radius: 5px; + background-color: transparent; + color: var(--color--btn-text); + font-weight: bold; +} +.btn--hover:hover { + color: white; + background-color: var(--color--btn-text); +} + +.westagram { + font-family: 'Lobster', cursive; + font-size: 25px; +} +.user { + display: flex; + align-items: center; + justify-content: flex-start; +} + +.user__img { + width: 30px; + height: 30px; + margin-right: 7px; + border-radius: 50%; + border: 1px solid var(--color-textgray); +} + +.user__img--brder-red { + border: 1px solid red; + border-spacing: 3px; +} + +.user__img--small { + width: 20px; + height: 18px; + margin-right: 5px; +} + +.user__img--big { + width: 40px; + height: 40px; +} + +.user__id { + font-size: 14px; + font-weight: bolder; +} + +.user-text2 { + display: flex; + flex-direction: column; +} + +.footer { + margin-top: 15px; +} + +/*--------nav--------*/ +/*--------nav--------*/ +/*--------nav--------*/ + +.nav { + display: flex; + justify-content: space-around; + align-items: center; + padding: 20px 0; + border-bottom: 1px solid var(--color-boxborder); + background-color: white; + + .nav__border { + height: 30px; + border-left: 2px solid black; + } + + .nav-left { + display: flex; + align-items: center; + justify-content: space-between; + width: 18%; + } + + .nav-center { + width: 20%; + } + + .nav-center > .search-bar { + height: 25px; + width: 100%; + border: 1px solid var(--color-boxborder); + background-color: var(--color-boxgray); + text-align: center; + } + .search-bar[value] { + color: var(--color-textgray); + } + + .nav-right { + display: flex; + justify-content: space-between; + width: 12%; + } +} + +/*----------feed----------*/ +/*----------feed----------*/ +/*----------feed----------*/ + +.feeds { + display: flex; + flex-direction: column; + width: 50%; + padding-top: 65px; + + .feed { + display: flex; + align-items: center; + flex-direction: column; + width: 100%; + margin-bottom: 50px; + border: 1px solid var(--color-boxborder); + background-color: white; + } + + .feed__head { + display: flex; + align-items: center; + justify-content: space-between; + width: 95%; + margin: 15px 20px; + } + + .feed__head > .user { + width: 22%; + } + + .feed__img { + width: 100%; + } + + .feed__icons { + display: flex; + justify-content: space-between; + width: 95%; + margin: 12px 0px; + } + + .feed__icons__left { + display: flex; + justify-content: space-between; + width: 18%; + } + + .feed__likes { + display: flex; + align-items: center; + width: 95%; + } + + .feed__text { + width: 95%; + margin-top: 15px; + } + + .feed__comment-box { + width: 100%; + height: 35px; + margin-top: 5px; + border-top: 1px solid var(--color-boxborder); + border-bottom: 1px solid var(--color-boxborder); + } + + .comment__input { + width: 90%; + height: 100%; + padding-left: 14px; + border: none; + } + + .comment__input[placehilder] { + color: var(--color-textgray); + } + + .comment__btn { + width: 8%; + height: 80; + } + + .feed__comment { + width: 100%; + } + .feed__comment-list { + width: 95%; + margin: 10px 0; + padding: 5px 14px; + } + + .feed__comment-list > li { + font-size: 13px; + } +} + +/*-------------main-right------------*/ +/*-------------main-right------------*/ +/*-------------main-right------------*/ + +.main-right { + position: fixed; + right: 40px; + width: 25%; + margin-top: 65px; + + .main-right__header { + display: flex; + align-items: flex-end; + justify-content: space-between; + margin-bottom: 15px; + padding: 0 5px; + } + + .user-and-botton { + width: 70%; + display: flex; + } + + .main-right__story > .box { + margin-top: 15px; + } + + .main-right__user2 { + display: flex; + justify-content: space-between; + margin: 2px; + } +}
Unknown
common.scss ํŒŒ์ผ์— ์ด๋ฏธ ์žˆ์–ด์„œ ํ•ด๋‹น ๋ถ€๋ถ„ ์‚ญ์ œํ–ˆ์–ด์šฉ ใ…Ž...๐Ÿ˜ฑ
@@ -0,0 +1,51 @@ +import React from 'react'; +import { withRouter } from 'react-router-dom'; + +class LoginForm extends React.Component { + constructor() { + super(); + this.state = { id: '', ps: '' }; + } + + goToMain = e => { + this.props.history.push('/main-yoonhee'); + }; + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + render() { + const { id, ps } = this.state; + const isAble = id.includes('@') && ps.length >= 5; + return ( + <form> + <input + name="id" + className="log-in__id" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={this.handleInput} + /> + + <input + name="ps" + className="log-in__ps" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={this.handleInput} + /> + <button + type="button" + className={`log-in__btn ${isAble ? '' : 'disabled'}`} + onClick={this.goToMain} + disabled={!isAble} + > + ๋กœ๊ทธ์ธ + </button> + </form> + ); + } +} + +export default withRouter(LoginForm);
JavaScript
๋ž˜์˜๋‹˜ ๊ฐ•์˜ ๋•๋ถ„์ž…๋‹ˆ๋‹น ใ…‹.ใ…‹
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
๋ฐ์ดํ„ฐ ๋‚ด์šฉ์˜ ๋œป์„ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ comment๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,29 @@ +package nextstep.security.authorization; + +import nextstep.security.authentication.Authentication; +import nextstep.security.authentication.AuthenticationException; +import org.aopalliance.intercept.MethodInvocation; + +import java.lang.reflect.Method; +import java.util.List; + +public class SecuredAuthorizationManager implements AuthorizationManager<MethodInvocation> { + private final AuthoritiesAuthorizationManager authoritiesAuthorizationManager = new AuthoritiesAuthorizationManager(); + + @Override + public AuthorizationDecision check(final Authentication authentication, final MethodInvocation invocation) { + Method method = invocation.getMethod(); + + if (method.isAnnotationPresent(Secured.class)) { + Secured secured = method.getAnnotation(Secured.class); + + if (authentication == null) { + throw new AuthenticationException(); + } + + return authoritiesAuthorizationManager.check(authentication, List.of(secured.value())); + } + + return new AuthorizationDecision(true); + } +}
Java
method.getAnnotation(Secured.class)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ `@Secured` ์–ด๋…ธํ…Œ์ด์…˜์„ ์กฐํšŒํ•˜๊ณ  ์žˆ๋„ค์š”. Spring AOP ํ™˜๊ฒฝ์—์„œ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ํ”„๋ก์‹œ ๊ฐ์ฒด๋กœ ๊ฐ์‹ธ์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ํ”„๋ก์‹œ๋œ ๋ฉ”์„œ๋“œ๋ฅผ ์กฐํšŒํ•  ๊ฒฝ์šฐ ์‹ค์ œ ๊ตฌํ˜„์ฒด์˜ ๋ฉ”์„œ๋“œ์—์„œ ์„ ์–ธ๋œ `@Secured` ์–ด๋…ธํ…Œ์ด์…˜์„ ์ฐพ์ง€ ๋ชปํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. method.getAnnotation ์™€ AopUtils.getMostSpecificMethod() ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ์„๊นŒ์š”? ๐Ÿ˜„
@@ -23,7 +23,7 @@ @AutoConfigureMockMvc class BasicAuthTest { private final Member TEST_ADMIN_MEMBER = new Member("[email protected]", "password", "a", "", Set.of("ADMIN")); - private final Member TEST_USER_MEMBER = new Member("[email protected]", "password", "b", "", Set.of()); + private final Member TEST_USER_MEMBER = new Member("[email protected]", "password", "b", "", Set.of("")); @Autowired private MockMvc mockMvc; @@ -37,6 +37,33 @@ void setUp() { memberRepository.save(TEST_USER_MEMBER); } + @DisplayName("์ธ์ฆ๋œ ์‚ฌ์šฉ์ž๋Š” ์ž์‹ ์˜ ์ •๋ณด๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.") + @Test + void request_success_members_me() throws Exception { + String token = Base64.getEncoder().encodeToString((TEST_USER_MEMBER.getEmail() + ":" + TEST_USER_MEMBER.getPassword()).getBytes()); + + ResultActions response = mockMvc.perform(get("/members/me") + .header("Authorization", "Basic " + token) + .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE) + ); + + response.andExpect(status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$.email").value(TEST_USER_MEMBER.getEmail())); + } + + @DisplayName("์ธ์ฆ๋˜์ง€ ์•Š์€ ์‚ฌ์šฉ์ž๋Š” ์ž์‹ ์˜ ์ •๋ณด๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์—†๋‹ค.") + @Test + void request_fail_members_me() throws Exception { + String token = Base64.getEncoder().encodeToString(("none" + ":" + "none").getBytes()); + + ResultActions response = mockMvc.perform(get("/members/me") + .header("Authorization", "Basic " + token) + .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE) + ); + + response.andExpect(status().isUnauthorized()); + } + @DisplayName("ADMIN ๊ถŒํ•œ์„ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญํ•  ๊ฒฝ์šฐ ๋ชจ๋“  ํšŒ์› ์ •๋ณด๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.") @Test void request_success_with_admin_user() throws Exception { @@ -64,6 +91,20 @@ void request_fail_with_general_user() throws Exception { response.andExpect(status().isForbidden()); } + @DisplayName("ํ—ˆ์šฉ๋œ URI์ด ์•„๋‹Œ๊ฒฝ์šฐ ์š”์ฒญ์ด ์‹คํŒจ ํ•œ๋‹ค.") + @Test + void request_fail_invalid_uri() throws Exception { + String token = Base64.getEncoder().encodeToString((TEST_USER_MEMBER.getEmail() + ":" + TEST_USER_MEMBER.getPassword()).getBytes()); + + ResultActions response = mockMvc.perform(get("/invalid-uri") + .header("Authorization", "Basic " + token) + .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE) + ); + + response.andExpect(status().isForbidden()); + } + + @DisplayName("์‚ฌ์šฉ์ž ์ •๋ณด๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ์š”์ฒญ์ด ์‹คํŒจํ•ด์•ผ ํ•œ๋‹ค.") @Test void request_fail_with_no_user() throws Exception {
Java
ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๐Ÿ‘
@@ -0,0 +1,29 @@ +package nextstep.security.authorization; + +import nextstep.security.authentication.Authentication; +import nextstep.security.authentication.AuthenticationException; +import org.aopalliance.intercept.MethodInvocation; + +import java.lang.reflect.Method; +import java.util.List; + +public class SecuredAuthorizationManager implements AuthorizationManager<MethodInvocation> { + private final AuthoritiesAuthorizationManager authoritiesAuthorizationManager = new AuthoritiesAuthorizationManager(); + + @Override + public AuthorizationDecision check(final Authentication authentication, final MethodInvocation invocation) { + Method method = invocation.getMethod(); + + if (method.isAnnotationPresent(Secured.class)) { + Secured secured = method.getAnnotation(Secured.class); + + if (authentication == null) { + throw new AuthenticationException(); + } + + return authoritiesAuthorizationManager.check(authentication, List.of(secured.value())); + } + + return new AuthorizationDecision(true); + } +}
Java
AopUtils.getMostSpecificMethod()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ”„๋ก์‹œ ๊ฐ์ฒด๋“  ์•„๋‹ˆ๋“  ์‹ค์ œ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์กฐํšŒํ•˜์—ฌ @Secured ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ชป์ฐพ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ฒ ๋„ค์š”! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,83 @@ +@import '../../../../../Styles/common.scss'; + +.navYeseul { + position: fixed; + top: 0; + left: 50%; + right: 0; + transform: translateX(-50%); + padding: 8px 0; + border-bottom: 1px solid $main-border; + background-color: #fff; + z-index: 9999; + + .inner-nav { + display: flex; + justify-content: space-between; + align-items: center; + margin: 0 auto; + padding: 0 20px; + width: 100%; + max-width: 975px; + box-sizing: border-box; + + h1 { + display: flex; + align-items: center; + margin: 0; + font-size: 28px; + color: $main-font; + + button { + margin-right: 15px; + padding: 0 14px 0 0; + width: 22px; + height: 22px; + box-sizing: content-box; + border-right: 2px solid $main-font; + + img { + margin: 0; + } + } + } + + .nav__search { + display: flex; + align-items: center; + padding: 3px 10px 3px 26px; + width: 215px; + min-width: 125px; + height: 28px; + box-sizing: border-box; + border: 1px solid $main-border; + border-radius: 3px; + background-color: $bg-light-grey; + + input { + height: 100%; + background-color: transparent; + + &::placeholder { + text-align: center; + } + } + } + + .nav__menu { + display: flex; + + li { + margin-left: 22px; + width: 22px; + height: 22px; + + .like-button { + padding: 0; + width: 22px; + height: 22px; + } + } + } + } +}
Unknown
css ์†์„ฑ ์ˆœ์„œ์— ๋”ฐ๋ฅด๋ฉด z-index๊ฐ€ ๊ฐ€์žฅ ์•„๋ž˜์— ์™€์•ผ ํ•  ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿ˜€
@@ -0,0 +1,83 @@ +@import '../../../../../Styles/common.scss'; + +.navYeseul { + position: fixed; + top: 0; + left: 50%; + right: 0; + transform: translateX(-50%); + padding: 8px 0; + border-bottom: 1px solid $main-border; + background-color: #fff; + z-index: 9999; + + .inner-nav { + display: flex; + justify-content: space-between; + align-items: center; + margin: 0 auto; + padding: 0 20px; + width: 100%; + max-width: 975px; + box-sizing: border-box; + + h1 { + display: flex; + align-items: center; + margin: 0; + font-size: 28px; + color: $main-font; + + button { + margin-right: 15px; + padding: 0 14px 0 0; + width: 22px; + height: 22px; + box-sizing: content-box; + border-right: 2px solid $main-font; + + img { + margin: 0; + } + } + } + + .nav__search { + display: flex; + align-items: center; + padding: 3px 10px 3px 26px; + width: 215px; + min-width: 125px; + height: 28px; + box-sizing: border-box; + border: 1px solid $main-border; + border-radius: 3px; + background-color: $bg-light-grey; + + input { + height: 100%; + background-color: transparent; + + &::placeholder { + text-align: center; + } + } + } + + .nav__menu { + display: flex; + + li { + margin-left: 22px; + width: 22px; + height: 22px; + + .like-button { + padding: 0; + width: 22px; + height: 22px; + } + } + } + } +}
Unknown
common css์— ์žˆ์–ด์„œ ๋นผ์…”๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,125 @@ +import React, { Component } from 'react'; +import { withRouter } from 'react-router-dom'; +import { API } from '../../../config'; +import './Login.scss'; + +class LoginYeseul extends Component { + constructor(props) { + super(props); + this.state = { + inputId: '', + inputPw: '', + loginMode: true, + }; + } + + handleInput = e => { + const { name, value } = e.target; + this.setState({ [name]: value }); + }; + + convertMode = () => { + this.setState({ + loginMode: !this.state.loginMode, + }); + }; + + signIn = e => { + const { inputId, inputPw } = this.state; + e.preventDefault(); + fetch(API.SIGN_IN, { + method: 'POST', + body: JSON.stringify({ + email: inputId, + password: inputPw, + }), + }) + .then(users => users.json()) + .then(users => { + if (users.MESSAGE === 'SUCCESS') { + this.setState({ + inputId: '', + inputPw: '', + }); + localStorage.setItem('token', users.ACCESS_TOKEN); + this.props.history.push('/main-yeseul'); + } else if (users.MESSAGE === 'INVALID_USER') { + const wantToSignUp = window.confirm( + '์ž˜๋ชป๋œ ์ •๋ณด์ž…๋‹ˆ๋‹ค. ํšŒ์›๊ฐ€์ž…ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?' + ); + wantToSignUp && this.convertMode(); + } + }); + }; + + signUp = e => { + const { inputId, inputPw } = this.state; + e.preventDefault(); + fetch(API.SIGN_UP, { + method: 'POST', + body: JSON.stringify({ + email: inputId, + password: inputPw, + }), + }) + .then(users => users.json()) + .then(users => { + if (users.MESSAGE === 'SUCCESS') { + this.setState({ + inputId: '', + inputPw: '', + }); + alert(`ํšŒ์›๊ฐ€์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค!๐ŸŽ‰ ๋กœ๊ทธ์ธํ•ด์ฃผ์„ธ์š”`); + this.convertMode(); + } else { + alert(users.MESSAGE); + } + }); + }; + + render() { + const { inputId, inputPw, loginMode } = this.state; + const checkId = /^\w[\w\-.]*@\w+\.\w{2,}/; + + return ( + <main className="loginYeseul give-border"> + <h1 className="logo">westagram</h1> + <form + className="login-form" + onSubmit={loginMode ? this.signIn : this.signUp} + > + <div className="login-form__input-box"> + <input + type="text" + name="inputId" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + value={inputId} + onChange={this.handleInput} + /> + <input + type="password" + name="inputPw" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + value={inputPw} + onChange={this.handleInput} + /> + </div> + <button + type="submit" + disabled={!(checkId.test(inputId) && inputPw.length > 8)} + > + {loginMode ? '๋กœ๊ทธ์ธ' : 'ํšŒ์›๊ฐ€์ž…'} + </button> + </form> + <a + href="https://www.instagram.com/accounts/password/reset/" + className="find-pw" + > + ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”? + </a> + </main> + ); + } +} + +export default withRouter(LoginYeseul);
JavaScript
๋””ํ…Œ์ผ....๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,140 @@ +import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; +import User from '../User/User'; +import Comment from '../Comment/Comment'; +import IconButton from '../Button/IconButton'; +import { API } from '../../../../../config'; +import './Feed.scss'; + +class Feed extends Component { + constructor(props) { + super(props); + this.state = { + inputComment: '', + commentId: 0, + comments: [], + }; + } + + componentDidMount() { + const { feedId } = this.props; + fetch(API.COMMENT) + .then(comments => comments.json()) + .then(comments => { + this.setState({ + commentId: comments.length + 1, + comments: comments.filter(comment => comment.feedId === feedId), + }); + }); + } + + handleInput = e => { + this.setState({ inputComment: e.target.value }); + }; + + addComment = e => { + const { inputComment, commentId, comments } = this.state; + + e.preventDefault(); + this.setState({ + inputComment: '', + commentId: commentId + 1, + comments: [ + ...comments, + { + id: commentId.toString(), + writer: this.props.userName, + content: inputComment, + tagId: '', + }, + ], + }); + }; + + deleteComment = clickedId => { + const { comments } = this.state; + this.setState({ + comments: comments.filter(comment => comment.id !== clickedId), + }); + }; + + render() { + const { inputComment, comments } = this.state; + const { writer, contents } = this.props; + + return ( + <article className="feed give-border"> + <header className="feed__header"> + <User size="small" user={writer}> + <IconButton + className="feed__header__more-icon align-right" + info={{ name: '๋”๋ณด๊ธฐ', fileName: 'more.svg' }} + /> + </User> + </header> + <div className="feed__image"> + <img + alt={`by ${writer.name} on ${contents.date}`} + src={contents.postedImage} + /> + </div> + <div className="feed__btns"> + <button type="button"> + <img + alt="์ข‹์•„์š”" + src="https://s3.ap-northeast-2.amazonaws.com/cdn.wecode.co.kr/bearu/heart.png" + /> + </button> + <IconButton info={{ name: '๋Œ“๊ธ€', fileName: 'comment.svg' }} /> + <IconButton info={{ name: '๊ณต์œ ํ•˜๊ธฐ', fileName: 'send.svg' }} /> + <IconButton + className="align-right" + info={{ name: '๋ถ๋งˆํฌ', fileName: 'bookmark.svg' }} + /> + </div> + <p className="feed__likes-number"> + <Link to="/main-yeseul">{`์ข‹์•„์š” ${contents.likesNum}๊ฐœ`}</Link> + </p> + <div className="feed__description"> + <p> + <span className="user-name">{writer.name}</span> + <span>{contents.description}</span> + </p> + </div> + <div className="feed__comments"> + {comments.map(comment => ( + <Comment + key={comment.id} + info={comment} + handleClick={this.deleteComment} + /> + ))} + </div> + <form + className="feed__form align-item-center space-between" + name="commentForm" + > + <IconButton info={{ name: '์ด๋ชจํ‹ฐ์ฝ˜', fileName: 'emoticon.svg' }} /> + <input + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + value={inputComment} + className="feed__input-comment" + name="inputComment" + onChange={this.handleInput} + /> + <input + type="submit" + className="feed__submit-comment" + name="submitComment" + value="๊ฒŒ์‹œ" + disabled={!(inputComment.length > 0)} + onClick={this.addComment} + /> + </form> + </article> + ); + } +} + +export default Feed;
JavaScript
์˜คํ™ ์˜ˆ์Šฌ๋‹˜์€ filter๋กœ ๊ตฌํ˜„ํ•˜์…จ๋„ค์—ฌ! :)
@@ -0,0 +1,140 @@ +import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; +import User from '../User/User'; +import Comment from '../Comment/Comment'; +import IconButton from '../Button/IconButton'; +import { API } from '../../../../../config'; +import './Feed.scss'; + +class Feed extends Component { + constructor(props) { + super(props); + this.state = { + inputComment: '', + commentId: 0, + comments: [], + }; + } + + componentDidMount() { + const { feedId } = this.props; + fetch(API.COMMENT) + .then(comments => comments.json()) + .then(comments => { + this.setState({ + commentId: comments.length + 1, + comments: comments.filter(comment => comment.feedId === feedId), + }); + }); + } + + handleInput = e => { + this.setState({ inputComment: e.target.value }); + }; + + addComment = e => { + const { inputComment, commentId, comments } = this.state; + + e.preventDefault(); + this.setState({ + inputComment: '', + commentId: commentId + 1, + comments: [ + ...comments, + { + id: commentId.toString(), + writer: this.props.userName, + content: inputComment, + tagId: '', + }, + ], + }); + }; + + deleteComment = clickedId => { + const { comments } = this.state; + this.setState({ + comments: comments.filter(comment => comment.id !== clickedId), + }); + }; + + render() { + const { inputComment, comments } = this.state; + const { writer, contents } = this.props; + + return ( + <article className="feed give-border"> + <header className="feed__header"> + <User size="small" user={writer}> + <IconButton + className="feed__header__more-icon align-right" + info={{ name: '๋”๋ณด๊ธฐ', fileName: 'more.svg' }} + /> + </User> + </header> + <div className="feed__image"> + <img + alt={`by ${writer.name} on ${contents.date}`} + src={contents.postedImage} + /> + </div> + <div className="feed__btns"> + <button type="button"> + <img + alt="์ข‹์•„์š”" + src="https://s3.ap-northeast-2.amazonaws.com/cdn.wecode.co.kr/bearu/heart.png" + /> + </button> + <IconButton info={{ name: '๋Œ“๊ธ€', fileName: 'comment.svg' }} /> + <IconButton info={{ name: '๊ณต์œ ํ•˜๊ธฐ', fileName: 'send.svg' }} /> + <IconButton + className="align-right" + info={{ name: '๋ถ๋งˆํฌ', fileName: 'bookmark.svg' }} + /> + </div> + <p className="feed__likes-number"> + <Link to="/main-yeseul">{`์ข‹์•„์š” ${contents.likesNum}๊ฐœ`}</Link> + </p> + <div className="feed__description"> + <p> + <span className="user-name">{writer.name}</span> + <span>{contents.description}</span> + </p> + </div> + <div className="feed__comments"> + {comments.map(comment => ( + <Comment + key={comment.id} + info={comment} + handleClick={this.deleteComment} + /> + ))} + </div> + <form + className="feed__form align-item-center space-between" + name="commentForm" + > + <IconButton info={{ name: '์ด๋ชจํ‹ฐ์ฝ˜', fileName: 'emoticon.svg' }} /> + <input + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + value={inputComment} + className="feed__input-comment" + name="inputComment" + onChange={this.handleInput} + /> + <input + type="submit" + className="feed__submit-comment" + name="submitComment" + value="๊ฒŒ์‹œ" + disabled={!(inputComment.length > 0)} + onClick={this.addComment} + /> + </form> + </article> + ); + } +} + +export default Feed;
JavaScript
id๊ฐ’์œผ๋กœ key props ํ• ๋‹น ๐Ÿ‘ ๐Ÿ’ฏ ๐Ÿฅ‡