My Laravel 5.4 Logged in User Sessions wont persist - laravel

I'm having this weird issue with my app, it can't persist logged in user session using the Auth facade. Example, in my header view I put logged in user data as :
#if (Auth::check())
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<img src="{{route('getphoto', Auth::user()->image)}}" alt="" />
{{ Auth::user()->name }}
<span class="caret"></span>
</button>
</div>
#else
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
Not Logged In
</button>
</div>
#endif
At the first time after login the logged in user data in the header like photo and name will be displayed correctly, but after I refresh or moving to another page they disappear and indicate that the user is not logged in. I don't know what I did wrong, all I did with user login related function is just changing the email to username. How do I solve this ? I'm using Laravel 5.4

You need to set correct permissions for the storage directory:
chmod -R 755 storage
After installing Laravel, you may need to configure some permissions. Directories within the storage and the bootstrap/cache directories should be writable by your web server.
https://laravel.com/docs/5.4/installation

Related

how to hit an api on button click using laravel

i am new to working with API ,i have 2 buttons in my view file and i want to print the sheet which is available as an API ,
<div class="card">
<div class="card-body">
#include('admin.inc.messages')
<div class="row justify-content-center">
<div class="col-md-4">
<button type="button" class="btn btn-primary btn-block">Back</button>
<button type="button" class="btn btn-primary btn-block">Print Airway Bill</button>
<button type="button" class="btn btn-primary btn-block">Print Invoice</button>
</div>
</div>
</div>
</div>
i have got the path , i want to ask where should i define the path for that , in the web.php or api.php
and second thing how should i give the path in the buttons
any help will be appreciated !
You will need to consume your own api, most likely, or use ajax request. This can be done via passport, seen here in the documentation. If you want you can always bypass, however you will not have the ability to utilize middleware with those requests. Meaning, that your project will be subjected to potential security risk as the request will not be subjected to route middleware. Which protect and assist you in some regards.
For example, if you cannot authenticate against a middleware you will not have the ability to determine which/what user is requesting data. Another comparison one might make would be the ability to login against a user, but then not have the ability through out the application pages to know which user was authenticated. This is comparable to when you log in to stackoverflow you can obviously see you user information/profile in the top right with your picture etc... If you by pass you will not have that ability.
Therefore, I would highly recommend visiting the link posted above and consider consuming your own api or creating a service to do so.

Laravel if condtition with two buttons

i am using Ajax with laravel, i have two buttons, Add and Update, what i am trying to achieve is, if i click on Update button,
Laravel #if statement should run,
#if(isset($teacher))
if i click on add button Laravel #else statement should run,
Complete Code:
<button class="btn btn-secondary" id="add" > </span > ADD NEW</button>
<button class="btn btn-secondary" id="update"> UPDATE OLD</button>
#if(isset($teacher))
#foreach($teachers as $teacher)
#endforeach
<button class="btn btn-secondary" id="update-t"> UPDATE Teacher</button>
#else
<button class="btn btn-secondary" id="add-t" > ADD Teacher</button>
#endif
You are misunderstanding the way Laravel Blade templates work.
Blade template is rendered into PHP, than executed to render HTML, which is sent to your browser to render it client-side. This is the moment when your JavaScript functions will start working, and they cannot interfere with what was previously rendered, as JavaScript will only 'see' the final HTML render.
Solution to your problem if you want to use AJAX, is to create some kind of API, which will be processed in AJAX's success function.

Cannot check if user has permissions

Im using the laravel spatie/permissions bundle.
Working with that bundle i ran in to a problem. I have made multiple roles and have used the $role->givePermissionTo('view users') method to give my role the permission to see my users.
Now when i use #can('view users') i cannot see the link.
i made it like this:
#can('view users')
<li class="nav-item">
<a class="nav-link font-weight-bold" href="{{ route('user.index') }}">
{{ __('User Management') }}
</a>
</li>
#endcan
When i check if the user has the permission it says it does. How come it doesnt show the link?

How do I hide login link and show logout link in homepage navbar once user logs in and gets re-directed to homepage?

I'm working on a Spring boot based web application and the homepage contains a boostrap navbar with Login and Register link.
Once user logs in (by clicking login link from homepage), they will be re-directed to home page again (unless they visit any other link before spring security kicks in for A&A). On re-direct, I want to hide login and register links from the navbar and show logout link in their place. Becuase I want to insert the navbar in all the pages of application.
I want to know what is the best/standard way of doing this when the requirement is re-direction to same page.
One solution that I can think of is to check and obtain a user-principal object from spring security, pass it to thymeleaf template and check that if userprincipal object is present in the request attribute to the thymeleaf template, that means a user has logged in and I can then hide(not-render) login & register links and show(render) logout link. If not, show login & register links and hide logout link. I'm wondering, is this a correct way to do so? This feels like a hack to me and hence want to know if there is any standard way of doing this.
There is a Thymeleaf Spring Security extension that you can use:
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
Add the namespace
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
And then you can use it:
<li sec:authorize="!isAuthenticated()" class="nav-item">
<a class="btn btn-outline-light" th:href="#{/login}">Login</a>
</li>
<li sec:authorize="isAuthenticated()" class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false" sec:authentication="name">
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="pl-3" th:href="#{/password}">Passwort ändern</a>
<div class="dropdown-divider"></div>
<a class="pl-3" th:href="#{/logout}">Logout</a>
</div>
</li>
Please find the whole documentation here:
https://github.com/thymeleaf/thymeleaf-extras-springsecurity

Lravel 5.0 - (This site can’t be reached The connection was reset)

I receive the following error message in "some" of my laravel webpages project:
This site can’t be reached
The connection was reset.
ERR_CONNECTION_RESET
Any advise please
I have found that the problem is because of the commenting syntax i am using. for example:
{{--
<div class="btn-group">
<button type="button" class="btn btn-default">
<span class="glyphicon glyphicon-ok"></span>
</button>
</div>
--}}

Resources