> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Socialite（LINE Login）- LINE SDK for Laravel

> 通过 Laravel Socialite 实现基于 LINE Login 的 OAuth2 认证。

## 概览

该包通过 `Socialite::extend()` 以官方方式提供 LINE Login 驱动。你可以像使用内置 provider 一样使用它。

可用驱动：

| 驱动           | 说明                                                                            |
| ------------ | ----------------------------------------------------------------------------- |
| `line-login` | 通过 [LINE Login](https://developers.line.biz/en/docs/line-login/) 进行 OAuth2 认证 |

<Info>
  关于 Laravel Socialite 自身的使用方式，请参见 [Socialite 指南](/zh/socialite)。
</Info>

## 配置

### .env

```dotenv theme={null}
LINE_LOGIN_CLIENT_ID=
LINE_LOGIN_CLIENT_SECRET=
LINE_LOGIN_REDIRECT=
```

## 基本使用

<Steps>
  <Step title="定义路由">
    在 `routes/web.php` 中添加两个路由，分别用于跳转和回调。

    ```php theme={null}
    use App\Http\Controllers\SocialiteController;

    Route::get('login', [SocialiteController::class, 'login']);
    Route::get('callback', [SocialiteController::class, 'callback']);
    ```
  </Step>

  <Step title="创建控制器">
    ```php theme={null}
    <?php

    namespace App\Http\Controllers;

    use App\Models\User;
    use Illuminate\Http\Request;
    use Laravel\Socialite\Facades\Socialite;

    class SocialiteController extends Controller
    {
        public function login()
        {
            return Socialite::driver('line-login')->redirect();
        }

        public function callback(Request $request)
        {
            if ($request->missing('code')) {
                // 调试用。生产环境请改为跳转或错误处理
                return redirect('/login')->withErrors('认证已被取消。');
            }

            /** @var \Laravel\Socialite\Two\User */
            $user = Socialite::driver('line-login')->user();

            $loginUser = User::updateOrCreate([
                'line_id' => $user->id,
            ], [
                'name'          => $user->nickname,
                'avatar'        => $user->avatar,
                'access_token'  => $user->token,
                'refresh_token' => $user->refreshToken,
            ]);

            auth()->login($loginUser, true);

            return redirect()->route('home');
        }
    }
    ```
  </Step>
</Steps>

## 可选参数

可以通过 `with()` 传入附加参数。

```php theme={null}
public function login()
{
    return Socialite::driver('line-login')->with([
        'prompt'     => 'consent',
        'bot_prompt' => 'normal',
    ])->redirect();
}
```

## Scope 配置

通过 scope 指定要获取的权限。

```php theme={null}
public function login()
{
    return Socialite::driver('line-login')
                    ->setScopes(['profile', 'openid'])
                    ->redirect();
}
```

主要的 scope 列表请参见 [LINE Login Scope](https://developers.line.biz/en/docs/line-login/integrate-line-login/#scopes)。

<Info>
  更多最新信息请参见 [GitHub 仓库](https://github.com/invokable/laravel-line-sdk)。
</Info>


## Related topics

- [LINE SDK for Laravel](/zh/packages/laravel-line-sdk/index.md)
- [通知 Channel - LINE SDK for Laravel](/zh/packages/laravel-line-sdk/notification.md)
- [Webhook / Bot - LINE SDK for Laravel](/zh/packages/laravel-line-sdk/bot.md)
- [Laravel Socialite（社交登录）](/zh/socialite.md)
- [Socialite - Laravel Bluesky](/zh/packages/laravel-bluesky/socialite.md)
