This is fresh laravel 9 Project, I didn't touch anything about auth but it is not working.
Route::get('test',function (){
$user = \App\Models\User::create([
'name' => fake()->name(),
'email' => 'a#d.com',
'email_verified_at' => now(),
'password' => bcrypt('password'), // password
]);
dd($user);
});
When i hit /test route, I got new user record.
But when i hit /test2, it returns false
Route::get('test2',function (){
$user = auth()->attempt([
'email' => 'a#d.com',
'password' => 'password'
]);
dd($user);
});
And my config/auth.php is same as default laravel 9 config/auth.php
Any thought?
Laravel encrypts your password by default, so you don't need to encrypt it anymore.
Use this:
$user = \App\Models\User::create([
'name' => fake()->name(),
'email' => 'a#d.com',
'email_verified_at' => now(),
'password' => 'password', // password
]);
Related
i am trying to upload an image as a profile picture in laravel bootstrap auth package.
in this i am trying to change some package files to upload image. also i added a column in users table.
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'campus_id' => $data['campus_id'],
'role' => $data['role'],
'remarks' => $data['remarks'],
'image' => $data['image'],
]);
}
i make changes in Auth controller in validation function
also makes some changes in user store function
I think you need to move user profile image before create its entry inside database.
protected function create(array $data)
{
$imageName = time().'.'.$data['image']->extension();
//$data['image']->move(public_path('images'), $imageName);
$data['image']->storeAs('public/images', $imageName);
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'campus_id' => $data['campus_id'],
'role' => $data['role'],
'remarks' => $data['remarks'],
'image' => $imageName,
]);
}
You can use Image intervention for this. After installing you can use it in your controller as use Image;
$image = $request->file('image');
$img_name = hexdec(uniqid()).'.'.$image->getClientOriginalExtension();
Image::make($image)->resize( 847.5, 431 )->save('uploads/sliders/'.$img_name);
$image_path = 'uploads/sliders/'.$img_name;
Slider::create([
'title' => $request->title,
'image' => $image_path,
'created_at' => Carbon::now()
]);
1st you need to move your image to your desired directory inside public folder and save that directory in the database.
I have some data i am receiving from new users and extracting the email to send to the new user. This is how i am doing it
public function register_mechanic_post(Request $request)
{
$validatedData = $request->validate([
'email' => 'required|email|unique:users',
'password' => 'required',
'password_confirmation' => 'required'
], [
'email.required' => 'Email address is required',
'password.required' => 'Password field is required',
'password_confirmation.required' => 'Password confirmation field is required'
]);
$data = $request->all();
$name = $request->input('name');
$data['role'] = 'manager';
$email = $request->input('email');
User::create([
'email' => $request->input('email'),
'name' => $request->input('name'),
'role' => 'manager',
'password' => Hash::make($request->input('password')),
//'email_verified_at' => now()
]);
$user = User::where('email','=',$email)->first();
$user->sendEmailVerificationNotification();
return back()->with('success', 'Mechanic created successfully.');
}
I am getting this error
403 THIS ACTION IS UNAUTHORIZED
The docs say its because of signed urls https://laravel.com/docs/9.x/urls#signed-urls
I haven't modified the existing email verification code as shipped with laravel. How do i use the signed urls feature in my case?.
Not an answer, but your code could be significantly simpler, making it easier to manage in the future.
public function register_mechanic_post(Request $request)
{
$validatedData = $request->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required',
'password_confirmation' => 'required'
], [
'email.required' => 'Email address is required',
'password.required' => 'Password field is required',
'password_confirmation.required' => 'Password confirmation field is required'
]);
$user = User::create([
'email' => $validatedData['email'],
'name' => $validatedData['name'],
'role' => 'manager',
'password' => Hash::make($validatedData['password']),
'email_verified_at' => now()
]);
$user->sendEmailVerificationNotification();
return back()->with('success', 'Mechanic created successfully.');
}
But why ask the user to verify their email when you are already setting the email_verified_at timestamp (indicating that verification has been performed)
I have a error, I'm using sanctum and I want to check that the email does not exist
the if returns an empty array but the if is satisfied because it returns true
$mail = $request->input(['email']);
if ($search = User::where('email', $mail)->get()) {
return response()->json(['msg' => 'account already exist'], 409);
} else {
$validate = $request->validate([
'name' => 'required|string|',
'email' => 'required|string',
'password' => 'required|string'
]);
}
any solution?
Why not use the Laravel validation since this looks more like validation, so something like:
$request->validate([
'name' => 'required|string|',
'email' => 'required|string|email|unique:users,email',
'password' => 'required|string'
]);
with this you don't need to do an if else. You can check the Laravel docs on https://laravel.com/docs/8.x/validation#introduction for more details
I would like to use the reset password mailable for my project but I do not know how to access it in laravel 6.1
Here is my method
public function store(Request $request)
{
$validatedData = $request->validate([
'address_id' => 'required',
'name'=> 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'POBox' =>['required', 'min:6', 'max:6'],
'role_id' => 'required',
]);
$quickpass = substr( str_shuffle( str_repeat( 'abcdefghijklmnopqrstuvwxyz0123456789', 10 ) ), 0, 10 );
$newuser = User::create([
'address_id' =>$request->address_id,
'name'=> $request->name,
'email' => $request->email,
'POBox'=> $request->POBox,
'password' => Hash::make($quickpass),
'role_id' => $request->role_id,
]);
Mail::to($newuser->email)
->send( );
return view('admin.index')->with('message','The user has been created and a password reset email has been sent to them.');
}
The Notification used is Illuminate\Auth\Notifications\ResetPassword. It builds a MailMessage inline.
The PasswordBroker can be used to create the token and send off the notification for you. The method sendResetLink will take an array of credentials to find the User by.
Illuminate\Auth\Passwords\PasswordBroker
$resp = Illuminate\Support\Facades\Password::broker()->sendResetLink([
'email' => '...',
]);
I am getting started with lumen.
this is my web.php file
$router->group(['prefix' => 'api'], function () use ($router) {
$router->post('user', ['uses' => 'UserController#createUser']);
});
and this is my UserController.php
public function createUser(Request $request){
$this->validate($request, [
'username' => 'required',
'password' => 'required|min:3'
]);
$user = [
'username' => $request->username,
'password' => app('hash')->make($request->password),
'email' => $request->email,
'api_token' => 'asdfasd' //want should I put here
];
$user = User::create($user);
return response()->json($user, 201);
}
How do I create a new api token for that a new user who is registering?