Passing logged-in user credentials from twig to vue.
This commit is contained in:
@@ -52,6 +52,7 @@ export default {
|
|||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
...mapState('booksmodule', ['books']),
|
...mapState('booksmodule', ['books']),
|
||||||
|
...mapState('usermodule', ['user']),
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
...mapActions('booksmodule', [
|
...mapActions('booksmodule', [
|
||||||
@@ -62,7 +63,7 @@ export default {
|
|||||||
this.findAll(this.searchTerm);
|
this.findAll(this.searchTerm);
|
||||||
},
|
},
|
||||||
updateHistory: function () {
|
updateHistory: function () {
|
||||||
if (history.pushState) {
|
if (this.searchTerm && history.pushState) {
|
||||||
let url = window.location.protocol + "//" + window.location.host + window.location.pathname + '?search=' + this.searchTerm;
|
let url = window.location.protocol + "//" + window.location.host + window.location.pathname + '?search=' + this.searchTerm;
|
||||||
window.history.pushState({path: url}, '', url);
|
window.history.pushState({path: url}, '', url);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {
|
|||||||
LOGIN_SUCCESS,
|
LOGIN_SUCCESS,
|
||||||
LOGIN_ERROR,
|
LOGIN_ERROR,
|
||||||
STORE_USER_INFO,
|
STORE_USER_INFO,
|
||||||
|
LOGIN_STOP,
|
||||||
} from '../mutation-types.js'
|
} from '../mutation-types.js'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -11,7 +12,7 @@ export default {
|
|||||||
state: {
|
state: {
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
error: null,
|
error: null,
|
||||||
user: null,
|
user: window.user,
|
||||||
userUri: null,
|
userUri: null,
|
||||||
},
|
},
|
||||||
getters: {
|
getters: {
|
||||||
@@ -36,6 +37,9 @@ export default {
|
|||||||
state.error = null;
|
state.error = null;
|
||||||
state.userUri = userUri;
|
state.userUri = userUri;
|
||||||
},
|
},
|
||||||
|
[LOGIN_STOP](state, userUri) {
|
||||||
|
state.isLoading = false;
|
||||||
|
},
|
||||||
[STORE_USER_INFO](state, user) {
|
[STORE_USER_INFO](state, user) {
|
||||||
state.isLoading = false;
|
state.isLoading = false;
|
||||||
state.error = null;
|
state.error = null;
|
||||||
@@ -60,8 +64,6 @@ export default {
|
|||||||
dispatch('getUserInfo', response.headers.location)
|
dispatch('getUserInfo', response.headers.location)
|
||||||
commit(LOGIN_SUCCESS, response.headers.location);
|
commit(LOGIN_SUCCESS, response.headers.location);
|
||||||
//this.$emit('user-authenticated', userUri);
|
//this.$emit('user-authenticated', userUri);
|
||||||
//this.email = '';
|
|
||||||
//this.password = '';
|
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
if (error.response.data.error) {
|
if (error.response.data.error) {
|
||||||
|
|
||||||
@@ -70,7 +72,7 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}).finally(() => {
|
}).finally(() => {
|
||||||
// this.isLoading = false;
|
commit(LOGIN_STOP);
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
async getUserInfo({commit}, userUri) {
|
async getUserInfo({commit}, userUri) {
|
||||||
|
|||||||
@@ -13,5 +13,6 @@ export const
|
|||||||
LOGIN_START = "LOGIN_START",
|
LOGIN_START = "LOGIN_START",
|
||||||
LOGIN_SUCCESS = "LOGIN_SUCCESS",
|
LOGIN_SUCCESS = "LOGIN_SUCCESS",
|
||||||
LOGIN_ERROR = "LOGIN_ERROR",
|
LOGIN_ERROR = "LOGIN_ERROR",
|
||||||
|
LOGIN_STOP = "LOGIN_STOP",
|
||||||
STORE_USER_INFO = "STORE_USER_INFO"
|
STORE_USER_INFO = "STORE_USER_INFO"
|
||||||
;
|
;
|
||||||
@@ -7,8 +7,12 @@ use App\Repository\UserRepository;
|
|||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
||||||
use Symfony\Component\Security\Core\User\UserInterface;
|
use Symfony\Component\Security\Core\User\UserInterface;
|
||||||
|
use Symfony\Component\Serializer\Annotation\Groups;
|
||||||
|
|
||||||
#[ApiResource]
|
#[ApiResource(collectionOperations: [
|
||||||
|
"get",
|
||||||
|
"post" => ["security" => "is_granted('ROLE_USER')"]
|
||||||
|
], normalizationContext: ['groups' => ['user:read']])]
|
||||||
#[ORM\Entity(repositoryClass: UserRepository::class)]
|
#[ORM\Entity(repositoryClass: UserRepository::class)]
|
||||||
#[ORM\Table(name: '`user`')]
|
#[ORM\Table(name: '`user`')]
|
||||||
class User implements UserInterface, PasswordAuthenticatedUserInterface
|
class User implements UserInterface, PasswordAuthenticatedUserInterface
|
||||||
@@ -19,9 +23,11 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
|
|||||||
private $id;
|
private $id;
|
||||||
|
|
||||||
#[ORM\Column(type: 'string', length: 180, unique: true)]
|
#[ORM\Column(type: 'string', length: 180, unique: true)]
|
||||||
|
#[Groups('user:read')]
|
||||||
private $email;
|
private $email;
|
||||||
|
|
||||||
#[ORM\Column(type: 'json')]
|
#[ORM\Column(type: 'json')]
|
||||||
|
#[Groups('user:read')]
|
||||||
private $roles = [];
|
private $roles = [];
|
||||||
|
|
||||||
#[ORM\Column(type: 'string')]
|
#[ORM\Column(type: 'string')]
|
||||||
@@ -51,7 +57,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
|
|||||||
*/
|
*/
|
||||||
public function getUserIdentifier(): string
|
public function getUserIdentifier(): string
|
||||||
{
|
{
|
||||||
return (string) $this->email;
|
return (string)$this->email;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ namespace App\Twig;
|
|||||||
|
|
||||||
use App\Form\SearchType;
|
use App\Form\SearchType;
|
||||||
use Symfony\Component\Form\FormFactoryInterface;
|
use Symfony\Component\Form\FormFactoryInterface;
|
||||||
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||||
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
use Twig\Extension\AbstractExtension;
|
use Twig\Extension\AbstractExtension;
|
||||||
use Twig\TwigFilter;
|
use Twig\TwigFilter;
|
||||||
use Twig\TwigFunction;
|
use Twig\TwigFunction;
|
||||||
@@ -12,11 +14,18 @@ use Twig\TwigFunction;
|
|||||||
class AppExtension extends AbstractExtension
|
class AppExtension extends AbstractExtension
|
||||||
{
|
{
|
||||||
|
|
||||||
private $formFactory;
|
private FormFactoryInterface $formFactory;
|
||||||
|
private SerializerInterface $serializer;
|
||||||
|
private TokenStorageInterface $tokenStorage;
|
||||||
|
|
||||||
public function __construct(FormFactoryInterface $formFactory)
|
public function __construct(
|
||||||
{
|
FormFactoryInterface $formFactory,
|
||||||
|
SerializerInterface $serializer,
|
||||||
|
TokenStorageInterface $tokenStorage
|
||||||
|
) {
|
||||||
$this->formFactory = $formFactory;
|
$this->formFactory = $formFactory;
|
||||||
|
$this->serializer = $serializer;
|
||||||
|
$this->tokenStorage = $tokenStorage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFunctions()
|
public function getFunctions()
|
||||||
@@ -24,6 +33,7 @@ class AppExtension extends AbstractExtension
|
|||||||
return [
|
return [
|
||||||
new TwigFunction('file_exists', [$this, 'file_exists']),
|
new TwigFunction('file_exists', [$this, 'file_exists']),
|
||||||
new TwigFunction('render_search_form', [$this, 'render_search_form']),
|
new TwigFunction('render_search_form', [$this, 'render_search_form']),
|
||||||
|
new TwigFunction('get_user', [$this, 'get_user']),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,4 +60,9 @@ class AppExtension extends AbstractExtension
|
|||||||
{
|
{
|
||||||
return $this->formFactory->create(SearchType::class)->createView();
|
return $this->formFactory->create(SearchType::class)->createView();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function get_user()
|
||||||
|
{
|
||||||
|
return $this->serializer->serialize($this->tokenStorage->getToken()?->getUser(), 'jsonld');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,9 @@
|
|||||||
<title>{% block title %}Welcome!{% endblock %}</title>
|
<title>{% block title %}Welcome!{% endblock %}</title>
|
||||||
<link rel="icon"
|
<link rel="icon"
|
||||||
href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 128 128%22><text y=%221.2em%22 font-size=%2296%22>📔</text></svg>">
|
href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 128 128%22><text y=%221.2em%22 font-size=%2296%22>📔</text></svg>">
|
||||||
|
<script>
|
||||||
|
window.user = {{ get_user() | raw }};
|
||||||
|
</script>
|
||||||
{% block stylesheets %}
|
{% block stylesheets %}
|
||||||
{{ encore_entry_link_tags('app') }}
|
{{ encore_entry_link_tags('app') }}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
Reference in New Issue
Block a user