Cara Membuat Login, Registrasi, dan Logout secara Manual di Laravel.

by | Aug 7, 2023 | Blog, Laravel, Modul, Pemrograman, Source Code, Templates, Tutorial | 24 comments

PENDAHULUAN

                   Laravel menangani proses otentikasi dengan berbagai cara, seperti menggunakan Jetstream, Breeze, dan sebagainya. Namun, kedua metode otentikasi tersebut terkadang terlalu kompleks jika digunakan dalam proyek-proyek dengan ruang lingkup yang lebih kecil. Oleh karena itu, Laravel menyediakan metode otentikasi manual. Pada kesempatan ini, kita akan membahas tentang cara melakukan otentikasi manual di Laravel.

 

Langkah 1 

         Persiapan Awal Pastikan Anda telah memiliki proyek Laravel yang telah dibuat sebelumnya. Jika belum, Anda dapat membuatnya dengan menggunakan perintah berikut di terminal :

composer create-project laravel/laravel nama-proyek-anda

Langkah 2 

         Database Pastikan juga Anda telah mengatur database dan informasi koneksi database di file .env proyek Laravel Anda.

Langkah 3 

         Tabel Pengguna (Users) Jika Anda belum memiliki tabel pengguna (users) di database, buatlah dengan menggunakan migration Laravel:

php artisan migrate

Langkah 4 

           Pembuatan Route Selanjutnya, buatlah rute (route) untuk halaman login, registrasi, dan logout. Anda dapat menambahkan rute ini di dalam file routes/web.php :

Route::get(‘/login’, ‘AuthController@showLoginForm’)->name(‘login’);

Route::post(‘/login’, ‘AuthController@login’);

Route::post(‘/logout’, ‘AuthController@logout’)->name(‘logout’);

Route::get(‘/register’, ‘AuthController@showRegistrationForm’)->name(‘register’);

Route::post(‘/register’, ‘AuthController@register’);

    Langkah 5

               Pembuatan Controller Buatlah controller yang akan mengurus logika otentikasi. Anda dapat menggunakan perintah artisan berikut :

    php artisan make:controller AuthController

    Setelah itu, buka file AuthController yang baru dibuat di dalam folder “app/Http/Controllers” dan implementasikan metode-metode berikut :

    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;
    class AuthController extends Controller
    {
        public function showLoginForm()
        {
            return view('login');
        }
        public function login(Request $request)
        {
            $credentials = $request->validate([
                'email' => 'required|email',
                'password' => 'required',
            ]);
            if (Auth::attempt($credentials)) {
                $request->session()->regenerate();
                return redirect()->intended('/');
            }
            return back()->withErrors([
                'email' => 'Kombinasi email dan password tidak cocok.',
            ]);
        }
        public function logout(Request $request)
        {
            Auth::logout();
            $request->session()->invalidate();
            $request->session()->regenerateToken();
            return redirect('/');
        }
        public function showRegistrationForm()
        {
            return view('register');
        }
        public function register(Request $request)
        {
            $credentials = $request->validate([
                'name' => 'required',
                'email' => 'required|email|unique:users',
                'password' => 'required|min:8|confirmed',
            ]);
            $user = User::create([
                'name' => $credentials['name'],
                'email' => $credentials['email'],
                'password' => bcrypt($credentials['password']),
            ]);
            Auth::login($user);
           return redirect()->intended('/');
        }
    }

    Langkah 6

                  Pembuatan View Terakhir, buatlah tampilan (view) untuk halaman login dan registrasi. Anda dapat membuat file “login.blade.php” dan “register.blade.php” di dalam folder “resources/views”.

    Berikut adalah contoh sederhana kode untuk login.blade.php  :

    ```

    <!DOCTYPE html>

    <html>

    <head>

        <title>Login</title>

    </head>

    <body>

        <h2>Login</h2>

        @if ($errors->any())

            <div>

                <ul>

                    @foreach ($errors->all() as $error)

                        <li>{{ $error }}</li>

                    @endforeach

                </ul>

            </div>

        @endif

        <form method=”POST” action=”{{ url(‘/login’) }}”>

            @csrf

            <label for=”email”>Email:</label>

            <input type=”email” name=”email” required>

     

            <label for=”password”>Password:</label>

            <input type=”password” name=”password” required>

     

            <button type=”submit”>Login</button>

        </form>

    </body>

    </html>

    Dan berikut adalah contoh sederhana kode untuk register.blade.php :

    <!DOCTYPE html>

    <html>

    <head>

        <title>Register</title>

    </head>

    <body>

        <h2>Register</h2>

     

        @if ($errors->any())

            <div>

                <ul>

                    @foreach ($errors->all() as $error)

                        <li>{{ $error }}</li>

                    @endforeach

                </ul>

            </div>

        @endif

     

        <form method=”POST” action=”{{ url(‘/register’) }}”>

            @csrf

     

            <label for=”name”>Name:</label>

            <input type=”text” name=”name” required>

     

            <label for=”email”>Email:</label>

            <input type=”email” name=”email” required>

     

            <label for=”password”>Password:</label>

            <input type=”password” name=”password” required>

     

            <label for=”password_confirmation”>Confirm Password:</label>

            <input type=”password” name=”password_confirmation” required>

     

            <button type=”submit”>Register</button>

        </form>

    </body>

    </html>

    Kesimpulan

                 Dengan mengikuti langkah-langkah di atas, Anda dapat membuat fitur login, registrasi, dan logout secara manual di Laravel. Pastikan Anda telah memahami kode yang ditulis dan juga melakukan penyesuaian sesuai kebutuhan proyek Anda. Selamat mencoba!

    24 Comments

    1. 18XBET

      Exceptional post however I was wondering if you could write a litte more on this topic?

      I’d be very thankful if you could elaborate a little bit more.
      Thanks!

      Reply
    2. asporlogistic.com.ua

      If you are going for best contents like me, only go
      to see this website everyday because it provides feature contents, thanks

      Reply
    3. https://aspor.ua/inshi-tovary/

      you’re in reality a good webmaster. The web site loading
      speed is amazing. It seems that you are doing any unique trick.
      Furthermore, The contents are masterpiece. you’ve performed a excellent process on this subject!

      Reply
    4. trysildenafil.com

      wonderful post, very informative. I wonder why the opposite specialists
      of this sector do not realize this. You should proceed
      your writing. I’m confident, you’ve a great readers’ base already!

      Reply
    5. angka keramat

      I was wondering if you ever considered changing the page layout of your website?
      Its very well written; I love what youve got to say.

      But maybe you could a little more in the way of content so people could connect
      with it better. Youve got an awful lot of text for only having 1 or 2
      images. Maybe you could space it out better?

      Reply
    6. asporlogistic.com.ua

      Hello there, I found your web site by the use of Google even as
      looking for a similar subject, your website got here
      up, it looks good. I’ve bookmarked it in my google bookmarks.

      Hello there, just became alert to your blog through Google, and located
      that it’s truly informative. I am going to watch out for brussels.

      I will appreciate when you proceed this in future.
      Lots of other folks can be benefited from your writing.
      Cheers!

      Reply
    7. asporlogistic.com.ua

      Hello! I could have sworn I’ve visited this web site before but after going through many of the posts
      I realized it’s new to me. Anyways, I’m certainly happy I
      discovered it and I’ll be bookmarking it and checking back often!

      Reply
    8. sweet bonanza

      Hi there, I enjoy reading all of your article post.
      I wanted to write a little comment to support
      you.

      Reply
    9. money rob

      I have read so many posts concerning the blogger lovers however this article is actually a good post, keep it
      up.

      Reply
    10. situs SV388

      Hey there! I realize this is kind of off-topic however I had to ask.
      Does managing a well-established website such as yours require a massive amount
      work? I’m brand new to operating a blog however
      I do write in my diary every day. I’d like to start a blog so I can share my personal experience and thoughts online.
      Please let me know if you have any suggestions
      or tips for brand new aspiring blog owners. Appreciate it!

      Reply
    11. asporlogistic.com.ua

      My relatives every time say that I am wasting my time here at web, however I know I am
      getting know-how everyday by reading such good content.

      Reply
    12. bp77 slot

      I love what you guys are up too. This kind of clever work and coverage!
      Keep up the great works guys I’ve included you guys to my blogroll.

      Reply
    13. bp77 slot link alternatif

      Please let me know if you’re looking for a author for your blog.
      You have some really great posts and I think I would be a good asset.
      If you ever want to take some of the load off, I’d really like to
      write some content for your blog in exchange for a link back to mine.

      Please blast me an email if interested. Thank you!

      Reply
    14. bp77 slot

      Hi to all, as I am in fact keen of reading this weblog’s post
      to be updated on a regular basis. It contains fastidious data.

      Reply
    15. Smuggling

      I am genuinely delighted to glance at this web site posts which includes tons of valuable
      data, thanks for providing such data.

      Reply
    16. fuck

      I don’t know whether it’s just me or if everybody else experiencing
      problems with your blog. It seems like some of the written text within your content
      are running off the screen. Can someone else please comment and let me know if this is happening to them as well?

      This might be a problem with my web browser because I’ve had this happen before.

      Thanks

      Reply
    17. JHONBET77 slot

      all the time i used to read smaller content which as well clear their motive, and that is also
      happening with this post which I am reading at this
      time.

      Reply
    18. SPACENET ONE TELEVISION

      Wow, awesome blog layout! How long have you been blogging for?
      you made blogging look easy. The overall look of your web site is fantastic, as well as
      the content!

      Reply
    19. xxx

      Ridiculous quest there. What happened after? Take care!

      Reply
    20. JHONBET77

      Hmm it looks like your blog ate my first comment (it was super long) so I guess I’ll just sum it
      up what I submitted and say, I’m thoroughly enjoying your blog.

      I as well am an aspiring blog writer but I’m still new to the whole thing.
      Do you have any suggestions for inexperienced blog writers?
      I’d certainly appreciate it.

      Reply
    21. Biowin69

      I do not know if it’s just me or if everybody else
      experiencing issues with your website. It appears as if some of the written text within your posts are running off the screen. Can somebody else please comment and let
      me know if this is happening to them as well?
      This could be a problem with my browser because
      I’ve had this happen before. Appreciate it

      Reply
    22. Child porn

      Hi, after reading this awesome post i am also happy to share my
      know-how here with friends.

      Reply
    23. WD808

      My brother suggested I might like this web site. He was totally right.
      This post truly made my day. You cann’t imagine
      just how much time I had spent for this information! Thanks!

      Reply
    24. led neon sign

      I’ll right away grasp your rss feed as I can’t find your e-mail subscription hyperlink or newsletter service.
      Do you have any? Kindly permit me realize so that I could subscribe.

      Thanks.

      Reply

    Submit a Comment

    Your email address will not be published. Required fields are marked *

    Cari

    Kategori Artikel