[英]How do I check if user is 'logged in' in a react component when using laravel authentication?
I have setup laravel authentication in my website. 我在我的网站上设置了laravel身份验证。 It works very well but I want to know how I can check if a user is logged in when inside a react component and display his/her specific content. 它工作得很好,但我想知道如何检查用户是否在反应组件内部登录并显示他/她的特定内容。
One approach I have used to solve the problem with checking if the user is logged in is to attach it in your main blade file as 'global data'. 我用来检查用户是否已登录的问题的一种方法是将其作为“全局数据”附加到主刀片文件中。 First set up your controller. 首先设置你的控制器。
public function index()
{
return view('welcome')->with(["globalData" => collect([
'user' => Auth::user()
]);
}
Then in your root blade file 然后在您的根刀片文件中
<script>
let globalData = "{!! $globalData->toJson() !!}";
</script>
Then you can access a user property anywhere in your components by refering to globalData.user
and thus test if user
is an object or null/empty. 然后,您可以通过globalData.user
来访问组件中任何位置的用户属性,从而测试user
是对象还是null / empty。
class GuiController extends Controller
{
public function __construct() {
$this->data = [
'projects' => $this->projects(),
'packs' => Pack::all(),
'stimpack_io_token' => env('STIMPACK_IO_TOKEN'),
'stimpack_data_url' => env('STIMPACK_DATA_URL'),
'manipulatorData' => ManipulatorController::attachStartupData()
];
}
public function index()
{
return view('welcome')->with(["data" => collect($this->data)]);
}
<body>
<div id="main"></div>
<script>
let data = {!! $data->toJson() !!};
</script>
<script src="{{asset('js/app.js')}}" ></script>
</body>
upload() {
var compiler = new Compiler(this.props.engine);
var compiled = compiler.compile();
$.ajax({
type: "POST",
beforeSend: function(request) {
request.setRequestHeader("stimpack-io-token", data.stimpack_io_token);
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.