M
M
MasterCopipaster2020-07-02 14:02:38
Laravel
MasterCopipaster, 2020-07-02 14:02:38

How does authorization work in laravel?

Hello everyone, I have laravel 5.8.38 - and the following question arose.
There is a standard method in the lara which logs in the user from the controller, if I do this
Auth::login($user,true);

Auth::login($user,true);
dump(auth()->user());

Then auth()->user() displays the desired user, everything is ok, but how the hell does he log in? how does it remember that this user is logged in? I thought that this is done by session cookies, but there are no cookies at all.
after successful authorization, I return json In theory, session cookies should come with the json response, but they are not there at all. wtf? Do I have to manually set these cookies? then why is Auth::login needed? The controller itself:
return response()->json(['user' => $user], 200);
$code = $request->code;
        preg_match("/(7[0-9]{10})/", $request->phone, $matches);
        $phone = $matches[0];
        $user = User::query()
            ->with('orders.items.product')
            ->where('phone', $phone)
            ->where('sms_code', $code)
            ->first();

        $token = Str::random(60);
        $token = hash('sha256', $token);
        if ($user) {
            // Log in current user
            $user->update([
                'token' => $token,
                'sms_code' => null
            ]);
            Auth::login($user,true);
            return response()->json(['user' => $user], 200);
        } else {
            return response()
                ->json(['message' => 'Go away, pls'. $phone], 404)
                ->withCookie(cookie('token', $token, 0));
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2020-07-02
@MasterCopipaster

If the driver is session, then it is stored somewhere in the session, if the driver is different, then depending on it. Either basic authorization for each request, or tokens (for example, passport or sanctum), or you can even invent something yourself and nail it, for example, by browser ip+fingerprint

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question