I'm new to Laravel and trying to send an e-mail after a user signed up via a form. I'm using the default authentication files shipped with Laravel and modified them. For example: I want only to require an unique emailadress for registration, then send the credentials and automaticly login the new user.
For maybe future purposes I'm also working with the language files where I store my translations.
I've customized the AuthController to also accept a MailerContract and within the postRegistration function I'm sending the e-mail like below:
/**
* Handle a registration request for the application.
*
* #param \Illuminate\Foundation\Http\FormRequest $request
* #return \Illuminate\Http\Response
*/
public function postRegister(Request $request)
{
$validator = $this->registrar->validator($request->all());
if ($validator->fails())
{
$this->throwValidationException(
$request, $validator
);
}
$input = $request->all();
$input['password'] = str_random(8);
$this->auth->login($this->registrar->create($input));
$this->mailer->send('emails.welcome', ['email' => $input['email'], 'password' => $input['password']], function($message) use($input) {
$message->to($input['email'])->subject(trans('emails.welcome:subject'));
});
return redirect($this->redirectPath());
}
I've noticed that my subject-value is not translated. How to use language files in the Controllers/Traits? Doesn't Laravel pick this up from the view?
As asked, a part of the language file: (/resources/lang/nl/emails.php)
<?php
return [
'welcome:subject' => 'Uw inloggegevens' // Dutch for Your login credentials
];
You need to use multidimensional arrays in /resources/lang/nl/emails.php:
return [
'welcome' => [
'subject' => 'Uw inloggegevens',
],
];
And use dot notation instead of colon in the trans function:
$message->to($input['email'])->subject(trans('emails.welcome.subject'));
I ran into another thing with localization and asked a question about that on SO. Seems like using a middleware is the solution to this, because that sets the correct language in the app.
With thanks to #lukasgeiter, the answer can be found here: Localization with Laravel5 without domain.com/language/
Related
I'm using laravel breeze as auth scaffolding package I want to know How can I
create two diffirent registration form for two User Types here is a simple explanation of hwat I want to achieve:
resources/auth/developer :
developer-register.blade.php
resources/auth/designer :
designer-register.blade.php
if the Visitor choose to register as "developer" it will display a diffirent form. and same thing for if the Visitor choose to register as "designer" it will display a diffirent form with fields.
I wish you understand what I want to achieve with this easy explanation.
Ok, so i've not used laravel/breeze myself (yet) but it shouldn't be much different from doing it in standard Laravel!
Views
By default, it looks like the breeze scaffolding is going to hit a create() method on the RegisteredUserController which will return a single view like so:
RegisteredUserController.php
/**
* Display the registration view.
*
* #return \Illuminate\View\View
*/
public function create()
{
return view('auth.register');
}
You have a few options here:
Replace this view with another
Add some logic to change the view which is returned based on the request being made (you can inject a Request object into the route like any other)
public function create(Request $request)
{
if ($request->has('developer')) {
return view('auth.developer-register');
} else {
return view('auth.designer-register');
}
}
Keep the original auth.register view and handle the logic in the blade template.
Registration
The forms on each of your registration pages will have an action that points to a controller route. This will likely be the RegisteredUserController within which you will find a store() method that handles the creation of a User model.
RegisteredUserController.php
/**
* Handle an incoming registration request.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\RedirectResponse
*
* #throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|confirmed|min:8',
]);
Auth::login($user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]));
event(new Registered($user));
return redirect(RouteServiceProvider::HOME);
}
As you can see, this store() method is handling the creation of a User model and then authenticating it before redirecting the user to the home route.
What you could do, is check the request for the the requested user type and then use a switch statement to change the type of use being created.
switch ($request->get('user_type'))
case 'developer':
$user = Developer::create([ /* add details here */ ]);
break;
case 'designer':
$user = Designer::create([ /* add details here */ ]);
break;
Auth::login($user);
I hope this will at least inspire you with your own solution!
Im new at Laravel, i made a login page for my backend application(its working fine) and then i did the JWT documentation to install and begin to use(https://jwt-auth.readthedocs.io/en/develop/).
In my Controllers i used after:
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
Now everytime i try to enter my same login i cant.
How can i enter my login now with JWT?
If you're using Laravel 8, the newly recommended route is to use Laravel's Sanctum feature:
https://laravel.com/docs/8.x/sanctum
For this, if you want stateful logins, you simply post to any controller that then makes an Auth::attempt call like this:
class AuthController extends Controller
{
/**
* Store a newly created resource in storage.
*
* #param Request $request
* #throws ValidationException
*/
public function store(Request $request)
{
$request->validate([
'email' => 'required|email',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if (!Auth::attempt($credentials, true)) {
throw ValidationException::withMessages([
'email' => ['The provided credentials are incorrect.'],
]);
}
}
}
I recently set up my login system successfully on React using this package:
https://github.com/koole/react-sanctum
For Stateless, you can follow the documentation here:
https://laravel.com/docs/8.x/sanctum#api-token-authentication
I am trying to pass a pageTitle variable to the password reset template (/resources/views/auth/passwords/reset.blade.php) in Laravel 5.3 in the following way:
return view('auth.passwords.reset')
->with('pageTitle', 'Change title')
->with(['token' => $token, 'email' => $request->email]);
this goes in the showResetForm method inside ResetsPasswords trait - and it doesn't work. Google doesn't come up with any helpful results. I've tried removing the line:
->with(['token' => $token, 'email' => $request->email]);
but it still doesn't work. I've also tried
$pageTitle = 'Change me';
return view('auth.passwords.reset', compact('pageTitle'));
but it doesn't work. Also, I've realized that the ResetsPassword trait is found in the vendor folder so it's a bad idea to change the code there, how do you suggest I do this instead? Can I overwrite the showResetForm method somewhere? - I found that for the registration trait I can put the showRegistrationForm in the RegistrationController and pass whatever variables I want to the view there; however that doesn't work for the ResetPasswordController
EDIT:
Here is the whole method from the ResetsPasswords trait, as requested:
/**
* Display the password reset view for the given token.
*
* If no token is present, display the link request form.
*
* #param \Illuminate\Http\Request $request
* #param string|null $token
* #return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showResetForm(Request $request, $token = null)
{
return view('auth.passwords.reset')->with(['token' => $token, 'email' => $request->email]);
}
Try using compact
return view('auth.passwords.reset', compact('token', 'email', etc..));
return view('auth.passwords.reset')
->with(['pageTitle' => 'Change Title','token' => $token, 'email' => $request->email]);
have you tried it like this?
EDIT
$pageTitle = 'Change Title';
$token = 'token';
$email = 'email';
return view('auth.passwords.reset', compact('pageTitle','token','email'));
The problem was I was editing the wrong trait, I need to edit the SendsPasswordResetEmails trait and not the ResetsPassword.
I am using built in laravel auth functionality.Its working fine.I am trying to override following two functionality.
1.send forgot password email using mandrill.
2.send verification email while registering account.
Can any one help me to solve this issue
My aim is to use mandril instead of default email
I can see auth built in methods but i didnt got idea how i can override that
trait ResetsPasswords
{
use RedirectsUsers;
/**
* Display the password reset view for the given token.
*
* If no token is present, display the link request form.
*
* #param \Illuminate\Http\Request $request
* #param string|null $token
* #return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showResetForm(Request $request, $token = null)
{
return view('auth.passwords.reset')->with(
['token' => $token, 'email' => $request->email]
);
}
/**
* Reset the given user's password.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function reset(Request $request)
{
$this->validate($request, $this->rules(), $this->validationErrorMessages());
// Here we will attempt to reset the user's password. If it is successful we
// will update the password on an actual user model and persist it to the
// database. Otherwise we will parse the error and return the response.
$response = $this->broker()->reset(
$this->credentials($request), function ($user, $password) {
$this->resetPassword($user, $password);
}
);
// If the password was successfully reset, we will redirect the user back to
// the application's home authenticated view. If there is an error we can
// redirect them back to where they came from with their error message.
return $response == Password::PASSWORD_RESET
? $this->sendResetResponse($response)
: $this->sendResetFailedResponse($request, $response);
}
As answered by Mahfuzal, Laravel comes with a bunch of mail drivers out of the box. So just update your .env file to use the right driver.
As for sending a verification email when creating an account, you just need to override the postRegister() function inside the Auth/AuthController like so:
public function postRegister(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
$confirmation_code = str_random(30);
$newUser = new User;
$newUser->username = $request->username;
$newUser->email = $request->email;
$newUser->password = bcrypt($request->password);
$newUser->confirmation_code = $confirmation_code;
$newUser->save();
$data = array('confirmation_code' => $confirmation_code, 'username' => $request->username);
Mail::send('emails.verify', $data, function ($message) use ($newUser){
$message->to($newUser->email, $newUser->username);
$message->subject('Please verify your email address');
});
return redirect('/auth/login');
}
This will execute the above code when registering a user rather than what Laravel does default out of the box so just tweak it to your needs.
You then just need to create a function that will check the token and verify their account when they click the link. For that, I use something similar to what is explained here.
Laravel provides drivers for SMTP, Mailgun, Mandrill, Amazon SES,
PHP's mail function, and sendmail, allowing you to quickly get started
sending mail through a local or cloud based service of your choice.
Open your .env file and change following by your Mandrill credentials and then you're good to go.
MAIL_DRIVER=mandrill
MAIL_HOST=
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
You can create your own reset method in the controller that uses the trait to override the method in the trait.
I've created a simple contact form in Laravel 5, using the Request object and a Validator object to check my input for errors.
The form in my view is coded in HTML, rather than the Laravel Form object, which isn't included by default in Laravel 5.
I need to set up my form so that if a validation rule fails, the user's input is flashed to the session so it doesn't disappear when the page redirects. I was able to accomplish this by putting a $request->flash() in the POST controller, before the validation code.
However, I do not want the data to be flashed (i.e, the form should be reset) if the validation passes and the form is successfully emailed. There's no apparent way for me to accomplish this in the $this->validate block, since Laravel helpfully handles the redirects automatically.
How can I tell Laravel to flash the form data ONLY if there is a validation error?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ContactController extends Controller
{
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
return view('contact');
}
/**
* Store a newly created resource in storage.
*
* #param Request $request
* #return Response
*/
public function store(Request $request)
{
// Flash current input in case the validator fails and redirects
$request->flash();
// Validate the form request, redirect on fail
$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'subject' => 'required',
'message' => 'required|min:5',
]);
// Generate email from template and send
\Mail::send('emails.feedback',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'user_message' => $request->get('message'),
'subject' => $request->get('subject')
), function ($message) use ($request) {
$message->from(\Config::get('site.from_email'));
$message->to(\Config::get('site.contact_email'), 'Admin');
$message->subject(\Config::get('site.name') . ': ' . $request->get('subject'));
});
// Redirect to Contact route with success message
return \Redirect::route('contact')
->with('message', 'Thanks for contacting us!');
}
}
?>
If anybody like me didn't know how to get the old values and end up here somehow, here it goes:
<input value="{{ old('var_name') }}">
I don't see you needing to access the session data when Laravel provides you helpers like this.
Hope to have helped, have a nice day. =)
Just remove the following line of code:
$request->flash();
Laravel will take care of that for you by flashing the data on failed validation. The following method gets called on failed validation:
/**
* Create the response for when a request fails validation.
*
* #param \Illuminate\Http\Request $request
* #param array $errors
* #return \Illuminate\Http\Response
*/
protected function buildFailedValidationResponse(Request $request, array $errors)
{
if ($request->ajax() || $request->wantsJson()) {
return new JsonResponse($errors, 422);
}
return redirect()->to($this->getRedirectUrl())
->withInput($request->input()) // <-- Flashes inputs
->withErrors($errors, $this->errorBag()); // <-- Flashes errors
}
This is the trait used in your controller for validating the request and it's located at Illuminate/Foundation/Validation, name is ValidatesRequests. Check it to clarify yourself.
Alternatively, you may do it manually if you want for any reason, check the documentation.
To restore the old value if validation fails the entered data will display the view
value="{{ (old('title')) ? old('title') : $data->title}}