Loading...
Loading...
Compare original and translation side by side
auth0/loginauth0/loginmbstringopenssljsonauth0-quickstartmbstringopenssljsonauth0-quickstart| Scenario | Use Instead |
|---|---|
| Laravel API with JWT Bearer validation | |
| Plain PHP (no framework) web app | |
| Plain PHP API | |
| Single Page Applications | |
| Next.js applications | |
| Node.js web apps | |
| Flask web apps | |
| 场景 | 替代方案 |
|---|---|
| 使用JWT Bearer验证的Laravel API | |
| 无框架的纯PHP Web应用 | |
| 纯PHP API | |
| 单页应用 | |
| Next.js应用 | |
| Node.js Web应用 | |
| Flask Web应用 | |
composer require auth0/loginauth0/loginauth0/auth0-phpcomposer require guzzlehttp/guzzle guzzlehttp/psr7composer require auth0/loginauth0/loginauth0/auth0-phpcomposer require guzzlehttp/guzzle guzzlehttp/psr7php artisan vendor:publish --tag=auth0config/auth0.phpphp artisan vendor:publish --tag=auth0config/auth0.php.envAPP_URL=http://localhost:8000
AUTH0_DOMAIN=your-tenant.us.auth0.com
AUTH0_CLIENT_ID=your_client_id
AUTH0_CLIENT_SECRET=your_client_secret
AUTH0_AUDIENCE=https://your-api-identifier
AUTH0_REDIRECT_URI=${APP_URL}/callbackAUTH0_DOMAINhttps://AUTH0_CLIENT_IDAUTH0_CLIENT_SECRETAUTH0_AUDIENCEAPP_KEYAPP_URLhttp://localhost:8000http://localhost.envAPP_URL=http://localhost:8000
AUTH0_DOMAIN=your-tenant.us.auth0.com
AUTH0_CLIENT_ID=your_client_id
AUTH0_CLIENT_SECRET=your_client_secret
AUTH0_AUDIENCE=https://your-api-identifier
AUTH0_REDIRECT_URI=${APP_URL}/callbackAUTH0_DOMAINhttps://AUTH0_CLIENT_IDAUTH0_CLIENT_SECRETAUTH0_AUDIENCEAPP_KEYAPP_URLhttp://localhost:8000http://localhosthttp://localhost:8000/callbackhttp://localhost:8000http://localhost:8000/callbackhttp://localhost:8000config/auth.php'guards' => [
'web' => [
'driver' => 'auth0.authenticator',
'provider' => 'auth0-provider',
'configuration' => 'web',
],
],
'providers' => [
'auth0-provider' => [
'driver' => 'auth0.provider',
'repository' => 'auth0.repository',
],
],configurationconfig/auth0.phpwebSTRATEGY_REGULARwebauthauth()->user()auth0-sessionwebconfig/auth.php'guards' => [
'web' => [
'driver' => 'auth0.authenticator',
'provider' => 'auth0-provider',
'configuration' => 'web',
],
],
'providers' => [
'auth0-provider' => [
'driver' => 'auth0.provider',
'repository' => 'auth0.repository',
],
],configurationconfig/auth0.phpwebSTRATEGY_REGULARwebauthauth()->user()auth0-sessionwebwebregisterAuthenticationRoutestrueconfig/auth0.phpGET /loginGET /callbackGET /logoutconfig/auth0.phpregisterAuthenticationRoutestrueGET /loginGET /callbackGET /logoutroutes/web.phpuse Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('home', ['user' => auth()->user()]);
});
Route::middleware('auth')->group(function () {
Route::get('/profile', function () {
return view('profile', ['user' => auth()->user()]);
});
});auth/loginroutes/web.phpuse Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('home', ['user' => auth()->user()]);
});
Route::middleware('auth')->group(function () {
Route::get('/profile', function () {
return view('profile', ['user' => auth()->user()]);
});
});auth/loginresources/views/home.blade.php<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
@if($user)
<h1>Welcome, {{ $user->name }}!</h1>
<p><a href="/profile">Profile</a></p>
<p><a href="/logout">Logout</a></p>
@else
<h1>Welcome</h1>
<p><a href="/login">Login</a></p>
@endif
</body>
</html>resources/views/profile.blade.php<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile</title>
</head>
<body>
<h1>{{ $user->name }}</h1>
<p>Email: {{ $user->email }}</p>
<img src="{{ $user->picture }}" alt="avatar" width="100" />
<hr>
<h2>User Claims</h2>
<pre>{{ json_encode($user->jsonSerialize(), JSON_PRETTY_PRINT) }}</pre>
<p><a href="/">Home</a> | <a href="/logout">Logout</a></p>
</body>
</html>resources/views/home.blade.php<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
@if($user)
<h1>Welcome, {{ $user->name }}!</h1>
<p><a href="/profile">Profile</a></p>
<p><a href="/logout">Logout</a></p>
@else
<h1>Welcome</h1>
<p><a href="/login">Login</a></p>
@endif
</body>
</html>resources/views/profile.blade.php<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile</title>
</head>
<body>
<h1>{{ $user->name }}</h1>
<p>Email: {{ $user->email }}</p>
<img src="{{ $user->picture }}" alt="avatar" width="100" />
<hr>
<h2>User Claims</h2>
<pre>{{ json_encode($user->jsonSerialize(), JSON_PRETTY_PRINT) }}</pre>
<p><a href="/">Home</a> | <a href="/logout">Logout</a></p>
</body>
</html>php artisan servehttp://localhost:8000http://localhost:8000http://127.0.0.1:8000localhostlocalhost127.0.0.1localhostphp artisan servehttp://localhost:8000http://localhost:8000http://127.0.0.1:8000localhostlocalhost127.0.0.1localhost| Mistake | Fix |
|---|---|
Using | Use |
| App created as SPA type in Auth0 Dashboard | Must be Regular Web Application for server-side session auth |
| Missing callback URL in Auth0 Dashboard | Add |
| Missing logout URL in Auth0 Dashboard | Add |
| Not publishing the config | Run |
| Using wrong guard driver name | Driver is |
Forgetting to set | Run |
Calling | Use Laravel's |
Manually defining | Routes are auto-registered by the service provider |
Setting | Use bare domain: |
Using | Only available in routes with the |
Missing | Without an audience, Auth0 returns opaque tokens the SDK cannot parse - causes "JWT string must contain two dots" crash |
Visiting | Session cookies set for |
Using | |
| 错误操作 | 修复方案 |
|---|---|
在Laravel中直接使用 | 使用 |
| 在Auth0控制台中创建SPA类型的应用 | 必须使用常规Web应用类型才能支持服务端会话认证 |
| Auth0控制台中未添加回调URL | 将 |
| Auth0控制台中未添加登出URL | 将 |
| 未发布配置文件 | 在配置前执行 |
| 使用错误的Guard驱动名称 | 驱动名称应为 |
忘记设置 | 执行 |
直接调用 | 使用Laravel的 |
手动定义 | 路由由服务提供者自动注册 |
| 使用纯域名: |
未使用中间件就调用 | 仅在应用了 |
缺少 | 未设置受众时,Auth0会返回无法被SDK解析的不透明令牌——导致“JWT字符串必须包含两个点”错误 |
访问 | 针对 |
使用 | |
| Method | Usage | Purpose |
|---|---|---|
| In routes/controllers | Returns the authenticated |
| In routes/controllers/views | Returns |
| When using multiple guards | Gets a specific Auth0 guard instance |
| On user object | User's display name (via |
| On user object | User's email (via |
| On user object | User's avatar URL (via |
| On user object | Returns the Auth0 |
| On user object | Returns any claim value explicitly |
| On user object | Returns all user claims as array |
| 方法 | 使用场景 | 用途 |
|---|---|---|
| 路由/控制器中 | 返回已认证的 |
| 路由/控制器/视图中 | 用户已认证时返回 |
| 使用多个Guard时 | 获取指定的Auth0 Guard实例 |
| 用户对象上 | 用户显示名称(通过 |
| 用户对象上 | 用户邮箱(通过 |
| 用户对象上 | 用户头像URL(通过 |
| 用户对象上 | 返回Auth0的 |
| 用户对象上 | 显式返回任意声明的值 |
| 用户对象上 | 返回所有用户声明的数组形式 |
StatefulUserAuthenticatable__get$user = auth()->user();
$user->name; // display name (via __get)
$user->email; // email address (via __get)
$user->picture; // avatar URL (via __get)
$user->email_verified; // any claim via property access
$user->getAuthIdentifier(); // Auth0 'sub' (e.g. 'auth0|abc123')
$user->getAttribute('sub'); // explicit claim access
$user->jsonSerialize(); // all claims as array$user->nickname$user->updated_at$user->sub$user->getAttribute('claim_name')StatefulUserAuthenticatable__get$user = auth()->user();
$user->name; // 显示名称(通过__get)
$user->email; // 邮箱地址(通过__get)
$user->picture; // 头像URL(通过__get)
$user->email_verified; // 通过属性访问任意声明
$user->getAuthIdentifier(); // Auth0 'sub'(例如 'auth0|abc123')
$user->getAttribute('sub'); // 显式访问声明
$user->jsonSerialize(); // 所有声明的数组形式$user->nickname$user->updated_at$user->sub$user->getAttribute('claim_name')auth0-laravel-apiauth0-phpauth0-quickstartauth0-mfaauth0-cliauth0-laravel-apiauth0-phpauth0-quickstartauth0-mfaauth0-cliconfig/auth.php'guards' => [
'web' => [
'driver' => 'auth0.authenticator',
'provider' => 'auth0-provider',
'configuration' => 'web',
],
],
'providers' => [
'auth0-provider' => [
'driver' => 'auth0.provider',
'repository' => 'auth0.repository',
],
],Route::middleware('auth')->group(function () {
Route::get('/dashboard', [DashboardController::class, 'index']);
});@auth
<p>Hello, {{ auth()->user()->name }}</p>
@else
<a href="/login">Login</a>
@endauthAPP_URLhttp://localhost:8000AUTH0_DOMAINtenant.us.auth0.comAUTH0_CLIENT_IDAUTH0_CLIENT_SECRETAUTH0_AUDIENCEAUTH0_REDIRECT_URI${APP_URL}/callbackAPP_KEYconfig/auth.php'guards' => [
'web' => [
'driver' => 'auth0.authenticator',
'provider' => 'auth0-provider',
'configuration' => 'web',
],
],
'providers' => [
'auth0-provider' => [
'driver' => 'auth0.provider',
'repository' => 'auth0.repository',
],
],Route::middleware('auth')->group(function () {
Route::get('/dashboard', [DashboardController::class, 'index']);
});@auth
<p>Hello, {{ auth()->user()->name }}</p>
@else
<a href="/login">Login</a>
@endauthAPP_URLhttp://localhost:8000AUTH0_DOMAINtenant.us.auth0.comAUTH0_CLIENT_IDAUTH0_CLIENT_SECRETAUTH0_AUDIENCEAUTH0_REDIRECT_URI${APP_URL}/callbackAPP_KEY