mirror of
https://github.com/devfake/flox.git
synced 2024-11-15 06:32:34 +01:00
implement auth
This commit is contained in:
parent
62d1546a8d
commit
e05dd39cda
40
backend/app/Http/Controllers/AuthController.php
Normal file
40
backend/app/Http/Controllers/AuthController.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Flox\Http\Controllers;
|
||||||
|
|
||||||
|
use Flox\Http\Requests\LoginRequest;
|
||||||
|
use Illuminate\Contracts\Auth\Guard;
|
||||||
|
|
||||||
|
class AuthController extends Controller {
|
||||||
|
|
||||||
|
private $auth;
|
||||||
|
|
||||||
|
public function __construct(Guard $auth)
|
||||||
|
{
|
||||||
|
$this->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()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -5,19 +5,11 @@
|
|||||||
use Flox\Item;
|
use Flox\Item;
|
||||||
use Flox\Category;
|
use Flox\Category;
|
||||||
use Flox\Http\Controllers\Controller;
|
use Flox\Http\Controllers\Controller;
|
||||||
use Illuminate\Support\Facades\Auth;
|
|
||||||
use Illuminate\Support\Facades\Request;
|
use Illuminate\Support\Facades\Request;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
class FloxController extends Controller {
|
class FloxController extends Controller {
|
||||||
|
|
||||||
public function checkLogin()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'logged' => Auth::check()
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function homeItems($category, $orderBy, $loading = 5)
|
public function homeItems($category, $orderBy, $loading = 5)
|
||||||
{
|
{
|
||||||
return $this->getItems($category, $orderBy, $loading);
|
return $this->getItems($category, $orderBy, $loading);
|
||||||
|
30
backend/app/Http/Requests/LoginRequest.php
Normal file
30
backend/app/Http/Requests/LoginRequest.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Flox\Http\Requests;
|
||||||
|
|
||||||
|
class LoginRequest extends Request {
|
||||||
|
|
||||||
|
public function authorize()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Login rules.
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'username' => 'required',
|
||||||
|
'password' => 'required',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Login error.
|
||||||
|
*/
|
||||||
|
public function response(array $errors)
|
||||||
|
{
|
||||||
|
return response('Login error', 422);
|
||||||
|
}
|
||||||
|
}
|
@ -15,7 +15,9 @@
|
|||||||
|
|
||||||
Route::group(['prefix' => 'api'], function() {
|
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('all-categories', 'FloxController@allCategories');
|
||||||
Route::get('home-items/{category}/{orderBy}/{loading?}', 'FloxController@homeItems');
|
Route::get('home-items/{category}/{orderBy}/{loading?}', 'FloxController@homeItems');
|
||||||
|
@ -16,6 +16,8 @@ class User extends Model implements AuthenticatableContract,
|
|||||||
{
|
{
|
||||||
use Authenticatable, Authorizable, CanResetPassword;
|
use Authenticatable, Authorizable, CanResetPassword;
|
||||||
|
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The database table used by the model.
|
* The database table used by the model.
|
||||||
*
|
*
|
||||||
|
@ -6,6 +6,10 @@ class Api extends React.Component {
|
|||||||
return $.get(config.api + 'check-login');
|
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() {
|
static categories() {
|
||||||
return $.get(config.api + 'all-categories');
|
return $.get(config.api + 'all-categories');
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ class Flox extends React.Component {
|
|||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Header logged={this.state.logged} />
|
<Header logged={this.state.logged} />
|
||||||
{React.cloneElement(this.props.children, {logged: this.state.logged})}
|
{React.cloneElement(this.props.children, {logged: this.state.logged, checkLogin: this.checkLogin.bind(this)})}
|
||||||
<Footer />
|
<Footer />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
@ -12,7 +12,7 @@ class Auth extends React.Component {
|
|||||||
return (
|
return (
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
{this.props.logged ? <Admin /> : <Login />}
|
{this.props.logged ? <Admin /> : <Login checkLogin={this.props.checkLogin} />}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
);
|
);
|
||||||
|
@ -9,7 +9,8 @@ class Login extends React.Component {
|
|||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
username: '',
|
username: '',
|
||||||
password: ''
|
password: '',
|
||||||
|
error: ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,6 +25,7 @@ class Login extends React.Component {
|
|||||||
<form className="login-form" onSubmit={this.onSubmit.bind(this)}>
|
<form className="login-form" onSubmit={this.onSubmit.bind(this)}>
|
||||||
<input type="text" className="login-input" placeholder="Username" ref="username" value={this.state.username} onChange={this.setUsername.bind(this)} />
|
<input type="text" className="login-input" placeholder="Username" ref="username" value={this.state.username} onChange={this.setUsername.bind(this)} />
|
||||||
<input type="password" className="login-input" placeholder="Password" value={this.state.password} onChange={this.setPassword.bind(this)} />
|
<input type="password" className="login-input" placeholder="Password" value={this.state.password} onChange={this.setPassword.bind(this)} />
|
||||||
|
<span className="login-error">{this.state.error}</span>
|
||||||
<input type="submit" value="Login" className="login-submit" />
|
<input type="submit" value="Login" className="login-submit" />
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
@ -38,10 +40,20 @@ class Login extends React.Component {
|
|||||||
let password = this.state.password;
|
let password = this.state.password;
|
||||||
|
|
||||||
if( ! username || ! 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) {
|
setUsername(event) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
.login-wrap {
|
.login-wrap {
|
||||||
width: 300px;
|
width: 400px;
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,4 +33,12 @@
|
|||||||
border: none;
|
border: none;
|
||||||
padding: 8px 25px;
|
padding: 8px 25px;
|
||||||
margin: 20px 0 0 0;
|
margin: 20px 0 0 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.login-error {
|
||||||
|
float: left;
|
||||||
|
clear: both;
|
||||||
|
color: #c64b4b;
|
||||||
|
font-size: 15px;
|
||||||
|
margin: 10px 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user