0

I have 2 strings

$password_api = VSE::user('password',$username); //abc
$password     = Input::get('password'); //abc

If they're matched, I want to log the user in

if ( $password == $password_api ) {

    // Not sure what to do in here ... 
    $auth = Auth::attempt();
}

Restriction:

I don't have Laravel user model or a users table. It's all via API call.

How can I do that in Laravel 5 ?

2
  • Do you have a Laravel User model? Commented Oct 29, 2015 at 15:02
  • I'm sorry, good call. No I don't have it. Commented Oct 30, 2015 at 14:22

4 Answers 4

1

I hope this helps

here i assume you have a "users" table with user data. your api username is same as the "users" table username field.

Controller function

public function login()
{
$password_api = VSE::user('password',$username);
$password     = Input::get('password'); 
if ( $password == $password_api ):
  if (Auth::attempt(['username' => $username, 'password' => $password])) {
  // Authentication passed...
  }
  else{
  // Authentication failed...
  }
endif;
}

IF YOU DO NOT HAVE "users" TABLE.

i think the simple way is to create a session value when password matches and create a middleware/filter for that auth.

controller function

public function login()
{
$password_api = VSE::user('password',$username);
$password     = Input::get('password'); 
if ( $password == $password_api ):
  Session::put('api_auth','1')
endif;
}

to check if user is logged in

if(Session::get('api_auth')==1):
//logged in
endif;

Middleware creation

refer http://laravel.com/docs/5.0/middleware for more info

<?php

namespace App\Http\Middleware;

use Closure;
use Session;

class apiAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        if (Session::get('api_auth') == 1) {
            return $next($request);
        }

        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
            //no authentication';
            return redirect()->route('login.page')->with('error','you need to login to view the page you requested');
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Very precise answer ! I like it
no problem just put the question as resolved if you think it's right.
1

I'll just assume you have a User model that is implementing the Authenticatable contract.

If that's the case you can just do the following:

$user = User::where('username', '=', 'johndoe'); // find by username
Auth::login($user);

You also don't have to assign Auth to a variable for this to work. After you've done the login call you can redirect the user or do whatever has to be done in your application.

Comments

1

Basic Authentication

Laravel's Auth provide authentication services

Auth::attempt([
     'username' => Input::get('username'),
     'password' => Input::get('password')
]);

The attempt method will return true if authentication was successful.

Remember to include the Auth facade at the top of the class use Auth;

Using Auth would be helpful to access the Authenticated users details through out the application

Ex: Auth::user()->id would get you the Auth users id anywhere in your application.

Please Refer: Laravel Authentication Docs

Hope this is helpful

1 Comment

Thanks for trying. Unfortunately, I don't have a users table locally.
0

Whoa, wrong way you're going here mate - you do not want to ask a password from the API - you want to let the API validate the authentication. Send a password and username to the API and the API will tell you if you're right - then the API will log you in. I suggest you check out this oauth2 project; https://github.com/lucadegasperi/oauth2-server-laravel/wiki/Laravel-5-Installation

3 Comments

Like I mention, this is just the beginning of the project, and I have to work with what I have. Right now, I just want to Auth my users in if the password is matched. I totally understand that this is not the proper way/ the right way of doing it.
Right now, everything is Hard Coded Data, Plus, I only got 2 users. I was hoping that someone can give me a little push here. I really appreciated your comment.
Hmm, sorry, I have 0 experience with the auth module from laravel itself. I can't help you further. I would just check out the methods that the "Auth" class exposes. There might be something in there that you can use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.