0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ログイン機能

0
Posted at

はじめに

前回の続きでLaravelの新規登録とログイン機能を実装します。
ModelはLaravelのプロジェクトを作った時に自動で作られるUserを使います。
前回の記事はこちら → https://qiita.com/kazuma0000/items/bb4aa3b63bbab406afd0

ルーティング設定

新規登録とログイン機能はフォームから入力があるのでGETとPOSTの処理を書きます。

nameを設定することでルートに名前をつけることができる

Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::get('/register', [AuthController::class, 'showRegisterForm'])->name('register');
Route::post('/register', [AuthController::class, 'register']);

コントローラーの実装

GETできた時はLogin画面を返します。

public function showLoginForm()
    {
        return view('login');
    }

POSTできた時はrequestから必要な情報を変数に入れます。
Laravelの認証機能を使って成功すればtrueを返します。
失敗した場合は元のページに戻します。

public function login(Request $request)
    {
        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials)) {
            return redirect('/');
        }
        return back()->withErrors(['email' => 'ログインに失敗しました']);
    }

GETできた時はRegister画面を返します。

public function showRegisterForm()
    {
        return view('register');
    }

POSTできた時は新しいユーザーをデータベースに登録。

public function register(Request $request)
    {
        $user = User::create([
            'name' => $request->name, 
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        Auth::login($user);

        return redirect('/');
    }

viewの作成

エラーメッセージを表示する。

login
<ul class="error-messages">
    @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
    @endforeach
</ul>

route()ではルーティングでつけた名前を参照することができる。

login
<form method="POST" action="{{ route('login') }}">
            @csrf
          <fieldset class="form-group">
            <input class="form-control form-control-lg" name="email" type="text" placeholder="Email" />
          </fieldset>
          <fieldset class="form-group">
            <input class="form-control form-control-lg" name="password" type="password" placeholder="Password" />
          </fieldset>
          <button class="btn btn-lg btn-primary pull-xs-right" type="submit">Sign in</button>
</form>
register
<form method="POST" action="{{ route('register') }}">
          @csrf
          <fieldset class="form-group">
              <input class="form-control form-control-lg" type="text" name="name" placeholder="Username" />
          </fieldset>
          <fieldset class="form-group">
              <input class="form-control form-control-lg" type="email" name="email" placeholder="Email" />
          </fieldset>
          <fieldset class="form-group">
              <input class="form-control form-control-lg" type="password" name="password" placeholder="Password" />
          </fieldset>
          <button class="btn btn-lg btn-primary pull-xs-right">Sign up</button>
</form>

まとめ

前回の続きで新規登録とログイン機能を実装しました。
attemptとloginの違いについて学びました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?