I could login successfully using graphql endpoint from postman, but after setting up a form the laravel backend, and access it through a browser, it doesn't login the user in.
What could be wrong please? See my set-up below:
RegisteredUserController
class RegisteredUserController extends Controller
{
/**
* Handle an incoming registration request.
*
* @throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request): Response
{
$rules = [
'firstname' => ['required', 'string', 'max:255'],
'lastname' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:'.User::class],
'password' => ['required', 'confirmed', 'max:40', Rules\Password::min(6)
->letters()
->mixedCase()
->numbers()
->symbols()
->uncompromised()
]
];
$validator = Validator::make($request->all(),$rules);
if($validator->fails()) {
throw ValidationException::withMessages(['error'=>$validator->errors()]);
// return response()->json(['error'=>$validator->errors()], 401);
}
try {
$user = User::create([
'lastname' => $request->lastname,
'firstname' => $request->firstname,
'email' => $request->email,
'password' => Hash::make($request->password),
'role' => json_encode(config('constants.roles.user'))
]);
event(new Registered($user));
Auth::login($user);
return response()->noContent();
} catch(\Exception $e){
// return redirect()->back()->with('error','Something goes wrong while uploading file!');
throw new CustomException(
config('constants.errors.invalid_upload').' Something went wrong',
$e
// config('constants.errors.error_reason')
);
}
}
}
login.blade.php
<form action="{{ route('login') }}" method="POST">
@csrf
<div class="modal-body">
<div class="form-group required">
<label for="name">Email</label>
<input type="email" class="form-control" id="name" name="email" placeholder="Email">
</div>
<div class="form-group required">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="password">
</div>
<div class="form-group required">
<label for="password">Remember Me</label>
<input class="form-check-input" type="checkbox" name="remember" id="flexCheckChecked" checked>
</div>
<div class="form-group required">
<input type="hidden" class="form-control" id="api" name="api_secret" value="{{env('API_SECRET')}}" >
</div>
</div>
<div class="modal-footer mt-3">
<button type="submit" class="btn btn-success">Login</button>
</div>
</form>
Note:
The Laravel app has the below modifications as per scaffolding
- I installed Auth Starter kit (breeze)
composer require laravel/breeze --dev
- Implemented API scaffold
php artisan breeze:install api
I suspect that the above may be the culprit.
Actually, I wish to use the web (blade templates) for all admin management actions on the server. However, since graphql requests work so well, I am considering using the endpoint for admin actions as well.