M
M
Manuk Minasyan2018-01-10 02:16:13
Laravel
Manuk Minasyan, 2018-01-10 02:16:13

Ajax Login and Registration LARAVEL 5.5 how to do?

I want to implement Login and registration LARAVEL 5.5, on ajax. how can i do? on LoginController I can’t get the data on the Request in any way, just empty, or what other optimal options are there?

Thank you

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Manuk Minasyan, 2018-01-10
@ARMbrain

JS

$('#login-form').on('submit', function(e){
   e.preventDefault();
    $.ajax({
        url: $(this).attr('action'),
        data: {"username":'asdasdasd',"password":'564a6sd'},
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        type: 'POST',
        dataType: 'JSON',
        success: function (html) {
            console.log(html);
        }
    });

   console.log('Submit');
});

Routes
LoginController.php
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function username()
    {
        return 'username';
    }

    public function authenticated(Request $request)
    {
       //
    return $request->all();
    }
}

E
eukoch, 2019-02-22
@eukoch

Error in Routes. You need to call the login method and not authenticated
Laravel will redirect on successful login to authenticated. Here is how this fragment is arranged in the protected function handleUserWasAuthenticated

if (method_exists($this, 'authenticated')) {
        return $this ->authenticated($request, Auth::user());
    }

But the main handler is still login .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question