# 使用auth()全局辅助和Auth Facade

auth()全局辅助函数是在整个应用内与已认证用户交互最简单的方法。你也可以注入Illuminate\Auth \AuthManager实例获取相同的功能，或者使用Auth facade。

最常见的需求是检测用户是或否登录(如果已经登录auth()->check()将返回true,auth()->guest()如果用户没登录则返回true)。以及获取当前登录的用户(auth()->user(),或者auth()->id()用于获取用户ID，如果用户没登录，这俩方法返回null)。

查看示例9-3，在控制器内使用全局auth()辅助函数。

{% code title="Example 9-3. Sample usage of the auth() global helper in a controller" %}

```php
public function dashboard() {
    if (auth()->guest()) {
        return redirect('sign-up');
    }
    return view('dashboard') ->with('user', auth()->user());
}
```

{% endcode %}

下一章介绍auth系统的幕后，这是有用的但不是特别重要的信息，如果你想查看的话请翻阅231页面的"The Auth Scaffold"


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liseen315.gitbook.io/laravel/yong-hu-ren-zheng-yu-shou-quan/shi-yong-auth-quan-ju-fu-zhu-he-auth-facade.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
