Skip to content

Commit

Permalink
1.0.4 - Ability to define own set of user attributes in response.
Browse files Browse the repository at this point in the history
  • Loading branch information
vdomah committed Dec 24, 2017
1 parent 081e5f7 commit f597f43
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 44 deletions.
7 changes: 7 additions & 0 deletions Plugin.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Vdomah\JWTAuth;

use RainLab\User\Models\User;
use System\Classes\PluginBase;
use App;
use Illuminate\Foundation\AliasLoader;
Expand Down Expand Up @@ -37,5 +38,11 @@ public function boot()

$this->app['router']->middleware('jwt.auth', '\Tymon\JWTAuth\Middleware\GetUserFromToken');
$this->app['router']->middleware('jwt.refresh', '\Tymon\JWTAuth\Middleware\RefreshToken');

User::extend(function($model) {
$model->addDynamicMethod('getAuthApiAttributes', function () {
return [];
});
});
}
}
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,18 @@ Route::post('test', function (\Request $request) {
})->middleware('jwt.auth');
```

Then when making the request set the header "Authorization" to "Bearer `{yourToken}`"
Then when making the request set the header "Authorization" to "Bearer `{yourToken}`"

### How to define own set of user attributes in response?

For sign up and sign in add corresponding methods getAuthApiSignupAttributes or/and getAuthApiSigninAttributes to User model by extending it in your plugin's boot method:

```
User::extend(function($model) {
$model->addDynamicMethod('getAuthApiSignupAttributes', function () use ($model) {
return [
'my-attr' => $model->my_attr,
];
});
});
```
69 changes: 27 additions & 42 deletions routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,60 +22,45 @@

$userModel = JWTAuth::authenticate($token);

$user = [
'id' => $userModel->id,
'name' => $userModel->name,
'surname' => $userModel->surname,
'username' => $userModel->username,
'email' => $userModel->email,
'is_activated' => $userModel->is_activated,
];
if ($userModel->methodExists('getAuthApiSigninAttributes')) {
$user = $userModel->getAuthApiSigninAttributes();
} else {
$user = [
'id' => $userModel->id,
'name' => $userModel->name,
'surname' => $userModel->surname,
'username' => $userModel->username,
'email' => $userModel->email,
'is_activated' => $userModel->is_activated,
];
}
// if no errors are encountered we can return a JWT
return response()->json(compact('token', 'user'));
});

Route::post('refresh', function (\Request $request) {
$token = Request::get('token');

try {
// attempt to refresh the JWT
if (!$token = JWTAuth::refresh($token)) {
return response()->json(['error' => 'could_not_refresh_token'], 401);
}
} catch (Exception $e) {
// something went wrong
return response()->json(['error' => 'could_not_refresh_token'], 500);
}

// if no errors are encountered we can return a new JWT
return response()->json(compact('token'));
});

Route::post('invalidate', function (\Request $request) {
$token = Request::get('token');

try {
// invalidate the token
JWTAuth::invalidate($token);
} catch (Exception $e) {
// something went wrong
return response()->json(['error' => 'could_not_invalidate_token'], 500);
}

// if no errors we can return a message to indicate that the token was invalidated
return response()->json('token_invalidated');
});

Route::post('/signup', function () {
$credentials = Input::only('email', 'password', 'password_confirmation');

try {
$user = UserModel::create($credentials);
$userModel = UserModel::create($credentials);

if ($userModel->methodExists('getAuthApiSignupAttributes')) {
$user = $userModel->getAuthApiSignupAttributes();
} else {
$user = [
'id' => $userModel->id,
'name' => $userModel->name,
'surname' => $userModel->surname,
'username' => $userModel->username,
'email' => $userModel->email,
'is_activated' => $userModel->is_activated,
];
}
} catch (Exception $e) {
return Response::json(['error' => $e->getMessage()], 401);
}

$token = JWTAuth::fromUser($user);
$token = JWTAuth::fromUser($userModel);

return Response::json(compact('token', 'user'));
});
Expand Down
4 changes: 3 additions & 1 deletion updates/version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
1.0.2:
- Compability with OctoberCMS builds > 419 (Laravel 5.5)
1.0.3:
- README.md updated
- README.md updated
1.0.4:
- Ability to define own set of user attributes in response.

0 comments on commit f597f43

Please sign in to comment.