send token jwt Laravel

0

I followed this tutorial for authentication in laravel by jwt and in the end a token comes back to me.

In postman it works perfectly and when wanting to enter any route I send it by postman the header: "Authorization: Bearer {{token}}" and opens it perfectly.

The problem is that I do not know how to pass that token that gave me the header of a view that is called by the route and it is protected with jwt-auth therefore, when accessing the route, it marks me: UnauthorizedHttpException Token not provided

In the web routes file:

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/',function(){
    return view('login');
});

Route::get('inicio',function(){
    return view('login');
});



Route::middleware(['jwt.auth'])-> group(function(){

//  Route::get('test', function(){
//     return Response::view('test')->header('Content-Type', 'ajjajaja');

//     // return view('test');
//     // return view($token);

// });
  Route::get('test2', function(){
    return view('test2');
});

 Route::get('map2', function(){
    return view('modulos/map_Heatmap');
});

Route::get('home',function(){
    return view('modulos/home');
});

Route::get('results',function(){
    return view('modulos/results');
});

// ANALISIS 
Route::get('analisis',function(){
    return ('analisis'); 
});
//ALL
Route::get('analisis1/{id1}/{id2}',function($id1,$id2){
    return view('modulos/'.$id2.'/analisis1')->with('id',$id1)->with('idd',$id2);
    // return view('modulos/$id/analisis1'); 
});
//SURFACES
Route::get('analisis2',function(){
    return view('analisis2'); 
});

Route::get('analisis2_1/{id1}/{id2}',function($id1,$id2){
    return view('modulos/'.$id2.'/analisis2-1')->with('id',$id1)->with('idd',$id2);
});

Route::get('analisis2_2/{id1}/{id2}',function($id1,$id2){
    return view('modulos/'.$id2.'/analisis2-2')->with('id',$id1)->with('idd',$id2);
});

Route::get('analisis3/{id1}/{id2}',function($id1,$id2){
    return view('modulos/'.$id2.'/analisis3')->with('id',$id1)->with('idd',$id2);
});

Route::get('Map/{id}',function($id1){
    return view('modulos/Map')->with('id',$id1);
});

Route::get('res','modulos\muestrasController@resultados');
Route::get('l','modulos\loginController@log');
Route::get('analisis2_1/2/1','modulos\muestrasController@jsonlist');

});

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

In the api routes file:

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

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



Route::middleware(['jwt.auth'])-> group(function(){


});
Route::post('login','AuthenticateController@authenticate')-> name('login');

In the Authentication driver:

<?php

namespace App\Http\Controllers;



use Illuminate\Http\Request;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Support\Facades\Auth;

class AuthenticateController extends Controller
{
    //
    public function authenticate(Request $request){

         // $header= $request -> header('Authorization');
         $credentials = $request -> only('email' , 'password');

         // return $header;



         try {
            if(!$token = JWTAuth::attempt($credentials)){
                // return response()-> json(['error'->'invalid_credentials'], 401);
                return "response()-> json(['error'->'invalid_credentials'], 401);";

            }

         } catch (Exception $e) {
            return "response()-> json(['error'->'error_crear_token'], 500);";
         }

         $response=compact('token');
         $response['user']= Auth::user();

         return $response;


         //Algo parecido es lo que quiero hacer...
         return response()->redirectTo("https://mobile-test.taag.mx/home")->header('Authorization', 'Bearer '.$token);

    }

    public function getAuthUser(Request $request)
    {
        $this->validate($request, [
            'token' => 'required'
        ]);

        $user = JWTAuth::authenticate($request->token);

        return response()->json(['user' => $user]);
    }

}

I hope you have given me to understand and thank you in advance for your help.

    
asked by Scamer Rosales 07.09.2018 в 23:28
source

0 answers