Last active
August 23, 2018 14:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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