0

I am implementing access to different databases, but when using the middleware('auth:api') it says that I am not authenticated and when I login I get the token.

this only happens when the DB is not the default

im on laravel 5.8

public function login(LoginRequest $request, $db)
    {
        Config::set("database.connections.mysql.database", $db);
        if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
            $this->user = Auth::user();
            $this->user->token = $this->user->createToken('ControlEscolar')->accessToken;
            $status = 200;
        } else {
            $this->user['error'] = "Unauthorized";
            $status = 401;
        }
        return response()->json($this->user, $status);
    }

public function userData($db)
    {
        Config::set("database.connections.mysql.database", $db);
        $user = userData::whereUserId(Auth::user()->id)
            ->first();
        return $user;
    }

on the login function i get this...

{
    "id": 1,
    "name": "name of user",
    ...
    "token": "eyJ0eXAiO...CFU"
}

so it works

but in the second at user data i get this

{
    "error": {
        "message": "Unauthenticated.",
        "status_code": 500,
        "debug": {
            "line": 67,
            "file": "/Users/.../vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php",
            "class": "Illuminate\\Auth\\AuthenticationException",
            ...

someone has a clue how to do it, or what am I doing wrong

1 Answers1

0

I resolve it doing configure set on the route file like above:

$api->group(['prefix' => 'auth'], function (Router $api) {
    Config::set("database.connections.mysql.database", $db);
    $api->post('singup', 'App\\Api\\V1\\Controllers\\SignUpController@signUp');
});

By de way i'm using dingo Api