There is an Auth
facade you may use for common tasks, it primarily inherits the October\Rain\Auth\Manager
class for functionality.
You may use register
method to register an account.
$user = Auth::register([
'name' => 'Some User',
'email' => '[email protected]',
'password' => 'changeme',
'password_confirmation' => 'changeme',
]);
The second argument can specify if the account should be automatically activated:
// Auto activate this user
$user = Auth::register([...], true);
The Auth::check
method is a quick way to check if the user is signed in.
// Returns true if signed in.
$loggedIn = Auth::check();
To return the user model that is signed in, use Auth::getUser
instead.
// Returns the signed in user
$user = Auth::getUser();
You may authenticate a user by providing their login and password with Auth::authenticate
.
// Authenticate user by credentials
$user = Auth::authenticate([
'login' => post('login'),
'password' => post('password')
]);
The second argument is used to store a non-expire cookie for the user.
$user = Auth::authenticate([...], true);
You can also authenticate as a user simply by passing the user model along with Auth::login
.
// Sign in as a specific user
Auth::login($user);
The second argument is the same.
// Sign in and remember the user
Auth::login($user, true);
You may look up a user by their login name using the retrieveByCredentials
method.
$user = Auth::retrieveByCredentials(['email' => '[email protected]']);
Creating a guest user allows the registration process to be deferred. For example, making a purchase without needing to register first. Guest users are not able to sign in and will be added to the user group with the code guest
.
Use the Auth::registerGuest
method to create a guest user, it will return a user object and can be called multiple times. The unique identifier is the email address, which is a required field.
$user = Auth::registerGuest(['email' => '[email protected]']);
When a user registers with the same email address using the Auth::register
method, they will inherit the existing guest user account.
// This will not throw an "Email already taken" error
$user = Auth::register([
'email' => '[email protected]',
'password' => 'changeme',
'password_confirmation' => 'changeme',
]);
Important: If you are using guest accounts, it is important to disable sensitive functionality for user accounts that are not verified, since it may be possible for anyone to inherit a guest account.
You may also convert a guest to a registered user with the convertToRegistered
method. This will generate a random password and sends an invitation using the rainlab.user::mail.invite
template.
$user->convertToRegistered();
To disable the notification and password reset, pass the first argument as false.
$user->convertToRegistered(false);