TEster
TEster

Reputation: 359

Laravel sanctum - unathenticated after refresh. Why am I being unauthenticated?

I have a simple authentication system in which I use vue to handle frontend and Laravel to handle the backend. Laravel Sanctum is being used to authentication.

When I login, it works fine and within Laravel I can dump the logged in user fine.

however when i refresh the page I am no longer authenticated.

As you can see I'll be using app.blade.php to tell javascript who the logged in user is, but isLoggedin is always false.This seems wrong to me. Am I missing something?

Any help is appricated.

api.php

Route::post('login', [UserController::class, 'login']);

UsersController@login

public function login(Request $request)
    {
        $credentials = [
            'email' => $request->email,
            'password' => $request->password,
        ];

        if (Auth::attempt($credentials)) {
            $request->session()->regenerate();
            $success = true;
            $message = 'User login successfully';
            $user = auth()->user();
        } else {
            $success = false;
            $message = 'Unauthorised';
            $user = null;
        }

        // response
        $response = [
            'success' => $success,
            'message' => $message,
            'user' => $user
        ];
        return response()->json($response);
    }

.env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=cookie
SANCTUM_STATEFUL_DOMAIN=localhost
SESSION_DOMAIN=localhost
SESSION_LIFETIME=120
SESSION_SECURE_COOKIE=false;

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

app.blade.php

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" value="{{ csrf_token() }}"/>
    <title>{{env('APP_NAME')}}</title>
    <link href="{{ mix('css/app.css') }}" type="text/css" rel="stylesheet"/>
</head>
<body>
@if (Auth::check())
    @php
    $user_auth_data = [
        'isLoggedin' => true,
        'user' =>  Auth::user()
    ];
    @endphp
@else
    @php
    $user_auth_data = [
        'isLoggedin' => false
    ];
    @endphp
@endif
<script>
    window.Laravel = JSON.parse(atob('{{ base64_encode(json_encode($user_auth_data)) }}'));
    console.log(window.Laravel);
</script>

<div id="app">
</div>
<script src="{{ mix('js/app.js') }}" type="text/javascript"></script>
</body>
</html>```

Upvotes: 0

Views: 2384

Answers (1)

TEster
TEster

Reputation: 359

Fixed it.

Change SANCTUM_STATEFUL_DOMAIN=localhost To: SANCTUM_STATEFUL_DOMAINS=localhost:9192

You need the port number.

Upvotes: 1

Related Questions