Refactore Profile controllers and routes

This commit is contained in:
Bubka 2020-03-03 22:09:06 +01:00
parent 284a9f75b2
commit 7090cd8b33
8 changed files with 98 additions and 75 deletions

View File

@ -0,0 +1,51 @@
<?php
namespace App\Http\Controllers\Profile;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class AccountController extends Controller
{
/**
* get detailed information about a user
* @return [type] [description]
*/
public function show()
{
return response()->json(Auth::user()->only('name', 'email'), 200);
}
/**
* Update the user's profile information.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function update(Request $request)
{
$user = $request->user();
$this->validate($request, [
'name' => 'required',
'email' => 'required|email|unique:users,email,'.Auth::id(),
'password' => 'required',
]);
if (!Hash::check( $request->password, Auth::user()->password) ) {
return response()->json(['message' => __('errors.wrong_current_password')], 400);
}
tap($user)->update($request->only('name', 'email'));
return response()->json([
'message' => __('auth.forms.profile_saved'),
'username' => $request->name
]);
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Http\Controllers\Profile;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class PasswordController extends Controller
{
/**
* Update the user's password.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function update(Request $request)
{
$this->validate($request, [
'currentPassword' => 'required',
'password' => 'required|confirmed|min:8',
]);
if (!Hash::check( $request->currentPassword, Auth::user()->password) ) {
return response()->json(['message' => __('errors.wrong_current_password')], 400);
}
$request->user()->update([
'password' => bcrypt($request->password),
]);
return response()->json(['message' => __('auth.forms.password_successfully_changed')]);
}
}

View File

@ -1,9 +1,10 @@
<?php
namespace App\Http\Controllers;
namespace App\Http\Controllers\Profile;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class SettingController extends Controller
{

View File

@ -7,7 +7,6 @@
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class UserController extends Controller
{
@ -98,68 +97,4 @@ public function register(Request $request)
return response()->json(['message' => $success]);
}
/**
* get detailed information about a user
* @return [type] [description]
*/
public function getDetails()
{
return response()->json(Auth::user()->only('name', 'email'), 200);
}
/**
* Update the user's profile information.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function update(Request $request)
{
$user = $request->user();
$this->validate($request, [
'name' => 'required',
'email' => 'required|email|unique:users,email,'.Auth::id(),
'password' => 'required',
]);
if (!Hash::check( $request->password, Auth::user()->password) ) {
return response()->json(['message' => __('errors.wrong_current_password')], 400);
}
tap($user)->update($request->only('name', 'email'));
return response()->json([
'message' => __('auth.forms.profile_saved'),
'username' => $request->name
]);
}
/**
* Update the user's password.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function updatePassword(Request $request)
{
$this->validate($request, [
'currentPassword' => 'required',
'password' => 'required|confirmed|min:8',
]);
if (!Hash::check( $request->currentPassword, Auth::user()->password) ) {
return response()->json(['message' => __('errors.wrong_current_password')], 400);
}
$request->user()->update([
'password' => bcrypt($request->password),
]);
return response()->json(['message' => __('auth.forms.password_successfully_changed')]);
}
}

View File

@ -27,7 +27,7 @@
},
async mounted() {
const { data } = await this.form.get('/api/user')
const { data } = await this.form.get('/api/profile/account')
this.form.fill(data)
},
@ -39,7 +39,7 @@
this.fail = ''
this.success = ''
this.form.patch('/api/user', {returnError: true})
this.form.patch('/api/profile/account', {returnError: true})
.then(response => {
this.success = response.data.message

View File

@ -33,7 +33,7 @@
this.fail = ''
this.success = ''
this.form.patch('/api/password', {returnError: true})
this.form.patch('/api/profile/password', {returnError: true})
.then(response => {
this.success = response.data.message

View File

@ -34,7 +34,7 @@
this.fail = ''
this.success = ''
this.form.post('/api/settings', {returnError: true})
this.form.post('/api/profile/settings', {returnError: true})
.then(response => {
this.success = response.data.message

View File

@ -27,12 +27,12 @@
Route::group(['middleware' => 'auth:api'], function() {
Route::post('logout', 'UserController@logout');
Route::patch('password', 'UserController@updatePassword');
Route::patch('user', 'UserController@update');
Route::get('user', 'UserController@getDetails');
Route::get('settings', 'SettingController@index');
Route::post('settings', 'SettingController@store');
Route::get('profile/account', 'Profile\AccountController@show');
Route::patch('profile/account', 'Profile\AccountController@update');
Route::patch('profile/password', 'Profile\PasswordController@update');
Route::get('profile/settings', 'Profile\SettingController@index');
Route::post('profile/settings', 'Profile\SettingController@store');
Route::delete('twofaccounts/batch', 'TwoFAccountController@batchDestroy');
Route::apiResource('twofaccounts', 'TwoFAccountController');