RegisterController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use Illuminate\Foundation\Auth\RegistersUsers;
  4. use Illuminate\Support\Carbon;
  5. use Illuminate\Support\Facades\Mail;
  6. use Illuminate\Support\Facades\URL;
  7. use Illuminate\Support\Facades\Validator;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\JsonResponse;
  11. use App\Http\Controllers\Controller;
  12. use App\Providers\RouteServiceProvider;
  13. use App\Models\User;
  14. use App\Models\DTO\ResponseData;
  15. use App\Mail\VerifyLink;
  16. use App\Rules\AllowNickname;
  17. use App\Rules\NumberLength;
  18. use App\Rules\SpecialCharLength;
  19. use App\Rules\UppercaseLength;
  20. use App\Rules\DeniedEmail;
  21. use Exception;
  22. class RegisterController extends Controller
  23. {
  24. /*
  25. |--------------------------------------------------------------------------
  26. | Register Controller
  27. |--------------------------------------------------------------------------
  28. |
  29. | This controller handles the registration of new users as well as their
  30. | validation and creation. By default this controller uses a trait to
  31. | provide this functionality without requiring any additional code.
  32. |
  33. */
  34. use RegistersUsers;
  35. /**
  36. * Where to redirect users after registration.
  37. *
  38. * @var string
  39. */
  40. protected $redirectTo = RouteServiceProvider::HOME;
  41. /**
  42. * Create a new controller instance.
  43. *
  44. * @return void
  45. */
  46. public function __construct()
  47. {
  48. $this->middleware('guest');
  49. }
  50. /**
  51. * Show the application registration form.
  52. *
  53. * @return \Illuminate\View\View
  54. */
  55. public function showRegistrationForm()
  56. {
  57. $policy_1 = nl2br(config('user_register_policy_1')); // 이용약관
  58. $policy_2 = nl2br(config('user_register_policy_1')); // 개인정보처리방침
  59. // 비밀번호 조건 확인
  60. $passwordMinLength = config('password_min_length');
  61. $passwordUppercaseLength = config('password_uppercase_length');
  62. $passwordNumbersLength = config('password_numbers_length');
  63. $passwordSpecialcharsLength = config('password_specialchars_length');
  64. $passwordGuideTip = "";
  65. if($passwordMinLength > 0) {
  66. $passwordGuideTip .= sprintf('최소 %d자 이상, ', $passwordMinLength);
  67. }
  68. if($passwordUppercaseLength > 0) {
  69. $passwordGuideTip .= sprintf('대문자 %d자 이상, ', $passwordUppercaseLength);
  70. }
  71. if($passwordNumbersLength > 0) {
  72. $passwordGuideTip .= sprintf('숫자 %d자 이상, ', $passwordNumbersLength);
  73. }
  74. if($passwordSpecialcharsLength > 0) {
  75. $passwordGuideTip .= sprintf('특수문자 %d자 이상, ', $passwordSpecialcharsLength);
  76. }
  77. $passwordGuideTip = rtrim($passwordGuideTip, ', ');
  78. return view('auth.register', [
  79. 'policy_1' => $policy_1,
  80. 'policy_2' => $policy_2,
  81. 'passwordGuideTip' => $passwordGuideTip
  82. ]);
  83. }
  84. /**
  85. * Get a validator for an incoming registration request.
  86. *
  87. * @param array $data
  88. * @return \Illuminate\Contracts\Validation\Validator
  89. */
  90. protected function validator(array $data)
  91. {
  92. return Validator::make($data, [
  93. 'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email', new DeniedEmail],
  94. 'password' => ['required', 'string', 'min:' . config('password_min_length', 4), 'confirmed', new NumberLength, new SpecialCharLength, new UppercaseLength],
  95. 'nickname' => ['required', 'string', 'min:2', 'max:20', new AllowNickname],
  96. 'agree_1' => 'required|numeric|in:1',
  97. 'agree_2' => 'required|numeric|in:2'
  98. ], [], [
  99. 'email' => '이메일',
  100. 'password' => '비밀번호',
  101. 'nickname' => '닉네임',
  102. 'agree_1' => '이용약관 동의',
  103. 'agree_2' => '개인정보처리방침 동의'
  104. ]);
  105. }
  106. /**
  107. * Create a new user instance after a valid registration.
  108. *
  109. * @param array $data
  110. * @return \App\Models\User
  111. */
  112. protected function create(array $data): mixed
  113. {
  114. return (new User)->register($data);
  115. }
  116. /**
  117. * Handle a registration request for the application.
  118. *
  119. * @param \Illuminate\Http\Request $request
  120. * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
  121. */
  122. public function register(Request $request)
  123. {
  124. // 회원가입 차단 확인
  125. if (config('use_register_block')) {
  126. return back()->withErrors('현재 회원 신청이 차단되어 회원가입을 할 수 없습니다. 관리자에게 문의하십시오.');
  127. }
  128. $postData = $request->all();
  129. // 회원가입 유효성 검증
  130. $this->validator($postData)->validate();
  131. $user = $this->create($postData);
  132. $this->guard()->login($user); // 로그인 처리
  133. $user->markEmailAsVerified(); // 이메일 인증 처리
  134. // 회원가입 이메일 알림
  135. $this->registered($request, $user);
  136. $message = sprintf('%s 님 회원가입을 환영합니다.', $user->name ?? $user->nickname);
  137. return $request->wantsJson() ? new JsonResponse([], 201) : redirect($this->redirectPath())->with('message', $message);
  138. }
  139. /**
  140. * 이메일 검증 주소 전송
  141. * @method POST
  142. * @see /auth/register/sendVerifyLink
  143. */
  144. public function sendVerifyLink(Request $request, ResponseData $response): ResponseData
  145. {
  146. try {
  147. $email = $request->post('email');
  148. if (!$email) {
  149. throw new Exception('이메일을 입력해주세요.');
  150. }
  151. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  152. throw new Exception('이메일 형식이 아닙니다.');
  153. }
  154. // 중복 여부
  155. if ((new User)->where('email', $email)->exists()) {
  156. throw new Exception('이미 사용 중인 이메일입니다.');
  157. }
  158. // 유효성 확인
  159. if (!(new DeniedEmail)->passes(null, $email)) {
  160. throw new Exception('입력하신 이메일은 사용하실 수 없습니다.');
  161. }
  162. $token = sha1($email);
  163. $verifyExpireTime = Carbon::now()->addMinutes(VERIFY_EXPIRES_AT);
  164. $verifyLink = URL::temporarySignedRoute(
  165. 'auth.register.verifyEmail', $verifyExpireTime, [
  166. 'token' => $token
  167. ]
  168. );
  169. // 인증 메일 전송
  170. Mail::to($email)->send(new VerifyLink($verifyLink, VERIFY_EXPIRES_AT, $email));
  171. // 인증 메일 캐시 저장
  172. Cache::put('verifyEmailToken_' . $token, $token, $verifyExpireTime);
  173. Cache::put('verifyEmailStatus_' . $token, 0);
  174. return $response;
  175. } catch (Exception $e) {
  176. return $response::fromException($e);
  177. }
  178. }
  179. /**
  180. * 이메일 검증 확인
  181. * @method GET
  182. * @see /auth/register/verifyEmail/{token}
  183. */
  184. public function verifyEmail(Request $request)
  185. {
  186. if (!$request->hasValidSignature()) {
  187. abort(404);
  188. }
  189. $token = (string)$request->route('token');
  190. if (!hash_equals((string)Cache::get('verifyEmailToken_' . $token), $token)) {
  191. abort(403);
  192. }
  193. Cache::put('verifyEmailStatus_' . $token, 1);
  194. return alertClose('이메일 인증이 완료되었습니다.');
  195. }
  196. /**
  197. * 이메일 인증 여부 확인
  198. * @method GET
  199. * @see /auth/register/checkVerifiedEmail
  200. */
  201. public function checkVerifiedEmail(Request $request): string
  202. {
  203. return json_encode([
  204. 'success' => intval(Cache::get('verifyEmailStatus_' . sha1($request->post('email'))))
  205. ]);
  206. }
  207. /**
  208. * 회원가입 후 처리
  209. */
  210. public function registered(Request $request, $user)
  211. {
  212. $this->sendMessageToRegister($user);
  213. }
  214. }