Laravel 10 入門トップページ


目次

  1. 全体の概要
  2. Laravel によるユーザ認証
    1. 概要
    2. プロジェクトの作成と初期設定
    3. メールの設定
    4. Laravel Breeze のインストール
    5. ユーザ登録機能の動作確認
    6. シーダによるテストユーザの登録
    7. テーブルにログイン ID の属性を追加
    8. ユーザ登録機能の修正
    9. ログイン ID でログインするように変更
    10. メールアドレスの検証
    11. シーダの修正
    12. 検証メールの有効期限と再送信
    13. セッション継続時間の確認と変更
    14. メールアドレスの変更
    15. パスワードの変更
    16. アカウントの削除
  3. ユーザ認証を備えたコメント掲示板の開発
  4. マルチ認証の実現

Laravel でユーザ認証とマルチ認証を実現する

Laravel によるユーザ認証

プロジェクトの作成と初期設定

ここではプロジェクトを作成して初期設定を行います.まず,ubuntu にログインして,laravel のディレクトリに移動します.この作業は各自の環境に合わせてください.ここの手順で ubuntu をインストールした場合には次のような操作になります.

rinsaka@macmini2020 ubuntu % vagrant ssh ⏎
Last login: Fri Nov  3 09:12:25 2023 from 10.0.2.2
vagrant@ubuntu2204 ~ $ cd Documents/ ⏎
vagrant@ubuntu2204 Documents $ ls ⏎
laravel
vagrant@ubuntu2204 Documents $ cd laravel/ ⏎
vagrant@ubuntu2204 laravel $ pwd ⏎
/home/vagrant/Documents/laravel
vagrant@ubuntu2204 laravel $ ls ⏎
comment_app  composer.phar  laravelRelationship
vagrant@ubuntu2204 laravel $

プロジェクト名を laravelAuth として新規プロジェクトを作成します.

vagrant@ubuntu2204 laravel $ php composer.phar create-project --prefer-dist laravel/laravel laravelAuth ⏎
Creating a "laravel/laravel" project at "./laravelAuth"
Installing laravel/laravel (v10.2.8)
  - Downloading laravel/laravel (v10.2.8)
  - Installing laravel/laravel (v10.2.8): Extracting archive
Created project in /home/vagrant/Documents/laravel/laravelAuth

(中略)

82 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force

   INFO  No publishable resources for tag [laravel-assets].

No security vulnerability advisories found.
> @php artisan key:generate --ansi

   INFO  Application key set successfully.

vagrant@ubuntu2204 laravel $

作成されたファイルを確認し,Git でコミットしておきます.(この先も適宜コミットを重ねてください)

vagrant@ubuntu2204 laravel $ ls ⏎
comment_app  composer.phar  laravelAuth  laravelRelationship
vagrant@ubuntu2204 laravel $ cd laravelAuth/ ⏎
vagrant@ubuntu2204 laravelAuth $ ls ⏎
README.md  artisan    composer.json  config    package.json  public     routes   tests   vite.config.js
app        bootstrap  composer.lock  database  phpunit.xml   resources  storage  vendor
vagrant@ubuntu2204 laravelAuth $ git init ⏎
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: 	git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: 	git branch -m <name>
Initialized empty Git repository in /home/vagrant/Documents/laravel/laravelAuth/.git/
vagrant@ubuntu2204 laravelAuth $ git add . ⏎
vagrant@ubuntu2204 laravelAuth $ git commit -m'initial commit' ⏎
[master (root-commit) e6f9d50] initial commit
 79 files changed, 11128 insertions(+)
 create mode 100644 .editorconfig
 create mode 100644 .env.example

(中略)

 create mode 100644 vite.config.js
vagrant@ubuntu2204 laravelAuth $

データベースには SQLite を使うこととして,空のデータベースファイルを作成します.(MySQLなどを使う場合はここを参考に適宜その設定を行ってください.)

vagrant@ubuntu2204 laravelAuth $ pwd ⏎
/home/vagrant/Documents/laravel/laravelAuth
vagrant@ubuntu2204 laravelAuth $ ls -a ⏎
.              .env.example    README.md  composer.json  package.json  routes   vite.config.js
..             .git            app        composer.lock  phpunit.xml   storage
.editorconfig  .gitattributes  artisan    config         public        tests
.env           .gitignore      bootstrap  database       resources     vendor
vagrant@ubuntu2204 laravelAuth $ cd database/ ⏎
vagrant@ubuntu2204 database $ ls ⏎
factories  migrations  seeders
vagrant@ubuntu2204 database $ touch database.sqlite ⏎
vagrant@ubuntu2204 database $ ls ⏎
database.sqlite  factories  migrations  seeders
vagrant@ubuntu2204 database $ cd .. ⏎
vagrant@ubuntu2204 laravelAuth $

データベースに SQLite を利用するための設定を .env ファイルに記載します(MySQLの場合はその設定を記載してください).

.env(抜粋)DB_CONNECTION=sqlite
# DB_CONNECTION=mysql
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=laravel
# DB_USERNAME=root
# DB_PASSWORD=

タイムゾーンと言語の設定を行います.

config/app.php(抜粋)    /*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => 'Asia/Tokyo',

    /*
    |--------------------------------------------------------------------------
    | Application Locale Configuration
    |--------------------------------------------------------------------------
    |
    | The application locale determines the default locale that will be used
    | by the translation service provider. You are free to set this value
    | to any of the locales which will be supported by the application.
    |
    */

    'locale' => 'ja',

    /*
    |--------------------------------------------------------------------------
    | Application Fallback Locale
    |--------------------------------------------------------------------------
    |
    | The fallback locale determines the locale to use when the current one
    | is not available. You may change the value to correspond to any of
    | the language folders that are provided through your application.
    |
    */

    'fallback_locale' => 'en',

    /*
    |--------------------------------------------------------------------------
    | Faker Locale
    |--------------------------------------------------------------------------
    |
    | This locale will be used by the Faker PHP library when generating fake
    | data for your database seeds. For example, this will be used to get
    | localized telephone numbers, street address information and more.
    |
    */

    'faker_locale' => 'ja_JP',

ubuntu に設定された IP アドレスを確認します.ip a の表示結果から目視で探しても良いですし,grep コマンドで検索しても良いでしょう.今回の IP アドレスは 192.168.56.101 です.

vagrant@ubuntu2204 laravelAuth $ ip a ⏎
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:2d:3b:77 brd ff:ff:ff:ff:ff:ff
    altname enp0s3
    inet 10.0.2.15/24 metric 100 brd 10.0.2.255 scope global dynamic eth0
       valid_lft 83633sec preferred_lft 83633sec
    inet6 fe80::a00:27ff:fe2d:3b77/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:30:e5:c5 brd ff:ff:ff:ff:ff:ff
    altname enp0s8
    inet 192.168.56.101/24 brd 192.168.56.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe30:e5c5/64 scope link
       valid_lft forever preferred_lft forever
vagrant@ubuntu2204 laravelAuth $ ip a | grep "inet " ⏎
    inet 127.0.0.1/8 scope host lo
    inet 10.0.2.15/24 metric 100 brd 10.0.2.255 scope global dynamic eth0
    inet 192.168.56.101/24 brd 192.168.56.255 scope global eth1
vagrant@ubuntu2204 laravelAuth $

IP アドレスが確認できたので,その IP アドレスとポート番号 8000 番を指定して Web サーバを起動します.終了するには Ctrl + C を押してください.

vagrant@ubuntu2204 laravelAuth $ php artisan serve --host=192.168.56.101 --port=8000 ⏎

   INFO  Server running on [http://192.168.56.101:8000].

  Press Ctrl+C to stop the server

Web サーバを起動したら,クライアント PC の Web ブラウザを起動して,http://192.168.56.101:8000/ にアクセスしてください.次のような画面が表示されたら OK です.なお,クライアント PC の画面表示設定がダークモードの場合は次のようなダークテーマで表示されます.従来のライトモードであれば,明るい白基調の画面になります.また,現時点では画面の右上に「Log in」や「Register」のリンクが存在しないことを確認してください.この後の作業でそれらのリンクが表示されるようになります.

laravel10-2023-comment-08.png

目次に戻る