mercredi 1 avril 2020

Laravel Authentication API return wrong result

I'm new to laravel. I doing the API authentication right now. And I found my API didn't work as what shd be. I followed the guide of building it but in the end there is no error but wrong output. HERE IS MY CODE I can't find whats the error from it.

'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
        'member-api' => [
          'driver' => 'session',
          'provider' => 'members',
        ],
    ],
'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'members' => [
            'driver' => 'eloquent',
            'model' => App\Member::class,
        ],
class APIController extends Controller
{
    //
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required',
            'c_password' => 'required|same:password',
            'contact_no' => 'required'
          ]);
        if ($validator->fails()) {
          return response()->json(['error'=>$validator->errors()], 401);
        }

        $member = Member::create([
          'name' => $request->name,
          'email' => $request->email,
          'password' => bcrypt($request->password),
          'contact_no' => $request->contact_no
        ]);
        $success['token'] =  $member->createToken('asApp')->accessToken;
        $success['name'] =  $member->name;
        return response()->json(['success'=>$success], 200);

        //return response()->json(['success'=>'work'], 200);
    }

    public function login(Request $request)
    {
      $validator = Validator::make($request->all(), [
          'email' => 'required|email',
          'password' => 'required',
        ]);
      if ($validator->fails()) {
        return response()->json(['error'=>$validator->errors()], 401);
      }

      $credentials = $request->only('email', 'password');
      if(Auth::guard('member-api')->attempt($credentials))
      {
        $user = Auth::guard('member-api')->user();
        $success['token'] =  $user->createToken('asApp')->accessToken;
        return response()->json(['success' => $success], 200);
      }else {
        return response()->json(['error'=>'Email or password incorrect'], 401);

      }
    }
    public function details()
    {
      return response()->json(['user' => auth()->member()], 200);
    }

}

}
Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('login','APIController@login');
Route::post('register','APIController@register');

Route::group(['middleware' => 'auth:api'], function(){
        Route::post('userDetails', 'APIController@details');
});

I just followed the guide online. And I can't find the point why it return wrong output. Looking for help. TQ



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire