Git Product home page Git Product logo

laravel-welcome-notification's Introduction

Send a welcome notification to new users

Latest Version on Packagist GitHub Workflow Status Total Downloads

Using this package you can send a WelcomeNotification to a new user of your app. The notification contains a secure link to a screen where the user can set an initial password.

$expiresAt = now()->addDay();

$user->sendWelcomeNotification($expiresAt);

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-welcome-notification

Migrating the database

You must publish the migrations provided by this package by executing this command:

php artisan vendor:publish --provider="Spatie\WelcomeNotification\WelcomeNotificationServiceProvider" --tag="migrations"

Next, you must migrate your database.

php artisan migrate

Preparing the user model

You must apply the \Spatie\WelcomeNotification\ReceivesWelcomeNotification trait to your User model.

Preparing the WelcomeController

Next you'll need to create a controller of your own that will extend Spatie\WelcomeNotification\WelcomeController. This controller will be used to show the welcome form and to save the password set by a user.

namespace App\Http\Controllers\Auth;

use Spatie\WelcomeNotification\WelcomeController as BaseWelcomeController;

class MyWelcomeController extends BaseWelcomeController
{
}

Registering the routes

You'll have to register these routes:

use Spatie\WelcomeNotification\WelcomesNewUsers;
use App\Http\Controllers\Auth\MyWelcomeController;

Route::group(['middleware' => ['web', WelcomesNewUsers::class,]], function () {
    Route::get('welcome/{user}', [MyWelcomeController::class, 'showWelcomeForm'])->name('welcome');
    Route::post('welcome/{user}', [MyWelcomeController::class, 'savePassword']);
});

Preparing the welcome form view

The welcome view that ships with the package, will be rendered when somebody clicks the welcome link in the welcome notification mail. You should style this view yourself. You can publish the views with this command:

php artisan vendor:publish --provider="Spatie\WelcomeNotification\WelcomeNotificationServiceProvider" --tag="views"

Usage

Here's how you can send a welcome notification to a user that you just created.

$expiresAt = now()->addDay();

$user->sendWelcomeNotification($expiresAt);

Handling successful requests

After the a user has successfully set a new password the sendPasswordSavedResponse of the WelcomeController will get called.

use Symfony\Component\HttpFoundation\Response;

class MyWelcomeController extends BaseWelcomeController
{
    public function sendPasswordSavedResponse(): Response

    {
        return redirect()->route('home');
    }
}

Customizing the notification

By default the WelcomeNotification will send a mail. If you wish to customize the mail you can extend WelcomeNotification and override the buildWelcomeNotificationMessage method.

use Illuminate\Notifications\Messages\MailMessage;

class MyCustomWelcomeNotification extends WelcomeNotification
{
    public function buildWelcomeNotificationMessage(): MailMessage
    {
        return (new MailMessage)
            ->subject('Welcome to my app')
            ->action(Lang::get('Set initial password'), $this->showWelcomeFormUrl);
    }
}

To use the custom notification you must add a method called sendWelcomeNotification to your User model.

public function sendWelcomeNotification(\Carbon\Carbon $validUntil)
{
    $this->notify(new MyCustomWelcomeNotification($validUntil));
}

Validating extra fields

The default welcome form that ships with this package only asks for a password. You can add more fields to the form by publishing the view and adding more fields to it.

To validate new fields you can override the rules function in your own WelcomeController. Here's an example where we want to validate an extra field named job_title.

class MyWelcomeController extends BaseWelcomeController
{
    public function rules()
    {
        return [
            'password' => 'required|confirmed|min:6',
            'job_title' => 'required',
        ];
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-welcome-notification's People

Contributors

adrianmrn avatar ainesophaur avatar alexmanase avatar alexvanderbist avatar bastien-phi avatar brendt avatar freekmurze avatar jartaud avatar jeffreyhosler avatar kristories avatar laravel-shift avatar lasseeee avatar ledeveloppeurdistingue avatar lloople avatar lloydowen avatar nielsvanpach avatar okipa avatar papamarfo avatar patinthehat avatar pkboom avatar riasvdv avatar xseguib avatar ziming avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-welcome-notification's Issues

419 Page Expired

Hi,
Followed the instructions through Handling successful requests and got a nice email.
When I clicked the Set initial password button I got a un-styled password reset screen.
when I added a password 2x and pressed Save password and login I got
419 | PAGE EXPIRED
the password was not set

user field welcome_valid_until has a valid entry
link: api/welcome/25?expires=1654789412&signature=real-token-here

How is the page expired?
How to fix this

Curious why `bcrypt` instead of `Hash::make`

I’m working on a new project using this package, and I’m extending the WelcomeController to include verifying a Google 2FA code in the showWelcomeForm controller.

I noticed you’re using bcrypt to hash passwords on this line instead of the Hash::make facade and am curious:

  1. Why you used that instead of the facade?
  2. If I change my hash driver, will it break or require me to change to match my hash driver?

Thanks!

PHP version Compatible

I found the solution.
I tried to install this package but it shows this error

[InvalidArgumentException]
Package spatie/laravel-welcome-notification at version has a PHP requirement incompatible with your PHP version (7.2.5)

Please help me with what the problem is.
Thanks

Attempt to read property "welcome_valid_until" on string

Hello, I've used this package with Laravel 8, and it worked a treat! Now I am working on a new project on Laravel 9 and whatever I do I can't get it to work.

Php Version: 8.1.6
Laravel Version: 9.19.0

This is the error message I receive:
Attempt to read property "welcome_valid_until" on string

Looking deeper, it looks like the request in the WelcomesNewUsers doesn't pass a user object and just the id integer. But im unsure if I'm looking in the right place.

Has anyone faced this issue? Or know a solution?

php 7.2.2 compatibility?

Hi,
ive been trying to install this package but im getting:

[InvalidArgumentException]
  Package spatie/laravel-welcome-notification at version  has a PHP requireme
  nt incompatible with your PHP version (7.2.2)

Call to undefined method Illuminate\Foundation\Auth\User::authentications()

Everything works as expected when creating a new user, until the user sets their password. After the password has been set I receive the following error:

[Call to undefined method Illuminate\Foundation\Auth\User::authentications()](https://dealerlogs.test/welcome/29?expires=1667893109&signature=944d9827a2f00acb40fa218fc3c8d9fda3fe0567e5e59474c122d2bf344158a6#top)

I am using:
PHP: 8.1.8
Laravel: 9.38.0

Any help with this would ne greatly appreciated.

THE WELCOME LINK HAS ALREADY BEEN USED.

I implemented the spatie package "laravel-welcome-notification" and whenever anyone clicks on the link sent in the email I'm getting the 403 error

THE WELCOME LINK HAS ALREADY BEEN USED.

I did a custom notification, and I think that in the beginning, it did work, and then suddenly, it stopped working.

Security Flaw

Hi All,
I am using this package in one of my projects and found that it has a security flaw, I can see the URL sent via email has a signature in it. During my testing, I changed the value of that signature and could still set my initial password. Then I checked the code and realized the signature was not being checked/validated anywhere.

So, If I know that someone has been registered by an admin and we know the ID they got, we can set their initial password.

Below is the function responsible for setting the initial password. But it does not validate the signature value.

public function savePassword(Request $request, User $user)
    {
        $request->validate($this->rules());

        $user->password = Hash::make($request->password);
        $user->welcome_valid_until = null;
        $user->save();

        auth()->login($user);

        return $this->sendPasswordSavedResponse();
    }

Publishing didn't work as described

Publishing didn't work as directed. Both the publishing of the migrations and the views didn't do anything with the supplied commands. I used the more generic one sans --provider to get them though:

$ php artisan vendor:publish --provider='Spatie\WelcomeNotification\WelcomeNotificationServiceProvider' --tag="migrations"

   INFO  Nothing to migrate.  

$ php artisan vendor:publish --provider='Spatie\WelcomeNotification\WelcomeNotificationServiceProvider' --tag="views"

   INFO  No publishable resources for tag [views].  

While this worked:

$ php artisan vendor:publish --tag="migrations"

   INFO  Publishing [migrations] assets.  

  Copying file [vendor/spatie/laravel-welcome-notification/database/migrations/add_welcome_valid_until_field_to_users_table.php.stub] to [database/migrations/2023_03_31_080232_add_welcome_valid_until_field_to_users_table.php]  DONE

$ php artisan vendor:publish --tag="views"

   INFO  Publishing [views] assets.  

  Copying directory [vendor/spatie/laravel-welcome-notification/resources/views] to [resources/views/vendor/welcomeNotification] .............. DONE

Using php 8.1 and laravel 9. Fresh install of laravel-welcome-notification.

Use `getRouteKey` when generating the url of the welcome form

I started getting a 404 error when I changed the user route key name to UUID. When debugging I noticed that the user id was still used in the URL, also I found that the package uses the user id directly when generating the user URL in the WelcomeNotification class. I suggest using the getRouteKey method instead.

before:

    protected function initializeNotificationProperties(User $user)
    {
        $this->user = $user;

        $this->user->welcome_valid_until = $this->validUntil;
        $this->user->save();

        $this->showWelcomeFormUrl = URL::temporarySignedRoute(
            'welcome',
            $this->validUntil,
            ['user' => $user->id]
        );
    }

after:

    protected function initializeNotificationProperties(User $user)
    {
        $this->user = $user;

        $this->user->welcome_valid_until = $this->validUntil;
        $this->user->save();

        $this->showWelcomeFormUrl = URL::temporarySignedRoute(
            'welcome',
            $this->validUntil,
            ['user' => $user->getRouteKey()]
        );
    }

Illuminate\Foundation\Auth\User in your BaseController instead of App\User

Love the package! it's great!

I noticed that you are using laravel's Illuminate\Foundation\Auth\User in your BaseController instead of App\User, and in my case there is some custom configuration i've done to my model that are lost while authenticating the user. For example the keyType is set to string bcz i'm using uuid.

I've had to copy and paste the controller for now, but hope you provide an answer for that soon to go back to using your controller.

Can I use with Multiple Authentication[Users, Admins]?

I have two models; User and Admin. I'm trying to set the package up for admins to so they get an email to set a password for their account.

I created a WelcomeNewAdmins middleware. How do I get the Admin object in the WelcomesNewAdmins middleware?

Event listener after successful password set

I currently have an event listener listening to the Illuminate\Auth\Events\Login event. After after a successful password save the package tries to authenticate the user manually into the app. For some reason the Listener doesn't have access to the user that was just authenticated. Is there a way to easily fix this?

PHP8

Hi,

When can we expect PHP8 support?

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.