samedi 30 mars 2019

Duplicate entry even on blank DB

I'm trying to implement JWT for my users. When I attempt to register I keep getting an error saying a user with the same email already exists. Even when I run off a blank users table.

The user is created once, so I don't know what the issue is. I'm attempting this in Postman.

Any ideas here

public function getToken($email, $password)
{
    $token = null;
    //$credentials = $request->only('email', 'password');
    try {
        if (!$token = JWTAuth::attempt( ['email'=>$email, 'password'=>$password])) {
            return response()->json([
                'response' => 'error',
                'message' => 'Password or email is invalid',
                'token'=>$token
            ]);
        }
    } catch (JWTAuthException $e) {
        return response()->json([
            'response' => 'error',
            'message' => 'Token creation failed',
        ]);
    }
    return $token;
}

public function register(Request $request)
{ 
    $payload = [
        'password'=>\Hash::make($request->password),
        'email'=>$request->email,
        'username'=>$request->username,
        'auth_token'=> ''
    ];

    $user = new \App\User($payload);
    if ($user->save())
    {

        $token = self::getToken($request->email, $request->password); // generate user token

        if (!is_string($token))  return response()->json(['success'=>false,'data'=>'Token generation failed'], 201);

        $user = \App\User::where('email', $request->email)->get()->first();

        $user->auth_token = $token; // update user token

        $user->save();

        $response = ['success'=>true, 'data'=>['name'=>$user->name,'id'=>$user->id,'email'=>$request->email,'auth_token'=>$token]];        
    }
    else
        $response = ['success'=>false, 'data'=>'Couldnt register user'];


    return response()->json($response, 201);
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire