Back

Problems: 419 status and cannot authorize


SirotinD posted 3 months ago

First, sorry for my english but i'll try to tell you my problems

Earlier site was on another server and worked on Apache+php Now Nginx+php-fpm PHP 7.1.22 Laravel 5.5.4

First - when i tried to login - i got ajax answer with status code 419 - unknown status But there is csrf-token in head section and i also added _token to ajax data but it didnt help

So i added login page to the exeptions for csrf token

Then everyting is ok with ajax status - but i cant login Auth::attempt - return true if after Auth::attempt i use Auth::check - it also return true but when ajax is finished and page reloads - i am not authorized

/routes/web.php

Route::middleware('guest')->get('/login', function () {
    return view('site.auth');
})->name('authUser');

Route::middleware('auth')->get('/logout', 'Auth\[email protected]')->name('logout');
Route::post('/registration', 'Site\[email protected]');
Route::post('/login', 'Auth\[email protected]');

my controller

 /**
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function auth(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email'    => 'required|email|exists:users,email',
            'password' => 'required|string|min:6',
        ]);

        $user = User::where(['email' => $request->email])->first();

        if (count($validator->errors()) || ! $user) {
            return response()->json(['loginErrors' => $validator->errors(), 'status' => 0], 400);
        }
		
		$rt = '/account';
		if($user->role == 'pp1')
			$rt = '/pp1';
		elseif($user->role == 'pp2')
			$rt = '/pp2';

        $credentials = $request->only(['email', 'password']);

        if (Auth::attempt($credentials)) {
            $response = redirect()->intended('/account');
			
			/*
			$user = auth()->user();
			print($user->id);
			print($user->name);
			print($user->email);
			die;
			*/
			
            return response()->json(['status' => 1,'redirect' => $rt, 'role'=> $user->role, 'url' => $response->headers->get('location')], 202);
        } else {
            return response()->json(['passwordError' => 'Не верный пароль','redirect' => $rt, 'status' => 0], 400);
        }
    }

	....
}

server config

server {
	server_name mysite.com [url=http://www.mysite.com]www.mysite.com[/url];
	charset UTF-8;
	index index.php index.html;
	disable_symlinks if_not_owner from=$root_path;
	include /etc/nginx/vhosts-includes/*.conf;
	include /etc/nginx/vhosts-resources/mysite.com/*.conf;
	access_log /var/www/httpd-logs/mysite.com.access.log;
	error_log /var/www/httpd-logs/mysite.com.error.log notice;
	ssi on;
	return 301 https://$host:443$request_uri; 
	set $root_path /var/www/www-root/data/www/mysite.com/public;
	root $root_path;

    location / {

        # First try and load files from the public folder, if they don't exist
        # then send the request through to laravel
        try_files $uri $uri/ /index.php;

        # Forward requests on to PHP-FPM
        location = /index.php {
            fastcgi_index index.php;
            fastcgi_intercept_errors on;
		fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
		try_files $uri =404;
		include fastcgi_params;
            fastcgi_pass unix:/var/www/php-fpm/www-root.sock;
        }
	}
	
    
    # If someone explicitly tries to load a PHP file return a 404 error,
    # always use url rewrites and never have the .php extension in the url
    location ~ \.php$ {
        return 404;
	}

	location @php {
		fastcgi_index index.php;
		fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f [email protected]";
		fastcgi_pass unix:/var/www/php-fpm/www-root.sock;
		fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
		try_files $uri =404;
		include fastcgi_params;
	}
	listen 5.23.49.172:80;
	gzip on;
	gzip_comp_level 9;
	gzip_disable "msie6";
	gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
}
server {
	server_name mysite.com [url=http://www.mysite.com]www.mysite.com[/url];
	ssl_certificate "/var/www/httpd-cert/www-root/mysite.com.crtca";
	ssl_certificate_key "/var/www/httpd-cert/www-root/mysite.com.key";
	ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
	ssl_prefer_server_ciphers on;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	add_header Strict-Transport-Security "max-age=31536000;";
	ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
	charset UTF-8;
	index index.php index.html;
	disable_symlinks if_not_owner from=$root_path;
	include /etc/nginx/vhosts-includes/*.conf;
	include /etc/nginx/vhosts-resources/mysite.com/*.conf;
	access_log /var/www/httpd-logs/mysite.com.access.log;
	error_log /var/www/httpd-logs/mysite.com.error.log notice;
	ssi on;
	  
	rewrite ^/(.*)/$ /$1 permanent; # remove trailing slash
	
	set $root_path /var/www/www-root/data/www/mysite.com/public;
	root $root_path;
	
    location / {

        # First try and load files from the public folder, if they don't exist
        # then send the request through to laravel
        try_files $uri $uri/ /index.php;

        # Forward requests on to PHP-FPM
        location = /index.php {
            fastcgi_index index.php;
            fastcgi_intercept_errors on;
		fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
		try_files $uri =404;
		include fastcgi_params;
            fastcgi_pass unix:/var/www/php-fpm/www-root.sock;
        }
	}
	
    
    # If someone explicitly tries to load a PHP file return a 404 error,
    # always use url rewrites and never have the .php extension in the url
    location ~ \.php$ {
        return 404;
	}
	
	location @php {
		fastcgi_index index.php;
		fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f [email protected]";
		fastcgi_pass unix:/var/www/php-fpm/www-root.sock;
		fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
		try_files $uri =404;
		include fastcgi_params;
	}
	listen 5.23.49.172:443 ssl;
	gzip on;
	gzip_comp_level 9;
	gzip_disable "msie6";
	gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
}

please help


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us