Matteo Del Mastro
Matteo Del Mastro

Reputation: 1

Send Credentials and verify Email of new created user

My question is: is it possible, in Laravel 11, send by mail to a new user, created by admin, a message with button so that he could simultaneously log in and verify the email address. Could you help me? this is my code for store new user:

  public function store(Request $request)
  {
    $userID = $request->id;
    if ($userID) {
      // update the value
      $users = User::updateOrCreate(
        ['id' => $userID],
        ['name' => $request->name, 'email' => $request->email]
      );
      DB::table('model_has_roles')
       ->where('model_id', $request->id)
       ->update(['role_id' =>  $request->input('user_role'), 
          'model_type' => 'App\Models\User']);
      // user updated
      return response()->json('Updated');
    } else {
      // create new one if email is unique
      $userEmail = User::where('email', $request->email)->first();
      if (empty($userEmail)) {
        $password = Str::random(10);
        $users = User::updateOrCreate(
          ['id' => $userID],
          ['name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($password),
            'status' => 1]
        );
        DB::table('model_has_roles')
          ->where('model_id', $users->id)
          ->insert(['role_id' =>  $request->input('user_role'), 
              'model_type' => 'App\Models\User', 'model_id' => $users->id, 'team_id' => 1]);
        //UserCreated::dispatch($users, $password);
        //$users->notify(new PasswordToUserEmail($password));
        event(new UserCreated($users, $password));
        // user created
        return response()->json('Created');
      } else {
        // user already exist
        return response()->json(['message' => "already exits"], 422);
      }
    }

Thanks

Upvotes: -3

Views: 26

Answers (0)

Related Questions