From e05dd39cda06c76b966f482bfbf176df0ee18b6b Mon Sep 17 00:00:00 2001 From: devfake Date: Thu, 25 Feb 2016 15:44:40 +0100 Subject: [PATCH] implement auth --- .../app/Http/Controllers/AuthController.php | 40 +++++++++++++++++++ .../app/Http/Controllers/FloxController.php | 8 ---- backend/app/Http/Requests/LoginRequest.php | 30 ++++++++++++++ backend/app/Http/routes.php | 4 +- backend/app/User.php | 2 + client/app/api.js | 4 ++ client/app/app.js | 2 +- client/app/sites/auth/auth.js | 2 +- client/app/sites/auth/login.js | 18 +++++++-- client/assets/sass/_auth.scss | 10 ++++- 10 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 backend/app/Http/Controllers/AuthController.php create mode 100644 backend/app/Http/Requests/LoginRequest.php diff --git a/backend/app/Http/Controllers/AuthController.php b/backend/app/Http/Controllers/AuthController.php new file mode 100644 index 0000000..01c8eec --- /dev/null +++ b/backend/app/Http/Controllers/AuthController.php @@ -0,0 +1,40 @@ +auth = $auth; + } + + public function login(LoginRequest $request) + { + $username = $request->input('username'); + $password = $request->input('password'); + + if($this->auth->attempt(['username' => $username, 'password' => $password], true)) { + return response('Success', 200); + } + + return response('Unauthorized', 401); + } + + public function logout() + { + $this->auth->logout(); + } + + public function checkLogin() + { + return [ + 'logged' => $this->auth->check() + ]; + } + } diff --git a/backend/app/Http/Controllers/FloxController.php b/backend/app/Http/Controllers/FloxController.php index 656ff4b..16258ab 100644 --- a/backend/app/Http/Controllers/FloxController.php +++ b/backend/app/Http/Controllers/FloxController.php @@ -5,19 +5,11 @@ use Flox\Item; use Flox\Category; use Flox\Http\Controllers\Controller; - use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Request; use Illuminate\Support\Str; class FloxController extends Controller { - public function checkLogin() - { - return [ - 'logged' => Auth::check() - ]; - } - public function homeItems($category, $orderBy, $loading = 5) { return $this->getItems($category, $orderBy, $loading); diff --git a/backend/app/Http/Requests/LoginRequest.php b/backend/app/Http/Requests/LoginRequest.php new file mode 100644 index 0000000..4af4b00 --- /dev/null +++ b/backend/app/Http/Requests/LoginRequest.php @@ -0,0 +1,30 @@ + 'required', + 'password' => 'required', + ]; + } + + /** + * Login error. + */ + public function response(array $errors) + { + return response('Login error', 422); + } + } \ No newline at end of file diff --git a/backend/app/Http/routes.php b/backend/app/Http/routes.php index edcd326..4b3564a 100644 --- a/backend/app/Http/routes.php +++ b/backend/app/Http/routes.php @@ -15,7 +15,9 @@ Route::group(['prefix' => 'api'], function() { - Route::get('check-login', 'FLoxController@checkLogin'); + Route::post('login', 'AuthController@login'); + Route::get('logout', 'AuthController@logout'); + Route::get('check-login', 'AuthController@checkLogin'); Route::get('all-categories', 'FloxController@allCategories'); Route::get('home-items/{category}/{orderBy}/{loading?}', 'FloxController@homeItems'); diff --git a/backend/app/User.php b/backend/app/User.php index 76b0179..9b429e3 100644 --- a/backend/app/User.php +++ b/backend/app/User.php @@ -16,6 +16,8 @@ class User extends Model implements AuthenticatableContract, { use Authenticatable, Authorizable, CanResetPassword; + public $timestamps = false; + /** * The database table used by the model. * diff --git a/client/app/api.js b/client/app/api.js index 7c68796..b235686 100644 --- a/client/app/api.js +++ b/client/app/api.js @@ -6,6 +6,10 @@ class Api extends React.Component { return $.get(config.api + 'check-login'); } + static login(username, password) { + return $.post(config.api + 'login', {username, password, _token: $('meta[name="csrf_token"]').attr('content')}) + } + static categories() { return $.get(config.api + 'all-categories'); } diff --git a/client/app/app.js b/client/app/app.js index b938cb7..0a6b2a6 100644 --- a/client/app/app.js +++ b/client/app/app.js @@ -25,7 +25,7 @@ class Flox extends React.Component { return (
- {React.cloneElement(this.props.children, {logged: this.state.logged})} + {React.cloneElement(this.props.children, {logged: this.state.logged, checkLogin: this.checkLogin.bind(this)})}
); diff --git a/client/app/sites/auth/auth.js b/client/app/sites/auth/auth.js index 2a96edd..8527699 100644 --- a/client/app/sites/auth/auth.js +++ b/client/app/sites/auth/auth.js @@ -12,7 +12,7 @@ class Auth extends React.Component { return (
- {this.props.logged ? : } + {this.props.logged ? : }
); diff --git a/client/app/sites/auth/login.js b/client/app/sites/auth/login.js index 8a1d466..cf74d0a 100644 --- a/client/app/sites/auth/login.js +++ b/client/app/sites/auth/login.js @@ -9,7 +9,8 @@ class Login extends React.Component { this.state = { username: '', - password: '' + password: '', + error: '' } } @@ -24,6 +25,7 @@ class Login extends React.Component {
+ {this.state.error}
@@ -38,10 +40,20 @@ class Login extends React.Component { let password = this.state.password; if( ! username || ! password) { - return; + return this.setState({ + error: 'Username and Password are required' + }); } - alert("submit"); + Api.login(username, password).done((value) => { + this.props.checkLogin(); + }).fail((value) => { + if(value.status === 422 || value.status === 401) { + this.setState({ + error: 'Login not correct' + }); + } + }); } setUsername(event) { diff --git a/client/assets/sass/_auth.scss b/client/assets/sass/_auth.scss index 19447bc..c6fa6c1 100644 --- a/client/assets/sass/_auth.scss +++ b/client/assets/sass/_auth.scss @@ -1,5 +1,5 @@ .login-wrap { - width: 300px; + width: 400px; margin: 0 auto; } @@ -33,4 +33,12 @@ border: none; padding: 8px 25px; margin: 20px 0 0 0; +} + +.login-error { + float: left; + clear: both; + color: #c64b4b; + font-size: 15px; + margin: 10px 0; } \ No newline at end of file