Recently I have make Laravel auth with composer require laravel/uiand I can edit/create user with MySQL where as auth users for Firebase .
I also can create/edit Users for each database and everything is OK for now, but what I just want is to create a new user form Laravel {RegisterController}
I can create the user with Firebase, but what value or what object shall I return to be compatible with Laravel auth?
Create User with Laravel
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
Create User with FireBase
$userProperties = [
'email' => $data['email'],
'emailVerified' => false,
'password' => Hash::make($data['password']),
'displayName' => $data['name'],
];
$user = new AuthController();
return $user->Create_User($userProperties);
AuthController Class
public function __construct()
{
$this->serviceAccount=ServiceAccount::fromValue(__DIR__.'/FirebaseKey.json');
$this->firebase=(new Factory)->withServiceAccount($this->serviceAccount);
$this->auth=$this->firebase->createAuth();
}
public function Create_User($userProperties=[]){
return $this->auth->createUser($userProperties);
}
return value with dd(); -->User(Laravel)
return value with dd(); -->User(Firebase)
Related
I have a problem with LaraTrust $User->attachRole('user'), and Laravel Auth UI. I want to attach a role upon registering a new User. After the registering, I don't see any data in the roles table.
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'phonenumber' => $data['phonenumber'],
'password' => Hash::make($data['password']),
]);
$User->attachRole('user');
}
You are directly returning the created user before attaching the role.
Either:
$user = User::create([]);
$user->attachRole('user');
return $user;
Or
return User::create([])->attachRole('user');
In my laravel application i'm trying to assign user role upon the registration. Following is my RegistrationController
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'mobile'=>$data['mobile'],
'username'=>$data['username'],
'password' => Hash::make($data['password']),
'propic'=>'user-pic.png',
'user_roles' =>'customer',
]);
$lastdata = DB::table('users')->orderBy('id', 'desc')->first();
$last_id=$lastdata->id;
$user= User::where('id','=',$last_id)->firstOrFail();
$user->assignRole('customer');
}
But when I run the code user get created, activation mail has been sent but the role is not assigning! how can i fix that and where am I doing wrong?
There are a few things you might want to consider.
1) your function won't proceed further after the return statement. You are creating a user with a return statement hence your code below of that won't execute. I'll recommend storing the return object into some variable.
// let's say
$user = User::create([
'name' => $data['name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'mobile'=>$data['mobile'],
'username'=>$data['username'],
'password' => Hash::make($data['password']),
'propic'=>'user-pic.png',
'user_roles' =>'customer',
]);
// then you can simply get it's value by
$user_id = $user->id; // you won't need to query last added row's value.
2) If the user role is not getting saved into your database then check if you have assigned that 'user_roles' field as fillable into User model.
I am currently learning laravel and stuck with an assignment. I have the program where User will have multiple Whiteboards where he can post his notes. What i want to achieve is when User is created it should create 4 different Whiteboards.
This is working for 1 Whiteboard creation but not sure how I can achieve 4 while creating user. I have relationship set up where each user can have multiple Whiteboards and each Whiteboard will have only one User.
protected function create(array $data)
{
$whiteboard = Whiteboard::create([
'username' => $data['username'],
'name' => $data['name'],
]);
return User::create([
'email' => $data['email'],
'password' => bcrypt($data['password']),
'whiteboard_id' => $whiteboard->id,
]);
}
I expect something like this (Just dummy example) -
User A should have whiteboards created with ID 1,2,3,4
User B should have whiteboards created with ID 5,6,7,8
You can do somethings like this,
protected function create(array $data)
{
$user = User::create([
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
for($i=0; $i<=3; $i++)
{
$whiteboard = Whiteboard::create([
'username' => $data['username'],
'name' => $data['name'],
'user_id' => $user->id
]);
}
return $user;
}
In that case you have to change your relationship User (one) Whiteboard (many)
I want to create a separate folder for each user when they register and log them in after the folder creation. I don't know how to do this. I tried something that creates the folder but redirects to the same registration page saying email already exists.(i.e It creates the folder and registers the user in the DB but instead of logging in it tries to register the user again).
protected function create(array $data)
{
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
return \Storage::makeDirectory($data['name']);
//The above creates folder.
}
I know i should return the user to log in. I don't know how to do both together.
Just create the folder and then return created user instance like it does Laravel:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
\Storage::makeDirectory($data['name']);
return $user;
}
First of all, I don't think creating a new folder for each user is a good practice. If you want to store user related stuff and identify them, you can map the file_name to user_id or append some user_id identification to the file_name. However in your scenario, please try the following code.
protected function create(array $data)
{
//create user and store it in variable called user
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
//make the folder with user_id, since the duplication can happen in name. Here the path is defined and the folder name is created with user id and the folder permission 755 is given
$makeDir = File::makeDirectory('/path/to/directory/'.$user->id , 0775);
//Then auth login user
Auth::login($user);
//Then return to dashboard or new view.
return redirect('/dashboard');
}
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
auth('your-guard-name')->login($user);
//this logs the user into the application
return \Storage::makeDirectory($data['name']);
//The above creates folder.
}
When a new user registers, they should be automatically assigned to a plan subscription. I can do that manually in Tinker (Laravel 5):
$token = Input::get('stripeToken');
$user = User::all();
$user->subscription('monthly')->create($token);
flash('Your account has been created with a membership');
Where, in Laravel 5, do I put such logic?
Edit
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'company_name' => $data['company_name'],
// I have added the below:
$token = Input::get('stripeToken');
$user = User::all();
$user->subscription('loop')->create($token);
]);
}
If you are using Laravels Registrar service then I'd do it in there. That could look like this:
public function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'company_name' => $data['company_name']
]);
$token = Input::get('stripeToken');
$user->subscription('loop')->create($token);
return $user;
}
You might also want to get the stripeToken from the $data array but I'll leave that to you.