# User Authentication
WARNING
This section only applies to Blessing Skin v4 or higher
# Overview
Blessing Skin v4 uses Laravel's built-in authentication system, so
getting current user's information is easy. Laravel's authentication
system provides a set of methods that you can either call through the
Auth
facade or use the auth
helper function. The demos below all use
the auth
helper function, but in practice, both the Auth
Facade and
the auth
helper function are the same.
# Check if the user is logged in
In general, you should use the auth
middleware if you want to make a
request only accessible when the user is logged in. However, in some
scenarios, you may need to manually check whether the user is logged in
in the controller or elsewhere, then you can use the check
method,
which returns a value of type bool
, indicating whether the user is
logged in.
if (auth()->check()) {
// user is logged in
}
# Get the model instance of the current user
You probably need to get a model instance of the currently logged in
user. You can use the user
method, if the user is not logged in, it
will return null
, so you don’t need to call the check
method before
using the user
method, you only need to judge whether the return value
of the user
method is empty .
$user = auth()->user();
// $user here is an instance of App\Models\User, so methods in the User model instance can be used.
# Get the uid
of the current user
You can get the uid
of the current user like this:
$uid = auth()->user()->uid;
But there is actually an easier way, which is to call the id
method:
$uid = auth()->id();
# Blade and Views
Laravel provides two directives for Blade, which can judge the current
authentication status without using the @if
directive to judge
manually.
# @auth
directive
@auth
The content here can only be seen when the user is logged in.
@endauth
# @guest
directive
@guest
The content here can only be seen by users who are not logged in.
@endguest