Send confirmation mail with user registration - laravel

I try to register user with send connfirmation mail at the samme time.but the page is reloaded there is no data stored in db and also not sending the confirmation email.
RegisterController.php
protected function create(array $data)
{
return User::create([
'title' => $data['title'],
'fname' => $data['fname'],
'lname' => $data['lname'],
'company' => $data['company'],
'email' => $data['email'],
'phone' => $data['phone'],
'people' => $data['people'],
'sdate' => $data['sdate'],
'notes' => $data['note'],
'password' => Hash::make($data['password']),
]);
}
public function register(Request $request)
{
//dd($request->all());
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
Mail::to($user->email)->send(new ConfirmationEmail($user));
return back()->with('status','Please confirm your email address...');
// $this->guard()->login($user);
// return $this->registered($request, $user)
// ?: redirect($this->redirectPath());
}
//confirmation email code
public function confirmEmail($token)
{
User::whereToken($token)->firstOrFail()->confirmEmail();
return redirect('/user/login')->with('status', 'You are now confirmed. Please login.');
}

Related

How do I fill in the mail verification field?

When I register a member at Laravel, I send a confirmation email. But when I click on the verification link, the column in the database does not fill.
Sample codes:
protected function create(array $data)
{
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone' => $data['phone'],
'password' => Hash::make($data['password']),
]);
$user->sendEmailVerificationNotification();
return $user;
}
public function register(Request $request) {
$validation = $this->validator($request->all());
if($request->input('type') == "customer" || $request->input('type') == "company") {
if ($validation->fails()) {
return response()->json([$validation->errors()->toArray()], 400);
}else {
$this->create($request->all());
}
}else {
return response()->json([
0 => ['Üyelik sırasında bir hata oluştu. Lütfen tekrar deneyiniz.']
], 404);
}
}
1) Have you migrated the tables after running the auth boilerplate?
2) Have you checked your logs for any additional errors that might be showing?

Auto login after register in laravel 5.7

I want when I finished register it redirects me to the dashboard page (Auto login after the register).
this is the user register code :
public function store(Request $request)
{
$this->validator($request->all())->validate();
$apprenant = Apprenant::create([
'nom' => $request['nom'],
'prenom' => $request['prenom'],
'email' => $request['email'],
'niveau' => $request['niveau'],
'password' => Hash::make($request['password']),
]);
return redirect('/apprenant/dashboard');
}
But when I finished registring it redirects me to the login page
Hi if you are create a guard then run this code for after register login
Auth::guard(guard_name)->loginUsingId($id);
// example
public function register(Request $req)
{
$user = new User; // define here your model
$user->name = $req->name;
$user->email = $req->email;
$user->password = Hash::make($req->password);
if($user->save()){
Auth::guard(guard_name)->loginUsingId($user->id);
}
}
// logout overite
public function logout()
{
Auth::guard('guard_name')->logout();
return redirect("login_path");
}
When creating a new user, create() method, should return a new model object.
Use Auth::loginUsingId($apprenant->id); before redirecting to dashboard:
public function store(Request $request)
{
$this->validator($request->all())->validate();
$apprenant = Apprenant::create([
'nom' => $request['nom'],
'prenom' => $request['prenom'],
'email' => $request['email'],
'niveau' => $request['niveau'],
'password' => Hash::make($request['password']),
]);
Auth::loginUsingId($apprenant->id);
return redirect('/apprenant/dashboard');
}
source
https://laravel.com/docs/5.7/authentication#other-authentication-methods
Write this line before redirecting to the dashboard:
\Auth::login($apprenant);
or just,
auth()->login($apprenant);
That means, you code will look like:
public function store(Request $request)
{
$this->validator($request->all())->validate();
$apprenant = Apprenant::create([
'nom' => $request['nom'],
'prenom' => $request['prenom'],
'email' => $request['email'],
'niveau' => $request['niveau'],
'password' => Hash::make($request['password']),
]);
//login the user
\Auth::login($apprenant);
return redirect('/apprenant/dashboard');
}

How can i update my form with validation in laravel?

My form validation is not working in Laravel. How can I update my form with validation in Laravel?
You can check my code here-
public function update(Request $request, $id)
{
$id->validate([
'Name'=>'required',
'UserName'=>'required',
'Password'=>'required|min:6',
'email'=>'required|email',
]);
$updateInfo= Info::findOrFail($id);
$updateInfo->user_id = $request->input('user_id');
$updateInfo->Name = $request->input('Name');
$updateInfo->UserName = $request->input('UserName');
$updateInfo->Password = $request->input('Password');
$updateInfo->save();
return redirect('/info');
}
You need to call validate on $request, like this-
$request->validate([
'Name'=>'required',
'UserName'=>'required',
'Password'=>'required|min:6',
'email'=>'required|email',
]);
Here is the full code-
public function update(Request $request, $id)
{
$request->validate([
'Name'=>'required',
'UserName'=>'required',
'Password'=>'required|min:6',
'email'=>'required|email',
]);
if (!$validator->fails()) {
$updateInfo= Info::findOrFail($id);
$updateInfo->user_id = $request->input('user_id');
$updateInfo->Name = $request->input('Name');
$updateInfo->UserName = $request->input('UserName');
$updateInfo->Password = $request->input('Password');
$updateInfo->save();
} else {
\Session::flash('error', $validator->messages()->first());
return redirect()->back()->withInput();
}
return redirect('/info');
}
I have added one more condition in the code to handle the validation errors. If validation fails then it will redirect back with your inputs as well as the validation error messages. Make sure you have error session flash in your blade views to show the errors.
For me this is best way , i can keep on track on query and other exceptions by putting it in try catch block
public function update(Request $request, $id)
{
try{
$validator = Validator::make($request->all(), [
'name' => 'required',
'UserName' => 'required',
'Password' => 'required',
'email' => 'required|email',
]);
if($validator->fails()) {
return redirect()
->route('path_to_edit_form')
->withErrors($validator)
->withInput();
}
Info::where('id',$id)->update([
'user_id' => $request->get('user_id'),
'Name' => $request->get('Name'),
'UserName' => $request->get('UserName'),
'Password' => $request->get('Password'),
]);
return back()->with([
'alert_type' => 'success',
'message' => 'User info updated successfully.'
]);
}catch(\Exception $e){
return back()->with([
'alert_type' => 'danger',
'message' => $e->getMessage()
]);
}
}

how to logout from laravel 5.6

I want to log out after register. how do I do that?
This is my register controller:
protected function create(array $data)
{
$user = User::create([
'firstname' => $data['firstname'],
'secondname' => $data['secondname'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'address' => $data['address'],
'mobileno' => $data['mobileno'],
'type' => $data['type'],
]);
$verifyUser = VerifyUser::create([
'user_id' => $user->id,
'token' => str_random(40)
]);
Mail::to($user->email)->send(new VerifyMail($user));
return $user;
return redirect('/login')->with('status', 'We sent you an activation code. Check your email and click on the link to verify.');
}
You can change the url :-
protected $redirectTo = '/where/you/want/to/redirect';
after registration in app/Http/Controller/Auth/RegisterController.php
and for logout:-
public function __construct()
{
$this->middleware('guest')->except('logout');
}
Add this two line in your controller and check
public function __construct()
{
$this->middleware('auth');
}

how to sign in or sign up with one auth for all user ?

public function postSignIn(Request $request)
{
$this->validate($request, [
'email' => 'required',
'password' => 'required'
]);
if (Auth::attempt(['email' => $request['email'], 'password' => $request['password'], 'active' => 1]))
{
return redirect()->route('dashboard');
}
return redirect()->back()->with('message', 'Email address and Password mismatch or account not yet activated.');
}
i want access to open my project with one auth for all users
if soemoene know to method can you tell me

Resources