Day9 跟着官方文件学习Laravel-登入验证

第九天罗,今天我们要做登入验证,首先我们要先想想,登入要做什麽判断。

  1. 打帐号密码,按下登入
  2. 确认帐号密码正确
  3. 成功登入

好了那我们今天的任务就是完成这些项目
第一步,我们要把确认帐号密码是否正确。

$user = DB::table('users')->where('account', '=', $account)
                                  ->first();

先取得user资讯

        if(Hash::check($password, $user->password)){
            return "你好,我的帐号是".$request->input('account');
        }
        return "帐号或密码错误";

确认密码也正确後,才能确认是本人,不是本人则告诉他帐密错误。

不过,我们跟他说密码错之後,要返回页面让他重新登入。

if(Hash::check($password, $user->password)){
            return "你好,我的帐号是".$request->input('account');
        }
        return Redirect::back()->withErrors(['帐号或密码错误']);

那麽们的view也需要改一下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>login</title>
</head>
<body>
    <h1>登入画面</h1>
    @if($errors->any())
    <h4>{{$errors->first()}}</h4>
    @endif

    <form action='/login' method='POST'>
        @csrf
        帐号:<input type='text' name='account'><br/>
        密码:<input type='password' name='password'><br/>
        <button type='submit'>登入</button>
    </form>
</body>
</html>

OK,这样就完成我们的帐号验证罗,明天我们来看看如何利用migration。


<<:  Day 21 | Livewire 实作 Todo List(三): 切换其他日期的待办事项

>>:  第 8 天 迈出 RxJS 小小的一步|pipe、operators

Day 21 | 状态管理套件 MobX - 到底什麽是状态管理

状态管理? 在介绍 MobX 以前我想先来说一下什麽是「状态管理」 究竟为什麽我们需要「状态管理」,...

【C++】Pointer to Pointer

Pointer to Pointer 顾名思义就是指标的指标~ 它可能是一个变数的地址的地址~ 我们...

Day 3 | Dart 基本介绍 - Dart vs JS

Dart是什麽? Dart 是一个静态强型别的语言,同时支援物件导向程序设计(OOP)及函数式程序设...

存放资料的 state、module

在 JavaScript 中,储存资料的方式,长这样。 { name: 'Chris', age: ...

[PoEAA] Domain Logic Pattern - Table Module

本篇同步发布於个人Blog: [PoEAA] Domain Logic Pattern - Tabl...