I'm facing problem in my Laravel 5.3 custom auth want to use my own functions or pages when I check Auth::check()
it returns false
.
Here is User controller:
namespace App\Http\Controllers;
use App\User;
use Illuminate\Support\Facades\Session;
use validatior;
use Auth;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function postSignUp(Request $request)
{
$validation = \Validator::make($request->all(), [
'email' => 'email|required|unique:users',
'password' => 'required|min:4'
]);
if ($validation->fails()) {
return redirect()->back()->withErrors($validation->errors());
} else {
$user = new User();
$user->email = $request->get("email");
$user->password = bcrypt($request->get['password']);
$user->save();
}
return redirect('signupPage');
}
public function postSignIn(Request $request)
{
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);
if (Auth::attempt(['email' => $request['email'], 'password' => $request['password']])) {
return redirect('users/profile');
}
dd(Auth::check());
exit;
}
}
After sign in I want to redirect at profile page but my condition is not working. My routes are:
Route::group(['prefix' => 'users'], function(){
Route::group(['middleware' => 'guest'], function(){
Route::get('/signupPage','UserController@getSignUp');
Route::post('/register',array ('as'=>'signup', 'uses' =>'UserController@postSignUp', ));
Route::get('signInPage', array('as' => 'signInPage', 'uses' => 'UserController@getSignIn'));
Route::post('/postLogin', array('as' => 'postLogin', 'uses' => 'UserController@postSignIn'));
});
Route::group(['middleware' => 'auth'], function(){
Route::get('/profile', array('as' => 'profile', 'uses' => 'UserController@getProfile'));
Route::get('/logout', array('as'=>'logout', 'uses'=> 'UserController@logout'));
});
});
Here are some Modifications of your code
public function postSignUp(Request $request) { $email = $request->input('email'); $pass = $request->input('password'); $validation = \Validator::make($request->all(), [ 'email' => 'email|required|unique:users', 'password' => 'required|min:4' ]); if ($validation->fails()) { return redirect()->back()->withErrors($validation->errors()); } else { $pas = bcrypt($pass); $user = new User(); $user->email = $email; $user->password = $pas; $user->save(); } return redirect('users/signInPage'); }
Do use bcrypt while registering your User as this function is by default used by Auth
Attempt
function
public function postSignIn(Request $request) { $this->validate($request, [ 'email' => 'required', 'password' => 'required', ]); $email= $request->input['email']; $password= $request->input['password']; if (Auth::attempt(['email'=>$email,'password'=>$password'])) { return redirect('users/profile'); } return redirect('users/signInPage')->withErrors(['failure' => 'The credentials you entered did not match our records. Try again!',]); }
finally this will give you too many redirects error as you using default middlewares, because your logic is now differ from the default one that's why you have to write your custom middleware, for this run this command
php artisan make:middleware CutomAuth
this will create your custom middleware now write code in it by your logic and after registering in app/Http/Kernel.php
as \n
'customauth' => \Illuminate\Auth\Middleware\CutomAuth::class,
you are good to go
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With