mirror of
https://github.com/Bubka/2FAuth.git
synced 2024-11-08 01:14:29 +01:00
API User details and logout
This commit is contained in:
parent
24588e5d26
commit
bf029bc741
@ -11,7 +11,10 @@
|
||||
class UserController extends Controller
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* log a user in
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
$credentials = [
|
||||
@ -30,6 +33,25 @@ public function login()
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* log out current user
|
||||
* @param Request $request
|
||||
* @return json
|
||||
*/
|
||||
public function logout()
|
||||
{
|
||||
$accessToken = Auth::user()->token();
|
||||
$accessToken->revoke();
|
||||
|
||||
return response()->json(['success' => 'signed out']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* register new user
|
||||
* @param Request $request [description]
|
||||
* @return json
|
||||
*/
|
||||
public function register(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
@ -53,8 +75,12 @@ public function register(Request $request)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get detailed information about a user
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function getDetails()
|
||||
{
|
||||
return response()->json(['success' => Auth::user()]);
|
||||
return response()->json(Auth::user(), 200);
|
||||
}
|
||||
}
|
@ -16,11 +16,9 @@
|
||||
Route::post('login', 'UserController@login');
|
||||
Route::post('register', 'UserController@register');
|
||||
|
||||
Route::middleware('auth:api')->get('/user', function (Request $request) {
|
||||
return $request->user();
|
||||
});
|
||||
|
||||
Route::group(['middleware' => 'auth:api'], function(){
|
||||
Route::post('logout', 'UserController@logout');
|
||||
Route::get('user', 'UserController@getDetails');
|
||||
Route::apiResource('twofaccounts', 'TwoFAccountController');
|
||||
Route::get('twofaccounts/{twofaccount}/totp', 'TwoFAccountController@generateTOTP')->name('twofaccounts.generateTOTP');
|
||||
Route::delete('twofaccounts/force/{id}', 'TwoFAccountController@forceDestroy')->name('twofaccounts.forceDestroy');
|
||||
|
@ -3,10 +3,12 @@
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\User;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Auth\Authenticatable;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class UserTest extends TestCase
|
||||
{
|
||||
@ -41,9 +43,10 @@ public function testUserCreation()
|
||||
'password' => 'test',
|
||||
]);
|
||||
|
||||
$response->assertStatus(200)->assertJsonStructure([
|
||||
'success' => ['token', 'name']
|
||||
]);
|
||||
$response->assertStatus(200)
|
||||
->assertJsonStructure([
|
||||
'success' => ['token', 'name']
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@ -59,9 +62,53 @@ public function testUserLogin()
|
||||
'password' => 'test'
|
||||
]);
|
||||
|
||||
$response->assertStatus(200)->assertJsonStructure([
|
||||
'success' => ['token']
|
||||
]);
|
||||
$response->assertStatus(200)
|
||||
->assertJsonStructure([
|
||||
'success' => ['token']
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* test User logout via API
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testUserLogout()
|
||||
{
|
||||
$user = ['email' => 'test@test.com',
|
||||
'password' => 'test'
|
||||
];
|
||||
|
||||
Auth::attempt($user);
|
||||
$token = Auth::user()->createToken('testToken')->accessToken;
|
||||
$headers = ['Authorization' => "Bearer $token"];
|
||||
|
||||
$response = $this->json('POST', '/api/logout', [], $headers)
|
||||
->assertStatus(200)
|
||||
->assertJson([
|
||||
'success' => 'signed out',
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* test User logout via API
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetUserDetails()
|
||||
{
|
||||
$user = \App\User::find(1);
|
||||
|
||||
$response = $this->actingAs($user, 'api')
|
||||
->json('GET', '/api/user')
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'id' => 1,
|
||||
'name' => 'testLogin',
|
||||
'email' => 'test@test.com',
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user