You may use [[isGuest]] to determine whether the current user is a guest or not.
If the user is a guest, the [[identity]] property would return null. Otherwise, it would
be an instance of IdentityInterface.
You may call various methods to change the user authentication status:
- User::login: sets the specified identity and remembers the authentication status in session and cookie.
- User::logout: marks the user as a guest and clears the relevant information from session and cookie.
- User::setIdentity: changes the user identity without touching session or cookie.
This is best used in stateless RESTful API implementation.
Note that User only maintains the user authentication status. It does NOT handle how to authenticate
a user. The logic of how to authenticate a user should be done in the class implementing IdentityInterface.
You are also required to set [[identityClass]] with the name of this class.
User is configured as an application component in Application by default.
You can access that instance via Yii::$app->user.
You can modify its configuration by adding an array to your application config under components
as it is shown in the following example:
~~~
'user' => [
'identityClass' => 'app\models\User', // User must implement the IdentityInterface
'enableAutoLogin' => true,
'loginUrl' => ['user/login'],
...
]
~~~