samedi 22 octobre 2016

how to fetch data from related tables in Laravel with jwt-auth passing jwt-auth token as the parameter to the ->find() emthod

I have been using jwt-auth for laravel authentication and AngularJs as the front end,the login part http://127.0.0.1/login works fine but tried to search from the internet for ways to get around using the returned token to pass it to my User::with('userDetail', 'userContact')->find($id) so that the route http://ift.tt/2eU4toS to return joined tables(user_contact,user_detail) passing the token,decode it and return data related to that user only.kindly help. sample code

User Model

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $table = 'user';
protected $fillable = [
    'id', 'email', 'password'
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token', 'updated_at', 'created_at'
];

public function userContact()
{
    return $this->hasOne('App\UserContact', 'user_id');
}

public function userDetail()
{
    return $this->hasOne('App\UserDetail', 'user_id');
}
}

UserContact model

<?php

 namespace App;

use Illuminate\Database\Eloquent\Model;

class UserContact extends Model
{
protected $table = 'user_contact';

public function userContact()
{
    return $this->belongsTo('App\User');
}

}

UserDetail Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserDetail extends Model
{
protected $table = 'user_detail';

public function userDetail()
{
    return $this->belongsTo('App\User');
}

public function role()
{
    return $this->belongsTo('App\UserRole');
}
}

AuthenticationController code

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Facades\JWTAuth;

class AuthenticationController extends Controller
{
public function __construct()
{
    $this->middleware('jwt.auth', ['except' => ['authenticate', 'register',    'getLastInsertedMaximumUserId']]);
}

public function authenticate(Request $request)
{
    $credentials = $request->only('email', 'password');
    try {
        if (!$token = JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'Wrong Login Credentials'], 403);
        }
    } catch (JWTException $ex) {
        return response()->json(['error' => 'Login Failed']);
    }
    return response()->json(compact('token'));
}

public function getAuthenticatedUser()
{
    try {

        if (!$user = JWTAuth::parseToken()->authenticate()) {
            return response()->json(['user not found'], 404);
        } else {
        }

    } catch (JWTException$e) {

        return response()->json(['Kindly login'], $e->getStatusCode());

    } catch (JWTException $e) {

        return response()->json(['Sorry Invalid login Request'], $e->getStatusCode());

    } catch (JWTException $e) {

        return response()->json(['Wrong login Request'], $e->getStatusCode());

    }

    *

// the token is valid and we have found the user via the sub claim,this is where want ot do something like .User::with('userDetail', 'userContact')->find($id) but I want to replace $id with the token retruned from login,is it possible?

* return response()->json(compact('user')); }

/**
 * registers new user
 * @param Request $request
 */
public function register(Request $request)
{
    $id = 1000;
    $newUser = $request->all();
    $password = Hash::make($request->input('password'));
    $newUser['password'] = $password;
    if ($this->getLastInsertedMaximumUserId() !== null && $this->getLastInsertedMaximumUserId() >= $id) {
        $userId = $newUser['id'] = $this->getLastInsertedMaximumUserId() + 1;
    } else {
        $newUser['id'] = $id;
    }
    return User::create($newUser);
}

public function getLastInsertedMaximumUserId()
{
    $maximumId = DB::select('SELECT max(`id`) as `id` FROM `user`');
    foreach ($maximumId as $maxId) {
        return $maxId->id;
    }

}
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire