Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Merge pull request #14 from stormpath/HEAD
Browse files Browse the repository at this point in the history
Version 0.1.0.RC2
  • Loading branch information
bretterer committed Dec 23, 2015
2 parents 071116d + 8e09324 commit 7ccb02c
Show file tree
Hide file tree
Showing 13 changed files with 651 additions and 7 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ Change Log

All library changes, in descending order.

Version 0.1.0.RC2
--------------------

**Released on December 23, 2015.**

- Added JSON Responses to Login, Register, Change Password Routes.
- Add ability to specify what Accept methods you want to allow via config


Version 0.1.0.RC1
--------------------

Expand Down
8 changes: 8 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ Change Log

All library changes, in descending order. something

Version 0.1.0.RC2
--------------------

**Released on December 23, 2015.**

- Added JSON Responses to Login, Register, Change Password Routes.
- Add ability to specify what Accept methods you want to allow via config

Version 0.1.0.RC1
--------------------

Expand Down
5 changes: 5 additions & 0 deletions docs/upgrading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Upgrade Guide
This page contains specific upgrading instructions to help you migrate between
Stormpath-Laravel releases.

Version 0.1.0.RC1 -> Version 0.1.0.RC2
--------------------------------------
Republish vendor config file.
Specify what Accept header you want to respond to in config.

Version 0.1.0.alpha2 -> Version 0.1.0.RC1
-----------------------------------------
Begin installing the package as stormpath/laravel instead of stormpath/laravel-auth
Expand Down
34 changes: 32 additions & 2 deletions src/Http/Controllers/ChangePasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,17 @@ public function getChangePassword()

public function postChangePassword()
{
$newPassword = $this->request->get('password');
$token = $this->request->get('spToken');
$newPassword = $this->request->input('password');
$token = $this->request->input('spToken');

$validator = $this->loginValidator();

if($validator->fails()) {

if($this->request->wantsJson()) {
return $this->respondWithError('Validation Failed', 400, ['validatonErrors' => $validator->errors()]);
}

return redirect()
->to(config('stormpath.web.changePassword.uri').'?spToken='.$token)
->withErrors($validator);
Expand All @@ -81,10 +86,16 @@ public function postChangePassword()
try {
$application->resetPassword($token, $newPassword);

if($this->request->wantsJson()) {
return $this->respondOk();
}
return redirect()
->to(config('stormpath.web.changePassword.nextUri'));

} catch (\Stormpath\Resource\ResourceError $re) {
if($this->request->wantsJson()) {
return $this->respondWithError($re->getMessage(), $re->getStatus());
}
return redirect()
->to(config('stormpath.web.changePassword.errorUri'))
->withErrors(['errors'=>[$re->getMessage()]]);
Expand Down Expand Up @@ -119,4 +130,23 @@ private function loginValidator()
return $validator;
}

private function respondOk()
{
return response()->json();
}

private function respondWithError($message, $statusCode = 400, $extra = [])
{
$error = [
'errors' => [
'message' => $message
]
];

if(!empty($extra)) {
$error['errors'] = array_merge($error['errors'], $extra);
}
return response()->json($error, $statusCode);
}

}
112 changes: 110 additions & 2 deletions src/Http/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,24 @@ class LoginController extends Controller
*/
public function __construct(Request $request, Validator $validator)
{
$this->middleware('stormpath.produces');
$this->request = $request;
$this->validator = $validator;
}

public function getLogin()
{
if($this->request->wantsJson()) {
return $this->respondWithForm();
}

$status = $this->request->get('status');

return view( config('stormpath.web.login.view'), compact('status') );
}

public function postLogin()
{

$validator = $this->loginValidator();

if($validator->fails()) {
Expand All @@ -73,7 +77,12 @@ public function postLogin()
}

try {
$result = $this->authenticate($this->request->get('login'), $this->request->get('password'));
$result = $this->authenticate($this->request->input('login'), $this->request->input('password'));

if($this->request->wantsJson()) {
$account = $result->getAccessToken()->getAccount();
return $this->respondWithAccount($account);
}

return redirect()
->intended(config('stormpath.web.login.nextUri'))
Expand Down Expand Up @@ -103,6 +112,11 @@ public function postLogin()
);

} catch (\Stormpath\Resource\ResourceError $re) {

if($this->request->wantsJson()) {
return $this->respondWithError($re->getMessage(), $re->getStatus());
}

return redirect()
->to(config('stormpath.web.login.uri'))
->withErrors(['errors'=>[$re->getMessage()]])
Expand Down Expand Up @@ -137,4 +151,98 @@ private function loginValidator()

return $validator;
}

private function respondWithForm()
{
$application = app('stormpath.application');
$accountStoreArray = [];
$accountStores = $application->getAccountStoreMappings();
foreach($accountStores as $accountStore) {
$store = $accountStore->accountStore;
$provider = $store->provider;
$accountStoreArray[] = [
'href' => $store->href,
'name' => $store->name,
'provider' => [
'href' => $provider->href,
'providerId' => $provider->providerId,
'clientId' => $provider->clientId
]
];
}

$data = [
'form' => [
'fields' => [
[
'label' => 'Username or Email',
'name' => 'login',
'placeholder' => 'Username or Email',
'required' => true,
'type' => 'text'
],
[
'label' => 'Password',
'name' => 'password',
'placeholder' => 'Password',
'required' => true,
'type' => 'password'
],
[
'label' => 'csrf',
'name' => '_token',
'placeholder' => '',
'value' => csrf_token(),
'required' => true,
'type' => 'hidden'
]
]
],
'accountStores' => [
$accountStoreArray
],

];

return response()->json($data);

}

private function respondWithError($message, $statusCode = 400)
{
$error = [
'errors' => [
'message' => $message
]
];
return response()->json($error, $statusCode);
}

private function respondWithAccount($account)
{
$properties = [];
$blacklistProperties = [
'providerData',
'httpStatus',
'createdAt',
'modifiedAt'
];

$propNames = $account->getPropertyNames();
foreach($propNames as $prop) {
if(in_array($prop, $blacklistProperties)) continue;
$properties[$prop] = $this->getPropertyValue($account, $prop);
}

return response()->json($properties);
}

private function getPropertyValue($account, $propName)
{
if(is_object($account->{$propName})) {
return ['href'=>$account->{$propName}->href];
}

return $account->{$propName};
}
}

0 comments on commit 7ccb02c

Please sign in to comment.