0

I know this question seems to be redundant, but I can't find the solution anywhere.

I'm using laravel-vujs mix and integrated the web routes and the api routes in the same project.

Now, I'm trying to create a register api function called registernative wherein it authenticates user and returns some status code.

for example

public function registernative(Request $request) {
    $user = User::where('email', '=', $request->email)->first();

        $response = [];

        if(!$user) { //If user not exist, create

            try {

                $slug = strtolower($request->firstname) . '-' . strtolower($request->lastname) . '-' . time();

                $user = new User();
                $user->name = $request->firstname . ' ' . $request->lastname;
                $user->email = $request->email;
                $user->slug = $slug;
                $user->password = Hash::make($request->password);
                $user->save();

                Auth::login($user);

                $response = ['status'=>1, 'msg'=>'ok', 'data'=>$user->toArray()];

            } catch (Exception $e) {
                $response = ['status'=> -1, 'msg'=>'bad', 'data'=>[]];
            }

        } else { //If exist, login
            $response = ['status'=> 0, 'msg'=>'user already exist', 'data'=>[]];

        }


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

now in my vue component's register function, I checked the status returned from the api, if its 1, I have to redirect to the default dashboard page.

the default page's inline-template goes like this (I don't know if its necessary providing this, but I'll provide it anyway).

@section('content')
    <defaultpage inline-template :user_session_details=" {{ Auth::user() }} ">
        <div>
            ...
            ...
            ...
        </div>

    </defaultpage>
@endsection

I tried using window.location.replace('http://localhost/defaultpage'); but it says 'Unauthenticated'.

Why is it unauthenticated knowing I Auth::login($user) it after saving?

Sorry for bad english, not a native speaker.

modi
  • 297
  • 2
  • 3
  • 15

1 Answers1

0

Sessions are not available in laravel api. To use sessions you can define your route in web routes or add \Illuminate\Session\Middleware\StartSession::class to api's middlewares in App/Http/Kernel.php

Mohammad Mirsafaei
  • 954
  • 1
  • 5
  • 16