credentials do not match our records - php

I am currently working on POS in laravel 8. I am having an error when I login in that "credentials do not match our records" When I login in through the default Register page than it works fine but when I register through internal panel than the data goes to the database but I face error when I log in.
Code for registration through panel:
public function store(Request $request)
{
$users = new User;
$users->name = $request->name;
$users->email = $request->email;
$users->password = Hash::make($request->name);
// $users->password = bcrypt($request->name);
$users->is_admin = $request->is_admin;
$users->save();
if($users){
return redirect()->back()->with('User Created Successfully');
}
return redirect()->back()->with('User Failed Created');
}
Default register code:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}

Your panel registration form is hashing the name input and not the password input.
$users->password = Hash::make($request->name);
Should probably be
$users->password = Hash::make($request->password);

#Code for registration through panel code should be like this#
public function store(Request $request){
$users = new User;
$users->name = $request->name;
$users->email = $request->email;
$users->password = Hash::make($request->password);
$users->is_admin = $request->is_admin;
$users->save();
if($users){
return redirect()->back()->with('User Created Successfully');
}
return redirect()->back()->with('User Failed Created');
}

Related

Problem with authenticating user using laravel socialite

I'm trying to make login with google using laravel socialite and I have a problem.
Route that initiates login:
Route::get('/auth/login/google', 'AuthController#google');
Method in controller that initiates login:
public function google()
{
return Socialite::driver('google')->redirect();
}
Callback route:
Route::get('/auth/login/google/redirect', 'AuthController#googleRedirect');
Callback method in controller:
public function googleRedirect()
{
$googleUser = Socialite::driver('google')->user();
$email = $googleUser->getEmail();
$user = new User();
$user = $user->firstOrCreate(['email' => $email], ['email' => $email, 'password' =>
bcrypt(str_shuffle('abcdefgh45678')), 'email_verified' => 1]);
Auth::login($user, true);
}
And I'm getting ERR_EMPTY_RESPONSE every time I'm trying to redirect user after login.
Funny thing is that I can dump data with dd(Auth::user()->id) and I'm getting user's ID, but when I try to redirect user to the home page using return redirect('/') I'm getting empty response error and if I manually go to home page my user is not authenticated.
#Matej Petric blow code is working for me.
public function handleProviderCallback($provider) {
$user = Socialite::driver('google')->stateless()->user();
$authUser = $this->findOrCreateUser($user);
if ($authUser) {
Auth::login($authUser, true);
return redirect('/');
} else {
return redirect('/login')->withErrors(['msg', 'The Message']);
}
}
public function findOrCreateUser($user) {
$authUser = User::where('email', $user->email)->first();
if ($authUser) {
return $authUser;
}
$userN = User::create([
'name' => $user->name,
'email' => $user->email,
'password' => bcrypt(generateRandom()),
]);
return $userN;
}

User Roles in Laravel

I want to implement user roles for my controller. When the user is admin or master it works, but when the user is a client it doesn't, and I receive a 205 http response.
This code works nicely in my localhost but it doesn't so in my host.
public function store(Request $request)
{
$validation = Validator::make($request->all(), [
'name' => 'required|string',
'imageone'=> 'required|string'
]);
if ($validation->fails())
{
return response()->json('Fails',404);
}
$user = JWTAuth::parseToken()->authenticate();
if ($user->roles=='admin'||$user->roles=='master') {
$name= $request->input('name');
$imageone = $request->input('imageone');
$cate=new categoryi();
$cate->name=$name;
$cate->imageone=$imageone;
if ($cate->save())
{
return response()->json($cate,202);
}
}
return response()->json('Fails',500);
}

Laravel 5.2 - Login user after create post

i'm doing a project where ALL user can create a post and pubblic it!
if user not logged blade enable a section with email and password to create account and post.
I would like do:
Creation user
Log user in session auth ( HERE IS MY PROBLEM, HOW CAN I DO THIS? )
Create Post (last point because i need user_id)
CONTROLLER
// if user NOT registered my form will enable email and password field --
if(!null($request->input('email') && request->input('password'))) {
// create user
$user = new User();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = bcrypt($request->input('name'));
$user->save();
// login user after create user
HOW CAN I LOG USER AFTER CREATED ACCOUNT ???
Thank you for your help!
// Get ID of user created
$get_user_id = User::where('email', '=', $request('email'))->first();
// create post
$post = new Post();
$post->title = $request->input('title');
$post->slug = str_slug($request->input('title'),'-');
$post->country = $request->input('country');
$post->zone = $request->input('zone');
$post->phone = $request->input('phone');
$post->address = $request->input('address');
$post->user_id = $get_user_id->id;
$post->save();
return redirect('/')->with('message-success', 'Post creato con successo!');
// IF USER LOGGED
}else{
$post = new Post();
$post->title = $request->input('title');
$post->slug = str_slug($request->input('title'),'-');
$post->country = $request->input('country');
$post->zone = $request->input('zone');
$post->phone = $request->input('phone');
$post->address = $request->input('address');
$post->user_id = $request->input('user_id');
$post->save();
return redirect('/')->with('message-success', 'Post creato con successo!');
}
The answer is:
Auth::login($user);
Check out the doc.
Is not so clear what you want to do here, the way you get the user id is insecure.
I'll recommend you to use $user = Auth::user(); instead to get the user id, ($user->id).
to check the login manually use
if (Auth::attempt(['email' => $email, 'password' => $password])) {}
so in your example I'll do something like:
if (!Auth::attempt(['email' => $email, 'password' => $password])) {
// create User
} else {
// $userId = Auth::user()->id;
}
// finally create post
Add a __construct() method to your class, and add following code:
public function __construct() {
$this->middleware('auth', ['except' => ['index', 'show']]);
}
And it must actually work

Why Laravel 5 hashes password twice?

I set hash mutator for User model:
public function setPasswordAttribute($value)
{
\Log::info('Hash');
$this->attributes['password'] = \Hash::make($value);
}
Then I have a register function. The password hashes twice (checked in logs)
public function register(RegisterNewUser $request)
{
$user = new User();
$user->first_name = $request->first_name;
$user->last_name = $request->last_name;
$user->email = $request->email;
$user->password = $request->password;
$user->admin = true;
$user->save();
$this->auth->login($user);
return redirect()->route('dashboard');
}
Actually the function was originally written like this:
$user = new User($request->all());
$user->admin = true;
$user->save();
With the same result. Password gets hashed twice and I can't figure why.
I even tried:
get('test', function() {
$user = \CRM\EloquentModels\User::create([
'email' => 'zzz#zzz.zzz',
'last_name' => 'zzz',
'password' => 123
]);
});
And it gets hashed twice. No idea...

Unexpected T Variable Laravel

I'm getting an error on the following on:
$user->email = Input::get('email');
I'm really unsure what is wrong with the code, it seems perfectly fine. I looked up t variable errors, simply involve missing a bracket or semi colon. But as far as I'm aware it seems fine.
If anyone could help me out, that would be great.
If there is any other code, could you list it as a comment and i'll happily add it.
Thanks!
public function doRegister()
{
$rules = array(
'name' => 'required|min:3', // name
'email' => 'required|email', // make sure the email is an actual email
'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
);
// run the validation rules on the inputs from the form
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()){
// validation not successful, send back to form
Redirect::back()->withErrors;
} else {
$user = Input::all();
User::addNewUser();
if (Auth::attempt($user)) {
return Redirect::to('member');
}
}
}
User model
public static function addNewUser()
{
$user = new User;
$user->name     = Input::get('name');
$user->email    = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
}
It's because of $user->save; it's a method not a property and it should be called like
$user->save();
Instead of
$user->save;
Update : Also, it's U not u
$user = new user;
should be
$user = new User; // capital U
Also, after if ($validator->fails())
Redirect::back()->withErrors;
should be
return Redirect::back()->withErrors($validator);
Update : So, after fixing 3 errors (so far), your full code should be
public function doRegister()
{
$rules = array(
'name' => 'required|min:3',
'email' => 'required|email',
'password' => 'required|alphaNum|min:3'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()){
return Redirect::back()->withErrors($validator);
}
else {
$user = new User;
$user->name =Input::get('name');
$user->email= Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
if (Auth::attempt($user)) {
return Redirect::to('member');
}
}
}

Categories