Skip to content

Instantly share code, notes, and snippets.

@darkylmnx
Last active August 23, 2018 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkylmnx/17a8ec705f0245a21f6a0c0672d2be6e to your computer and use it in GitHub Desktop.
Save darkylmnx/17a8ec705f0245a21f6a0c0672d2be6e to your computer and use it in GitHub Desktop.
Expose Auth User Object to a Vue component in a Multi-App-Page
<?php require 'header.php' ?>
<?php if (isLogged()) : ?>
<script>
// this is one way, the global var way
// the function getAuthUser() returns an Object of the user
// oui json encode it which will give something like : { name: "", ... }
// and this when rendered here is a string, but in a browser it's a js object so no need for quotes or it will be a string in JS too
window.user = <?= json_encode(getAuthUser()) ?>;
</script>
<?php endif ?>
<script src="my-vue-components.js"></script>
<?php require 'footer.php' ?>
<?php
function getRandomComponent($user) {
?>
<script>
new Vue({
el: '#widget',
data() {
return {
auth: <?= $user ?>
}
}
});
</script>
<?php
}
require 'header.php';
if (isLogged()) {
getRandomComponent(getAuthUser());
}
?>
<script src="my-vue-components.js"></script>
<?php require 'footer.php' ?>
<?php require 'header.php' ?>
<?php if (isLogged()) : ?>
<script>
const VueAuthMixin = {
methods: {
getAuthUser() {
return <?= json_encode(getAuthUser()) ?>;
}
}
};
</script>
<?php else : ?>
<script>
const VueAuthMixin = {}
</script>
<?php endif ?>
<!-- here, in this JS, we asume you could include the previous mixin anywhere -->
<script src="my-vue-components.js"></script>
<?php require 'footer.php' ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment