How to use the reset password mailable given by auth command - laravel

I would like to use the reset password mail but in another controller, for I do not know how to code the link in the button for mailtrap or email. So if there is a way of calling the same reset mail password, It would save me some lines. Or if someone could show me how to code that link in another mail function I am okay
Here is the controller I would like to use
public function create()
{
$roles = Role::all();
$addresses = Address::all();
$user = new User();
return view('admin.create', compact('roles', 'addresses', 'user'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validatedData = $request->validate([
'address_id' => 'required',
'name'=> 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'role_id' => 'required',
'description' =>['max:255'],
]);
$quickpass = substr( str_shuffle( str_repeat( 'abcdefghijklmnopqrstuvwxyz0123456789', 10 ) ), 0, 10 );
$newuser = User::create([
'address_id' =>$request->address_id,
'name'=> $request->name,
'email' => $request->email,
'password' => Hash::make($quickpass),
'role_id' => $request->role_id,
'description'=> $request->description,
]);
Mail::to($newuser->email)
->send(new NewUserPassReset());
return view('admin.index')->with('message','The user has been created and a password reset email has been sent to them.');
}
The NewUserPassReset is the mailable I created on my own which has only the message but does not have the link
Right now, I am using mailtrap and above is my admin controller and he is the one who supposed to create user for me
I will kindly appreciate any help

Check out Illuminate\Auth\Notifications\ResetPassword.php.
It has the url that you're looking for:
url(config('app.url').route('password.reset', ['token' => $token, 'email' => $email]))
As you see, you'll need an email and a token to generate the reset password link.
Assuming you know the email, you can obtain the token with the Illuminate\Support\Facades\Password facade:
$token = \Password::createToken($user)
Hope this helps.

Related

How to get mobile number in session of laravel

My project is, after a user come registered redirect to verification page.
public function register(Request $request)
{
Session::put('mobile',$mobile)
$code = rand(10000,99999);
$user = User::create([
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'gender' => $request->gender,
'mobile' => $request->mobile,
//continue code
}
return redirect()->route('verification/?session'.$session);
}
To get the value of Session use
Session::get('mobile')
anywhere in the controller or in blade file or anywhere where Session::class accessible.

Laravel, registration and login on same page. Errors on both forms

I'm working on a website with Laravel and I have the registration and login forms on the same page. The only problem is that if I type the wrong password on the login form the error will show on both forms below the password input.
I've googled this and I have seen some other people with this problem but they are all working on a version below 5.4 and all those solutions are different in version 5.4. Does anyone know what exactly I need to change to make this work?
So far I've changed the names in the forms to 'login_password' and 'register_password', but this only gives me errors.
If you're going to go down the route of changing the input names you'll need to update your LoginController and RegisterController.
Login Controller
You will need to add the following:
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required', 'login_password' => 'required',
]);
}
protected function credentials(Request $request)
{
return [
$this->username() => $request->input($this->username()),
'password' => $request->input('login_password'),
];
}
RegisterController (these methods should already exist in the controller, you'll just need to update password to register_password where applicable)
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'register_password' => 'required|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['register_password']),
]);
}
You will also need to include the Request by putting the following at the top of the file with the other use statements:
use Illuminate\Http\Request;
Hope this helps!

How do I modify my Laravel 5.2 registration to assign new users a role in Laravel 5.3?

I'm new to Laravel, and have been trying to build a user-role authentication system. As 5,3 is very new, there are not many resources that are beginner-friendly. I am using the authentication system created by php artisan make:auth, however I am unsure on how to associate my newly created users with a my roles table.
Initially I had been following this video series on YouTube, but the Authentication controllers differ greatly between 5.2/5.3
I'm hoping that someone can help me figure out what must be done to add in an equivalent what is done in the video to the new controller.
Laravel 5.2 Role Association
public function postSignUp(Request $request)
{
$user = new User();
. . .
$user->save();
$user->roles()->attach(Role::where('name', 'User')->first();
}
From Laravel 5.3 RegisterController.php
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
Thanks for reading.
You can modify the auto-generated Controllers:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$user->roles()->attach(Role::where('name', 'User')->first());
return $user;
}
To attach a role to a user by inserting a record in the intermediate table that joins the models right after creating an account, use the attach method before returning the new User, like this:
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
//attach default user role to user
$user->roles()->attach(Role::where('name', 'User')->first());
return $user;
}

Laravel recaptcha validation in registrar does not work

I am newbie in Laravel.
I've used this recaptcha package: https://github.com/greggilbert/recaptcha
And the documentation saids that:
In your validation rules, add the following:
$rules = array(
// ...
'g-recaptcha-response' => 'required|recaptcha',
};
By the way I use the laravel 5's Registrar:
<?php namespace taxman\Services;
use taxman\User;
use Validator;
use Illuminate\Contracts\Auth\Registrar as RegistrarContract;
class Registrar implements RegistrarContract {
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
public function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:4',
'telephone' => 'required',
'g-recaptcha-response' => 'required|recaptcha',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
But in this case the laravel return with a error message: "The g-recaptcha-response is required", however in the view I used this command:
<div class="form-group">
{!! Recaptcha::render() !!}
</div>
And yes, the field is not empty!
So, I think, I should put the 'g-recaptcha-response' => 'required|recaptcha' somewhere else?
Because it seems, in the Registrar's validator does not work.
if someone is still pulling their hair because of this issue just remove the 'recaptcha' in validation rule.
use
'g-recaptcha-response' => 'required'
instead of
'g-recaptcha-response' => 'required|recaptcha'
see http://tuts.codingo.me/google-recaptcha-in-laravel-application/
if you paste the following in the controller where this action happends it should work.
$this->validate($request,['g-recaptcha-response' => 'required|recaptcha']);
According to the documentation https://developers.google.com/recaptcha/docs/verify, you need to check g-recaptcha-response POST parameter. So pass it to the validator() function.

How to send email after registration in laravel?

I am using the auth system that comes with laravel 5 and would like to send notification email after registration. I tried adding a method that sends email(and it works I tested) in postReigster method of AuthenticatesAndRegistersUsrers.php but it doesn't work.
Please help
The code I tried adding in registrar create and authandregister postRegister
$email = EmailTemplate::all()->first();
Mail::raw($email->topic, function($message) use ($data, $email)
{
$message->from($email->sender, $email->sender);
$message->to($data->email);
});
You must add your code in this file:
/app/Services/Registrar.php
to this function before return you must add something like:
public function create(array $data)
{
your_mail_function($data['email'], 'towhom', 'subject' );
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}

Resources