-
-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74c9b48
commit ad269fd
Showing
11 changed files
with
194 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php namespace Flarum\Api\Actions\Auth; | ||
|
||
use Event; | ||
use Response; | ||
use Auth; | ||
|
||
use Flarum\Core\Users\User; | ||
use Flarum\Api\Actions\Base; | ||
|
||
class Login extends Base | ||
{ | ||
/** | ||
* Log in and return a token. | ||
* | ||
* @return Response | ||
*/ | ||
protected function run() | ||
{ | ||
$identifier = $this->input('identifier'); | ||
$password = $this->input('password'); | ||
$field = filter_var($identifier, FILTER_VALIDATE_EMAIL) ? 'email' : 'username'; | ||
$credentials = [$field => $identifier, 'password' => $password]; | ||
|
||
if (! Auth::attempt($credentials)) { | ||
return $this->respondWithError('invalidLogin', 401); | ||
} | ||
|
||
$token = Auth::user()->getRememberToken(); | ||
|
||
return Response::json(compact('token')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,35 @@ | ||
<?php | ||
namespace Codeception\Module; | ||
|
||
// here you can define custom actions | ||
// all public methods declared in helper class will be available in $I | ||
use Laracasts\TestDummy\Factory; | ||
use Auth; | ||
use DB; | ||
|
||
class ApiHelper extends \Codeception\Module | ||
{ | ||
public function haveAnAccount($data = []) | ||
{ | ||
return Factory::create('Flarum\Core\Users\User', $data); | ||
} | ||
|
||
public function login($identifier, $password) | ||
{ | ||
$this->getModule('REST')->sendPOST('/api/auth/login', ['identifier' => $identifier, 'password' => $password]); | ||
|
||
$response = json_decode($this->getModule('REST')->grabResponse(), true); | ||
if ($response && is_array($response) && isset($response['token'])) { | ||
return $response['token']; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function amAuthenticated() | ||
{ | ||
$user = $this->haveAnAccount(); | ||
$user->groups()->attach(3); // Add member group | ||
Auth::onceUsingId($user->id); | ||
|
||
return $user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
use \ApiTester; | ||
|
||
use Laracasts\TestDummy\Factory; | ||
|
||
class AuthCest | ||
{ | ||
protected $endpoint = '/api/auth'; | ||
|
||
public function loginWithEmail(ApiTester $I) | ||
{ | ||
$I->wantTo('login via API with email'); | ||
|
||
$user = $I->haveAnAccount([ | ||
'email' => 'foo@bar.com', | ||
'password' => 'pass7word' | ||
]); | ||
|
||
$token = $I->login('foo@bar.com', 'pass7word'); | ||
$I->seeResponseCodeIs(200); | ||
$I->seeResponseIsJson(); | ||
|
||
$loggedIn = User::where('remember_token', $token)->first(); | ||
$I->assertEquals($user->id, $loggedIn->id); | ||
} | ||
|
||
public function loginWithUsername(ApiTester $I) | ||
{ | ||
$I->wantTo('login via API with username'); | ||
|
||
$user = $I->haveAnAccount([ | ||
'username' => 'tobscure', | ||
'password' => 'pass7word' | ||
]); | ||
|
||
$token = $I->login('tobscure', 'pass7word'); | ||
$I->seeResponseCodeIs(200); | ||
$I->seeResponseIsJson(); | ||
|
||
$loggedIn = User::where('remember_token', $token)->first(); | ||
$I->assertEquals($user->id, $loggedIn->id); | ||
} | ||
|
||
public function invalidLogin(ApiTester $I) | ||
{ | ||
$user = $I->haveAnAccount([ | ||
'email' => 'foo@bar.com', | ||
'password' => 'pass7word' | ||
]); | ||
|
||
$I->login('foo@bar.com', 'incorrect'); | ||
$I->seeResponseCodeIs(401); | ||
$I->seeResponseIsJson(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
<?php | ||
|
||
$factory('Flarum\Core\Discussions\Discussion', [ | ||
'title' => $faker->sentence | ||
'title' => $faker->sentence, | ||
'start_time' => $faker->dateTimeThisYear, | ||
'start_user_id' => 'factory:Flarum\Core\Users\User' | ||
]); | ||
|
||
$factory('Flarum\Core\Users\User', [ | ||
'username' => $faker->sentence | ||
'username' => $faker->userName, | ||
'email' => $faker->safeEmail, | ||
'password' => 'password', | ||
'join_time' => $faker->dateTimeThisYear, | ||
'time_zone' => $faker->timezone | ||
]); |