2019-11-30 18:19:13 +01:00
< ? php
namespace App\Http\Controllers ;
use App\Http\Requests\LoginRequest ;
use App\Http\Requests\RegisterRequest ;
use App\Repositories\UsersRepositoryInterface ;
use Illuminate\Http\JsonResponse ;
use Illuminate\Http\Request ;
use Illuminate\Validation\ValidationException ;
use JWTAuth ;
use Tymon\JWTAuth\Exceptions\JWTException ;
use Validator , DB , Hash , Mail ;
class UsersController extends Controller
{
2019-12-05 15:19:09 +01:00
private $usersRepository ;
2019-11-30 18:19:13 +01:00
public function __construct ( UsersRepositoryInterface $usersRepository )
{
$this -> usersRepository = $usersRepository ;
}
public function register ( RegisterRequest $request )
{
$email = $request -> input ( 'email' );
$password = $request -> input ( 'password' );
$this -> usersRepository -> create ([
'email' => $email ,
'password' => bcrypt ( $password ),
]);
$token = auth () -> attempt ( $request -> only ([ 'email' , 'password' ]));
return response () -> json ([
'success' => true ,
'data' => [ 'token' => $token ]
]);
}
/**
* API Login , on success return JWT Auth token
*
* @ param LoginRequest $request
* @ return JsonResponse
*/
public function login ( LoginRequest $request )
{
if ( ! $request -> validated ()) {
return response () -> json ([ 'success' => false , 'error' => $request -> messages ()], 401 );
}
try {
if ( ! $token = auth () -> attempt ( $request -> only ( 'email' , 'password' ))) {
return response () -> json ([ 'success' => false , 'error' => 'We cant find an account with this credentials. Please make sure you entered the right information and you have verified your email address.' ], 404 );
}
} catch ( JWTException $e ) {
// something went wrong whilst attempting to encode the token
return response () -> json ([ 'success' => false , 'error' => 'Failed to login, please try again.' ], 500 );
}
// all good so return the token
return response () -> json ([ 'success' => true , 'data' => [ 'token' => $token ]], 200 );
}
/**
* Log out
* Invalidate the token , so user cannot use it anymore
* They have to login again to get a new token
*
* @ param Request $request
* @ return JsonResponse
* @ throws ValidationException
*/
public function logout ( Request $request )
{
$this -> validate ( $request , [ 'token' => 'required' ]);
try {
auth () -> invalidate ( $request -> input ( 'token' ));
return response () -> json ([ 'success' => true , 'message' => " You have successfully logged out. " ]);
} catch ( JWTException $e ) {
return response () -> json ([ 'success' => false , 'error' => 'Failed to logout, please try again.' ], 500 );
}
}
public function me ()
{
$requestedBy = auth () -> user ();
return response () -> json ([
'success' => true , 'data' => $requestedBy
]);
}
2020-01-07 19:45:45 +01:00
public function saveRecipe ( Request $request )
{
$userID = auth () -> id ();
$recipeID = $request -> input ( 'recipeID' );
$savedRecipe = $this -> usersRepository -> saveRecipe ( $userID , $recipeID );
if ( ! $savedRecipe ) {
return response () -> json ([ 'success' => false , 'error' => 'Failed to save recipe, please try again.' ], 400 );
}
return response () -> json ([ 'success' => true , 'message' => 'Recipe saved successfully in history' ]);
}
2020-01-19 14:53:23 +01:00
public function getRecipeHistory ( Request $request )
2020-01-19 14:02:14 +01:00
{
$userID = auth () -> id ();
2020-01-19 14:53:23 +01:00
$groupBy = $request -> input ( 'groupBy' ) ? ? 'day' ;
$recipeHistory = $this -> usersRepository -> getRecipeHistory ( $userID , $groupBy );
2020-01-19 14:02:14 +01:00
return response () -> json ([ 'success' => true , 'data' => [ 'history' => $recipeHistory ]], 200 );
}
2019-11-30 18:19:13 +01:00
}