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.
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');
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)
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 use a standart Laravel Authentication.
php artisan make:auth
But I need to check the status of the user email (confirmed/not confirmed). If not confirmed, then an error will be shown in the login page.
Laravel 5.2
Thanks!
Make a column is_verified and another column verification_hash in users table. In AuthController.php
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'verification_hash' => md5($data['name'] . Carbon::now()->timestamp),
'is_verified' => 0,
]);
}
override register method to send him mail with Verification link
public function register(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
$user = $this->create($request->all());
Auth::guard($this->getGuard())->login($user);
// Send verification email
// use $user->verification_hash to generate link
return redirect($this->redirectPath());
}
Now this verification link should look like below as it contain a hash specific to that user
http://your_domain.com/auth/verify/6d533b7664483d3cadd13c23477e4f12
on this link method, change is_verified to 1 for that user.
Now modify your validator method like this
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'username' => 'required|max:255|unique:users',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'is_verified'=>'in:1'
]);
}
This will allow only those users to login who have is_verified value as 1.
Hope this helps.
I'm new to Laravel and am trying to figure out how to authenticate against two tables during new user registration.
I've modified the default methods in AuthController - I'm checking to see if a store number is valid, and if it is, register the user. This works fine - if the store number provided checks out, the user is inserted into both tables (user and user_store) and redirected to the dashboard page.
However, if the validation against $store is false, then I receive the following error
Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Http\RedirectResponse given
As you can see in the code I'm just trying to redirect to the auth/register view and provide an error message that the store number was invalid. Where am I going wrong?
SEE UPDATED CODE BELOW THIS BLOCK...
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:user',
'password' => 'required|min:6',
'store_number' => 'required',
]);
}
protected function create(array $data)
{
if(!$store = Store::where('number', $data['store_number'])->first()) {
// HERE'S WHERE I'M HAVING THE PROBLEM
return redirect('auth/register')->withErrors('store_number','Could not find a match for the Store Number');
} else {
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$data['sid'] = $store->id;
$data['uid'] = $user->id;
$store = UserStore::create($data);
return $user;
}
}
UPDATE
I've since moved this into the validator() method, because, well, it makes more sense to do the validation in the validator() method... right?
Here's my new code.
protected function validator(array $data)
{
if(!Store::where('number', $data['store_number'])->first()) {
// still not working!
return redirect('auth/register')->withErrors('store_number','Could not find a match for the Store Number');
}
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:user',
'password' => 'required|min:6',
'store_number' => 'required',
]);
}
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$s['sid'] = $data['store_number'];
$s['uid'] = $user->id;
$store = UserStore::create($s);
return $user;
}
And here's my new error message
BadMethodCallException in RedirectResponse.php line 198:
Method [fails] does not exist on Redirect.
Figured it out. I needed to use the After validation hook inside the validator() method. :)
protected function validator(array $data)
{
$validator = Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:user',
'password' => 'required|min:6',
'store_number' => 'required',
]);
$validator->after(function($validator) {
if(!Store::where('number', $_POST['store_number'])->first()) {
$validator->errors()->add('store_number', 'Could not find a match for the Store number');
}
});
return $validator;
}