0

i'm developing my website with angularjs and laravel5. i wrote code for login and registration page in both angularjs and laravel5 where validate my value and insert everything works good but redirect url in laravel 5 not occur . i wrote code like return redirect('Home/profile') in login controller. it returns total page to angularjs controller not redirecting page.

routes.php in laravel:

Route::group(array('prefix'=>'api'),function(){
    Route::resource('register','Registration\RegisterController@basicForm');
    Route::resource('login','Registration\RegisterController@makeLogin');
});

my controller :

public function makeLogin()
{
    $email=Input::get('email');
    $pwd=Input::get('pwd');
    $verify=Authenticated::attempt($email,$pwd);
    if($verify)
    {
        return redirect('Home/profile');
    }
    else if($verify=='user')
    {
        return redirect('/')->with('Email address mismatch');
    }
    else if($verify=='pwd')
    {
        return redirect('/')->with('Password Authentication Failed');
    }
}

i send post request from angular js controller via factory method:

this.scope.authUserInfo.authenticateUser(this.scope.signin).then(function(data){
    console.log(data.data);
});

In this console.log,display 'Home/profile' page

Marco Pallante
  • 3,923
  • 1
  • 21
  • 26
Sekar
  • 11
  • 7
  • We can't guess at your login process flow or your code. The process is totally unclear and you provided no code. Please see http://stackoverflow.com/help/how-to-ask – charlietfl Jul 16 '15 at 02:29
  • `if (Auth::attempt(['email' => $email, 'password' => $password])) { return redirect()->intended('dashboard'); }` – atinder Jul 16 '15 at 03:28
  • i tried this method also.it return total html page to my angular js controller not redirected page – Sekar Jul 16 '15 at 04:48

1 Answers1

0

I can see many mistakes in the Laravel code.

What is Authenticated in your code? The authentication service in Laravel is called Auth. The way you use the attempt() method is not correct; this is the method signature:

attempt(array $credentials = array(), bool $remember = false, bool $login = true)

so you should pass the email and the password in the first parameter as an array, something like:

$verify = Auth::attempt(['email' => $email, 'password' => 

Moreover, the attempt() method returns a boolean: true on success and false on failure. Your code

if ($verify) {
    // ...
} else if ($verify == 'user') {
    // ...
} else if ($verify == 'pwd') {
    // ...
}

has no sense and you never run the else parts because on failure false is always different from either 'user' or 'pwd'. So, when the authentication fails you reach the end of the makeLogin() method and Laravel returns a blank page.

You should use something like:

if ($verify) {
    // authenticated: go to the profile page
} else {
    // username OR password are wrong
}

(In my opinion you shouldn't give hints on what of the two is wrong for security reasons: a potential attacker would know if he/she guessed a right email and concentrate the attempts on guessing the password.)

If you really want to give a hint to the user on what was wrong with her data, you should use a different technique, like searching the users table for a record with the right email to know whether the user exists (the provided password was wrong) or not (the provided email was wrong).

On the client side, I don't think Angular will redirect on your own. See answer to Handle an express redirect from Angular POST, even if in that question the server uses ExpressJs and not Laravel, but the basics are the same.

You should understand that in most cases an Angular client expect to receive only data and not a full HTML page. Here is my little attempt to do what you want:

On the Laravel side:

public function makeLogin(Request $request)
{
    $email = $request->get('email');
    $pwd = $request->get('pwd');

    if (Auth::attempt(['email' => $email, 'password' => $pwd])) {
        // authenticated!

        // if you return an array from a controller public method, Laravel
        // will convert it to JSON; I also use the url() Laravel helper to
        // generate a fully qualified url to the path
        return [
            'status' => 'redirect',
            'to' => url('home/profile')
        ];
    }

    // failed
    return [
        'status' => 'failed',
        'message' => 'Email or password are wrong'
    ];
}

Now the method will return a JSON answer. On the Angular side, you can do something like (I'm not an Angular guru, there could be mistakes here):

this.scope.authUserInfo.authenticateUser(this.scope.signin).then(function(data) {
    if (data.response == 'redirect') {
        $location.url(data.to);
    } else {
        // failed: you can show the error message
        console.log(data.message);
    }
});

UPDATE

I noticed there is something wrong in the routes too. Your controller does not seems a resourceful controller, so don't use the Route::resource() method. Use the get() and post() methods instead:

Route::group(array('prefix'=>'api'),function(){
    Route::get('register', 'Registration\RegisterController@basicForm');
    Route::post('login', 'Registration\RegisterController@makeLogin');
});

so that you can give the method that should be called in your controller.

Community
  • 1
  • 1
Marco Pallante
  • 3,923
  • 1
  • 21
  • 26
  • thanks @marco pallante .i didn't use Auth helper functionality.i wrote customized Authenticated function – Sekar Jul 16 '15 at 12:24
  • Ok, if you use a custom `Authenticated` class with your custom `attempt()` method, then you are not limited to a boolean result, but the idea behind my answer remains the same :) – Marco Pallante Jul 16 '15 at 13:16
  • how to use memcached in my project.i am creating social network like linked in . please refer some idea – Sekar Jul 17 '15 at 10:26
  • That's another question unrelated to this one ;) (but I would suggest to take a deep look at the Laravel cache documentation.) – Marco Pallante Jul 17 '15 at 13:11
  • okay @marco.but i am beginner so i need clear about memcached and session handling process – Sekar Jul 17 '15 at 13:59
  • If you have *specific* doubts about something, then ask here on StackOverflow. But if you just need general understanding of how things work in Laravel, then maybe you have to search for tutorials and guides elsewhere, like the Laravel docs pages or Laracasts, just to name a few starting points. – Marco Pallante Jul 17 '15 at 14:09