lundi 24 août 2020

Llaravel and blade , api and frontal in the same project

I have a project with Laravel and I have Passport installed , because i'm using it to manage authorization and authentication with the project's API.

I have a login method that returns token and then from the responsive wep app I send this token on each request. The example would be, I make the call to / login, I receive the token I store it and in the other calls for example / api / companies I send the token and if it is valid the api returns the correct answer. This works OK

What I can't get to work is within the same project to authenticate through a form that calls another method

I have the problem in that within the same project I want to authenticate through a form that calls another method and save the data in the laravel session.

Method I use from the progressive web apps

public function login()
{
    if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
        $user = Auth::user();
        $success['token'] =  $user->createToken('MyApp')-> accessToken;

        return response()->json(['success' => $success,'email' => request('email')], $this-> successStatus);
    }
    else{
        return response()->json(['error'=>'Unauthorised'], 401);
    }
}

This is the other method that I want to use from the same laravel application, using sessions. The idea is, when a user is correctly logged in, they will save their email session.

 /**
 * @param Request $request
 * @return \Illuminate\Http\JsonResponse
 */
public function loginweb(Request $request)
{

    $request->session()->put('login_email', $request->get('email'));

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


    if (null !== $user) {

        $request->session()->put('login_email', $request->get('email'));
        $request->session()->put('user_id', $user->id);

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

    return response()->json('Error',401);
}

The problem is that I use the loginweb function, and then I access another form and I have lost the session data.

For example, I access this route (in web.php)

Route::get('/forms/list', 'Front\FormController@list')->name('front.user.forms.list');

Content from this method

    public function list(Request $request)
{
    if ($request->get('email') !== null) {
        $request->session()->put('login_email', $request->get('email'));
        $request->session()->put('logged', true);
    }

     dd($request->session());

     return view ('front.user.forms.list');
}

This last "dd" dont show me session variables.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/31pX5aX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire