Skip to content

Commit

Permalink
회원 수정, 탈퇴 추가 (#67)
Browse files Browse the repository at this point in the history
* Update users migration, nickname unique

* Update users nickname

* Update UserResource

* Fix UserController namespace

* Create user language(localization)

* Create User Update&Destroy

초안 작성

* Update user destroy, provider disconnect

* Update user edit

* Fix return type&comment
  • Loading branch information
kimchanhyung98 authored Apr 4, 2024
1 parent c3907d3 commit a59dae0
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 33 deletions.
6 changes: 2 additions & 4 deletions app/Http/Controllers/Account/AppleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ class AppleController extends Controller
public const string PROVIDER = 'apple';

/**
* SocialLogin (Apple)
*
* @return AccessTokenResource
* 소셜로그인 (애플)
*/
public function __invoke(Request $request)
public function __invoke(Request $request): AccessTokenResource
{
$this->getToken($request->redirect_uri);

Expand Down
6 changes: 2 additions & 4 deletions app/Http/Controllers/Account/KakaoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ class KakaoController extends Controller
public const string PROVIDER = 'kakao';

/**
* SocialLogin (Kakao)
*
* @return AccessTokenResource
* 소셜로그인 (카카오)
*/
public function __invoke(Request $request)
public function __invoke(Request $request): AccessTokenResource
{
DB::beginTransaction();
try {
Expand Down
8 changes: 3 additions & 5 deletions app/Http/Controllers/Account/SignInController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
class SignInController extends Controller
{
/**
* Sign in
*
* @return AccessTokenResource
* 로그인
*/
public function __invoke(SignInRequest $request)
public function __invoke(SignInRequest $request): AccessTokenResource
{
try {
$user = User::where('email', $request->email)->firstOrFail();
Expand All @@ -26,7 +24,7 @@ public function __invoke(SignInRequest $request)
}
} catch (Exception $e) {
logger($e);
abort(401, 'invalid credentials');
abort(401, __('user.signin_denied'));
}

return new AccessTokenResource(
Expand Down
13 changes: 5 additions & 8 deletions app/Http/Controllers/Account/SignUpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,24 @@
class SignUpController extends Controller
{
/**
* Sign up
*
* @return AccessTokenResource
* 회원 가입
*/
public function __invoke(SignUpRequest $request)
public function __invoke(SignUpRequest $request): AccessTokenResource
{
DB::beginTransaction();
try {
DB::beginTransaction();
$user = User::firstOrCreate([
'email' => $request->email,
], [
'name' => $request->name,
'nickname' => $request->nickname ?? $request->name,
'nickname' => $request->nickname,
'password' => Hash::make($request->password),
]);

if (! $user->wasRecentlyCreated) {
abort(409, 'already exists');
abort(409, __('user.signup_duplicate_email'));
}
// $user->sendEmailVerificationNotification();

DB::commit();
} catch (Exception $e) {
DB::rollBack();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\Account;
namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Http\Resources\Account\UserResource;
Expand All @@ -9,12 +9,10 @@
class UserController extends Controller
{
/**
* Handle the incoming request.
* 회원 정보 확인
*/
public function __invoke()
public function __invoke(): UserResource
{
return new UserResource(
Auth::user()
);
return new UserResource(Auth::user());
}
}
65 changes: 65 additions & 0 deletions app/Http/Controllers/User/UserDestroyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Http\Resources\MessageResource;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class UserDestroyController extends Controller
{
/**
* 회원 탈퇴
*/
public function __invoke(Request $request): MessageResource
{
$user = Auth::user();
if (! $user) {
abort(401, __('user.unauthorized'));
}

try {
DB::beginTransaction();
$this->revoke($user, $request->deleted_reason);
DB::commit();
} catch (Exception $e) {
DB::rollBack();
logger($e);
abort($e->getCode(), __('user.destroy_denied'));
}

return new MessageResource([
'message' => __('user.destroy'),
]);
}

/**
* 소셜 로그인 탈퇴 처리
*/
private function revoke($user, $reason): void
{
if ($user->provider === 'apple') {
$user->apple->delete();
// @todo : Apple 로그인 탈퇴 처리
} elseif ($user->provider === 'kakao') {
$user->kakao->delete();
/*
Http::withHeaders(['Authorization' => 'KakaoAK '.config('services.kakao.admin_key')])
->asForm()->throw()
->post('https://kapi.kakao.com/v1/user/unlink', [
'target_id_type' => 'user_id',
'target_id' => $user->provider_id,
]);
*/
} else {
$user->update(['password' => null]);
}

$user->update(['deleted_reason' => $reason]);
$user->tokens()->delete();
$user->delete();
}
}
44 changes: 44 additions & 0 deletions app/Http/Controllers/User/UserUpdateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Http\Resources\MessageResource;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserUpdateController extends Controller
{
/**
* 회원 정보 수정
*/
public function __invoke(Request $request): MessageResource
{
$user = Auth::user();
if (! $user) {
abort(401, __('user.unauthorized'));
}

try {
DB::beginTransaction();
$user->update([
'name' => $request->name,
'nickname' => $request->nickname,
'email' => $request->email,
// 'password' => $request->password ? Hash::make($request->password) : $user->password,
]);
DB::commit();
} catch (Exception $e) {
DB::rollBack();
logger($e);
abort($e->getCode(), __('user.update_denied'));
}

return new MessageResource([
'message' => __('user.update'),
]);
}
}
2 changes: 1 addition & 1 deletion app/Http/Requests/Account/SignUpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function rules(): array
return [
'email' => ['required', 'email', 'unique:users,email', 'max:100'],
'name' => ['required', 'string', 'max:50'],
'nickname' => ['nullable', 'string', 'max:50'],
'nickname' => ['required', 'string', 'unique:users,nickname', 'max:50'],
'password' => ['required', 'string', 'min:8', 'max:100'],
];
}
Expand Down
1 change: 0 additions & 1 deletion app/Http/Resources/Account/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class UserResource extends JsonResource
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'nickname' => $this->nickname,
'email' => $this->email,
Expand Down
14 changes: 14 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace App\Models;

use App\Models\User\UserApple;
use App\Models\User\UserKakao;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
Expand All @@ -19,6 +22,7 @@ class User extends Authenticatable
*/
protected $fillable = [
'name',
'nickname',
'email',
'password',
];
Expand All @@ -42,4 +46,14 @@ class User extends Authenticatable
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

public function apple(): HasOne
{
return $this->HasOne(UserApple::class);
}

public function kakao(): HasOne
{
return $this->hasOne(UserKakao::class);
}
}
3 changes: 2 additions & 1 deletion database/migrations/2014_10_12_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function up(): void
Schema::create('users', static function (Blueprint $table) {
$table->id();
$table->string('name', 50);
$table->string('nickname', 50)->nullable();
$table->string('nickname', 50)->unique();
$table->string('email', 200)->nullable()->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password', 100)->nullable();
Expand All @@ -27,6 +27,7 @@ public function up(): void
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
$table->string('deleted_reason', 200)->nullable();
});
}

Expand Down
13 changes: 13 additions & 0 deletions lang/ko/user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [

'signup_denied' => '회원 가입 중 오류가 발생했습니다.',
'signup_duplicate_email' => '이미 가입한 이메일 주소입니다.',
'signin_denied' => '로그인 중 오류가 발생했습니다.',
'update' => '회원 정보가 수정되었습니다.',
'update_denied' => '회원 정보 수정 중 오류가 발생했습니다.',
'destroy' => '회원 탈퇴가 완료되었습니다.',
'destroy_denied' => '회원 탈퇴 처리 중 오류가 발생했습니다.',

];
8 changes: 5 additions & 3 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
use App\Http\Controllers\Account\KakaoController;
use App\Http\Controllers\Account\SignInController;
use App\Http\Controllers\Account\SignUpController;
use App\Http\Controllers\Account\UserController;
use App\Http\Controllers\PostController;
use App\Http\Controllers\User\UserController;
use App\Http\Controllers\User\UserDestroyController;
use App\Http\Controllers\User\UserUpdateController;
use Illuminate\Support\Facades\Route;

/*
Expand All @@ -32,8 +34,8 @@

Route::middleware('auth:sanctum')->group(static function () {
Route::get('/', UserController::class);
// Route::put('/', UserUpdateController::class);
// Route::delete('/', UserDeleteController::class);
Route::put('/', UserUpdateController::class);
Route::delete('/', UserDestroyController::class);
});
});

Expand Down

0 comments on commit a59dae0

Please sign in to comment.