Laravel6 로그인 기능 구현

버전 5.8 까지는 php artisan make:auth 라는 명령을 실행하는 것만으로 로그인 기능을 구현할 수 있었습니다만, 6.0 로부터 그 순서가 바뀌었습니다.

이번은 Laravel 6.0 로 로그인 기능을 구현하는 방법을 소개합니다.

추가 사항: Laravel 8부터 Laravel UI는 더 이상 사용되지 않으며 Jetstream이라는 패키지로 마이그레이션되었습니다.
  • Docker × Laravel 8 Jetstream으로 로그인, 사용자 등록, 2단계 인증 구현

  • 환경


  • PHP 7.3
  • Laravel 6.0.3

  • 실제 소스 코드



    GitHub로 올립니다. 아래의 순서로 동작을 확인할 수 있습니다.
    $ git clone [email protected]:ucan-lab/laravel60-authentication
    $ cd laravel60-authentication
    $ docker-compose up -d --build
    $ docker-compose exec app composer install
    $ docker-compose exec app cp .env.example .env
    $ docker-compose exec app php artisan key:generate
    $ docker-compose exec app php artisan migrate
    $ docker-compose run node npm install
    $ docker-compose run node npm run dev
    

    Laravel 6.0에서 로그인 기능 구현



    이전 준비


    $ php artisan migrate
    

    Laravel을 설치하고 마이그레이션까지 실행하십시오.

    laravel/ui 설치



    laravel/ui 라이브러리를 설치합니다.
    $ composer require laravel/ui 1.*
    
  • 변경되는 파일
  • composer.json
  • composer.lock


  • composer require laravel/ui 파일 차이
    $ php artisan ui vue --auth
    
    Vue scaffolding installed successfully.
    Please run "npm install && npm run dev" to compile your fresh scaffolding.
    Authentication scaffolding generated successfully.
    
  • 추가되는 파일
  • app/Http/Controllers/HomeController.php
  • resources/js/components/ExampleComponent.vue
  • resources/sass/_variables.scss
  • resources/views/auth/login.blade.php
  • resources/views/auth/passwords/email.blade.php
  • resources/views/auth/passwords/reset.blade.php
  • resources/views/auth/register.blade.php
  • resources/views/auth/verify.blade.php
  • resources/views/home.blade.php
  • resources/views/layouts/app.blade.php

  • 변경되는 파일
  • package.json
  • resources/js/app.js
  • resources/js/bootstrap.js
  • resources/sass/app.scss
  • routes/web.php
  • webpack.mix.js


  • php artisan ui vue --auth 파일 차이
    $ npm install
    $ npm run dev
    

    이것으로 Laravel 로그인 화면의 구현이 완료되었습니다.

    스크린샷



    / Welcome 화면




  • 오른쪽 상단에 LOGINREGISTER 메뉴가 추가됩니다.

  • /register 회원 등록 화면





    /home 홈(대시보드) 화면





    /login 로그인 화면





    /password/reset 비밀번호 재설정 화면





    라우팅


    php artisan ui vue --auth 명령 실행 후 라우팅 정의입니다.
    $ php artisan route:list
    +--------+----------+----------------------------+------------------+------------------------------------------------------------------------+-------------------------------------------------+
    | Domain | Method   | URI                        | Name             | Action                                                                 | Middleware                                      |
    +--------+----------+----------------------------+------------------+------------------------------------------------------------------------+-------------------------------------------------+
    |        | GET|HEAD | /                          |                  | Closure                                                                | web                                             |
    |        | POST     | _ignition/execute-solution |                  | Facade\Ignition\Http\Controllers\ExecuteSolutionController             | Facade\Ignition\Http\Middleware\IgnitionEnabled |
    |        | GET|HEAD | _ignition/health-check     |                  | Facade\Ignition\Http\Controllers\HealthCheckController                 | Facade\Ignition\Http\Middleware\IgnitionEnabled |
    |        | GET|HEAD | _ignition/scripts/{script} |                  | Facade\Ignition\Http\Controllers\ScriptController                      | Facade\Ignition\Http\Middleware\IgnitionEnabled |
    |        | POST     | _ignition/share-report     |                  | Facade\Ignition\Http\Controllers\ShareReportController                 | Facade\Ignition\Http\Middleware\IgnitionEnabled |
    |        | GET|HEAD | _ignition/styles/{style}   |                  | Facade\Ignition\Http\Controllers\StyleController                       | Facade\Ignition\Http\Middleware\IgnitionEnabled |
    |        | GET|HEAD | api/user                   |                  | Closure                                                                | api,auth:api                                    |
    |        | GET|HEAD | home                       | home             | App\Http\Controllers\HomeController@index                              | web,auth                                        |
    |        | POST     | login                      |                  | App\Http\Controllers\Auth\LoginController@login                        | web,guest                                       |
    |        | GET|HEAD | login                      | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest                                       |
    |        | POST     | logout                     | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web                                             |
    |        | POST     | password/email             | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest                                       |
    |        | GET|HEAD | password/reset             | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest                                       |
    |        | POST     | password/reset             | password.update  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest                                       |
    |        | GET|HEAD | password/reset/{token}     | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest                                       |
    |        | GET|HEAD | register                   | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest                                       |
    |        | POST     | register                   |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,guest                                       |
    +--------+----------+----------------------------+------------------+------------------------------------------------------------------------+-------------------------------------------------+
    

    비고



    php 아르지씨 위


    $ php artisan ui -h
    Description:
      Swap the front-end scaffolding for the application
    
    Usage:
      ui [options] [--] <type>
    
    Arguments:
      type                   The preset type (bootstrap, vue, react)
    
    Options:
          --auth             Install authentication UI scaffolding
          --option[=OPTION]  Pass an option to the preset command (multiple values allowed)
      -h, --help             Display this help message
      -q, --quiet            Do not output any message
      -V, --version          Display this application version
          --ansi             Force ANSI output
          --no-ansi          Disable ANSI output
      -n, --no-interaction   Do not ask any interactive question
          --env[=ENV]        The environment the command should run under
      -v|vv|vvv, --verbose   Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
    
    vue 이외에도 react , bootstrap 에서 선택할 수 있습니다.

    인증 구성 파일



    config/auth.php



    config/auth.php
    <?php
    
    return [
    
        /*
        |--------------------------------------------------------------------------
        | Authentication Defaults
        |--------------------------------------------------------------------------
        |
        | This option controls the default authentication "guard" and password
        | reset options for your application. You may change these defaults
        | as required, but they're a perfect start for most applications.
        |
        */
    
        'defaults' => [
            'guard' => 'web',
            'passwords' => 'users',
        ],
    
        /*
        |--------------------------------------------------------------------------
        | Authentication Guards
        |--------------------------------------------------------------------------
        |
        | Next, you may define every authentication guard for your application.
        | Of course, a great default configuration has been defined for you
        | here which uses session storage and the Eloquent user provider.
        |
        | All authentication drivers have a user provider. This defines how the
        | users are actually retrieved out of your database or other storage
        | mechanisms used by this application to persist your user's data.
        |
        | Supported: "session", "token"
        |
        */
    
        'guards' => [
            'web' => [
                'driver' => 'session',
                'provider' => 'users',
            ],
    
            'api' => [
                'driver' => 'token',
                'provider' => 'users',
                'hash' => false,
            ],
        ],
    
        /*
        |--------------------------------------------------------------------------
        | User Providers
        |--------------------------------------------------------------------------
        |
        | All authentication drivers have a user provider. This defines how the
        | users are actually retrieved out of your database or other storage
        | mechanisms used by this application to persist your user's data.
        |
        | If you have multiple user tables or models you may configure multiple
        | sources which represent each model / table. These sources may then
        | be assigned to any extra authentication guards you have defined.
        |
        | Supported: "database", "eloquent"
        |
        */
    
        'providers' => [
            'users' => [
                'driver' => 'eloquent',
                'model' => App\User::class,
            ],
    
            // 'users' => [
            //     'driver' => 'database',
            //     'table' => 'users',
            // ],
        ],
    
        /*
        |--------------------------------------------------------------------------
        | Resetting Passwords
        |--------------------------------------------------------------------------
        |
        | You may specify multiple password reset configurations if you have more
        | than one user table or model in the application and you want to have
        | separate password reset settings based on the specific user types.
        |
        | The expire time is the number of minutes that the reset token should be
        | considered valid. This security feature keeps tokens short-lived so
        | they have less time to be guessed. You may change this as needed.
        |
        */
    
        'passwords' => [
            'users' => [
                'provider' => 'users',
                'table' => 'password_resets',
                'expire' => 60,
            ],
        ],
    
    ];
    

    참고 기사



  • Laravel 6.0 인증
  • Laravel 6.0 인증


  • Laravel 6.0 mix
  • Laravel 6.0 자산 컴파일(Mix)

  • 좋은 웹페이지 즐겨찾기