How to update a user state from a login component in vue js?
I´m trying to update the user vuex state from a Login component have here. I have very few experience with vuex so I´m little lost in the process... this is how I´m trying:
By the way, I´m trying to use (https://github.com/devjin0617/vue2-admin-lte) , however I just added a Login component to the project
user.js
`const mutations = {
set: (state, user) =>{
state.main = Object.assign({}, state.main, user)
}
actions.js
export const updateCurrentUser = ( {commit}, form ) => {
commit('setUser',form)
}
mutation-types
export const UPDATE_USER = 'UPDATE_USER'
Finally in the Login.vue component I have the login() method in which I´m trying to call the action
Login.vue
methods:{
...mapActions([
'updateCurrentUser'
]),
logIn() {
this.$store.commit() // I dont know which parameters to call here
this.currentUser.mutations.set(this.form) // I´ve tried this, but doesn´t work
}
`
I´m want to update the user but at the moment I´m being able to access the setter method
I don´t know if I´m following the correct method resolution order to make this work...
I still lost in the order to call the procedures.
vue.js vuex vuex-modules
add a comment |
I´m trying to update the user vuex state from a Login component have here. I have very few experience with vuex so I´m little lost in the process... this is how I´m trying:
By the way, I´m trying to use (https://github.com/devjin0617/vue2-admin-lte) , however I just added a Login component to the project
user.js
`const mutations = {
set: (state, user) =>{
state.main = Object.assign({}, state.main, user)
}
actions.js
export const updateCurrentUser = ( {commit}, form ) => {
commit('setUser',form)
}
mutation-types
export const UPDATE_USER = 'UPDATE_USER'
Finally in the Login.vue component I have the login() method in which I´m trying to call the action
Login.vue
methods:{
...mapActions([
'updateCurrentUser'
]),
logIn() {
this.$store.commit() // I dont know which parameters to call here
this.currentUser.mutations.set(this.form) // I´ve tried this, but doesn´t work
}
`
I´m want to update the user but at the moment I´m being able to access the setter method
I don´t know if I´m following the correct method resolution order to make this work...
I still lost in the order to call the procedures.
vue.js vuex vuex-modules
add a comment |
I´m trying to update the user vuex state from a Login component have here. I have very few experience with vuex so I´m little lost in the process... this is how I´m trying:
By the way, I´m trying to use (https://github.com/devjin0617/vue2-admin-lte) , however I just added a Login component to the project
user.js
`const mutations = {
set: (state, user) =>{
state.main = Object.assign({}, state.main, user)
}
actions.js
export const updateCurrentUser = ( {commit}, form ) => {
commit('setUser',form)
}
mutation-types
export const UPDATE_USER = 'UPDATE_USER'
Finally in the Login.vue component I have the login() method in which I´m trying to call the action
Login.vue
methods:{
...mapActions([
'updateCurrentUser'
]),
logIn() {
this.$store.commit() // I dont know which parameters to call here
this.currentUser.mutations.set(this.form) // I´ve tried this, but doesn´t work
}
`
I´m want to update the user but at the moment I´m being able to access the setter method
I don´t know if I´m following the correct method resolution order to make this work...
I still lost in the order to call the procedures.
vue.js vuex vuex-modules
I´m trying to update the user vuex state from a Login component have here. I have very few experience with vuex so I´m little lost in the process... this is how I´m trying:
By the way, I´m trying to use (https://github.com/devjin0617/vue2-admin-lte) , however I just added a Login component to the project
user.js
`const mutations = {
set: (state, user) =>{
state.main = Object.assign({}, state.main, user)
}
actions.js
export const updateCurrentUser = ( {commit}, form ) => {
commit('setUser',form)
}
mutation-types
export const UPDATE_USER = 'UPDATE_USER'
Finally in the Login.vue component I have the login() method in which I´m trying to call the action
Login.vue
methods:{
...mapActions([
'updateCurrentUser'
]),
logIn() {
this.$store.commit() // I dont know which parameters to call here
this.currentUser.mutations.set(this.form) // I´ve tried this, but doesn´t work
}
`
I´m want to update the user but at the moment I´m being able to access the setter method
I don´t know if I´m following the correct method resolution order to make this work...
I still lost in the order to call the procedures.
vue.js vuex vuex-modules
vue.js vuex vuex-modules
edited Dec 28 '18 at 3:11
asked Dec 28 '18 at 0:49
Adriel Werlich
4119
4119
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
From the look of your given codes, your codes and files is a bit messy, I see you are trying to use vuex with modules, but the way you are doing it seems wrong, can't really tell because I can't see your folders and files structure.
Let start with a simple method of achieving this.
1. create a store folder inside your src folder
2. create a mutation type const js src/store/mutation-types.js
Inside your
mutation-types.jsput this code
export const UPDATE_USER = 'UPDATE_USER'
3. Inside your store folder create an index.js file
4. Inside your src/store/index.js you put the following code
index.js
// import your mutation type name constants
import * as types from './mutation-types'
export default new Vuex.Store({
state: {
user: {} // default empty object
},
getters: {
//...other getters...
},
mutations: {
// mutating your user state
[types.UPDATE_USER](state, user) {
state.user = user
}
},
actions: {
},
})
5. Make sure you import vuex's store inside your main.js
Inside your
main.js
import store from './store'
// other codes
// export Vue
export default new Vue({
el: '#app',
router,
store, // add your store
components: {App},
template: '<App/>'
})
6. committing inside your component
Login.vue
methods:{
logIn() {
// let say you have gotten your user data
// then you can commit it into vuex storage by using the mutation constant name you defined
let userData = {user_id: 1}
this.$store.commit('UPDATE_USER', userData)
}
}
That's it, keep it simple when you first started, don't try anything fancy like modules.
Hey @TriDiamond, thanks a lot for your help... I´m being able to update the user now... but I´m calling an action before the mutation to retrieve the profile details (from firebase) of the logged user before the mutation...
– Adriel Werlich
Dec 30 '18 at 14:35
Going through an action before mutation or commit straight to a mutation is completely depend on your logic. Both is ok.
– TriDiamond
2 days ago
add a comment |
The final solution is like this:
1- vuex/modules/user.js
import firebase from 'firebase'
const state = {
user:null
}
const getters = {
user: state => {
return state.user
}
}
// mutations
const mutations = {
'SET_USER' (state, user) {
state.user = user
}
}
// actions
const actions = {
setUser: ( {commit}, user ) => {
if (user){
let fireUser = null
firebase.firestore().collection("users").doc(user.uid).get()
.then( doc => {
fireUser = doc.data()
commit('SET_USER', fireUser)
}).catch( err => {
console.log(err)
})
}
console.log(state.user)
}
}
export default {
state,mutations,actions,getters
}
2- vuex/store.js
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
Vue.use(Vuex)
// debugger
export const store = new Vuex.Store({
modules:{
user
}
})
3- App.vue
// So here is where the event is fired when the user get´s logged in the firebase
created(){
let _this = this
window.firebase.auth().onAuthStateChanged( user => {
// set user credentials
if (user){
if (_this.$store)
_this.$store.dispatch('setUser', user)
} else {
if(_this.$store)
_this.$store.dispatch('setUser', null)
}
})
}
I don´t know if it is the best implementation, but It´s working for the moment
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53952493%2fhow-to-update-a-user-state-from-a-login-component-in-vue-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
From the look of your given codes, your codes and files is a bit messy, I see you are trying to use vuex with modules, but the way you are doing it seems wrong, can't really tell because I can't see your folders and files structure.
Let start with a simple method of achieving this.
1. create a store folder inside your src folder
2. create a mutation type const js src/store/mutation-types.js
Inside your
mutation-types.jsput this code
export const UPDATE_USER = 'UPDATE_USER'
3. Inside your store folder create an index.js file
4. Inside your src/store/index.js you put the following code
index.js
// import your mutation type name constants
import * as types from './mutation-types'
export default new Vuex.Store({
state: {
user: {} // default empty object
},
getters: {
//...other getters...
},
mutations: {
// mutating your user state
[types.UPDATE_USER](state, user) {
state.user = user
}
},
actions: {
},
})
5. Make sure you import vuex's store inside your main.js
Inside your
main.js
import store from './store'
// other codes
// export Vue
export default new Vue({
el: '#app',
router,
store, // add your store
components: {App},
template: '<App/>'
})
6. committing inside your component
Login.vue
methods:{
logIn() {
// let say you have gotten your user data
// then you can commit it into vuex storage by using the mutation constant name you defined
let userData = {user_id: 1}
this.$store.commit('UPDATE_USER', userData)
}
}
That's it, keep it simple when you first started, don't try anything fancy like modules.
Hey @TriDiamond, thanks a lot for your help... I´m being able to update the user now... but I´m calling an action before the mutation to retrieve the profile details (from firebase) of the logged user before the mutation...
– Adriel Werlich
Dec 30 '18 at 14:35
Going through an action before mutation or commit straight to a mutation is completely depend on your logic. Both is ok.
– TriDiamond
2 days ago
add a comment |
From the look of your given codes, your codes and files is a bit messy, I see you are trying to use vuex with modules, but the way you are doing it seems wrong, can't really tell because I can't see your folders and files structure.
Let start with a simple method of achieving this.
1. create a store folder inside your src folder
2. create a mutation type const js src/store/mutation-types.js
Inside your
mutation-types.jsput this code
export const UPDATE_USER = 'UPDATE_USER'
3. Inside your store folder create an index.js file
4. Inside your src/store/index.js you put the following code
index.js
// import your mutation type name constants
import * as types from './mutation-types'
export default new Vuex.Store({
state: {
user: {} // default empty object
},
getters: {
//...other getters...
},
mutations: {
// mutating your user state
[types.UPDATE_USER](state, user) {
state.user = user
}
},
actions: {
},
})
5. Make sure you import vuex's store inside your main.js
Inside your
main.js
import store from './store'
// other codes
// export Vue
export default new Vue({
el: '#app',
router,
store, // add your store
components: {App},
template: '<App/>'
})
6. committing inside your component
Login.vue
methods:{
logIn() {
// let say you have gotten your user data
// then you can commit it into vuex storage by using the mutation constant name you defined
let userData = {user_id: 1}
this.$store.commit('UPDATE_USER', userData)
}
}
That's it, keep it simple when you first started, don't try anything fancy like modules.
Hey @TriDiamond, thanks a lot for your help... I´m being able to update the user now... but I´m calling an action before the mutation to retrieve the profile details (from firebase) of the logged user before the mutation...
– Adriel Werlich
Dec 30 '18 at 14:35
Going through an action before mutation or commit straight to a mutation is completely depend on your logic. Both is ok.
– TriDiamond
2 days ago
add a comment |
From the look of your given codes, your codes and files is a bit messy, I see you are trying to use vuex with modules, but the way you are doing it seems wrong, can't really tell because I can't see your folders and files structure.
Let start with a simple method of achieving this.
1. create a store folder inside your src folder
2. create a mutation type const js src/store/mutation-types.js
Inside your
mutation-types.jsput this code
export const UPDATE_USER = 'UPDATE_USER'
3. Inside your store folder create an index.js file
4. Inside your src/store/index.js you put the following code
index.js
// import your mutation type name constants
import * as types from './mutation-types'
export default new Vuex.Store({
state: {
user: {} // default empty object
},
getters: {
//...other getters...
},
mutations: {
// mutating your user state
[types.UPDATE_USER](state, user) {
state.user = user
}
},
actions: {
},
})
5. Make sure you import vuex's store inside your main.js
Inside your
main.js
import store from './store'
// other codes
// export Vue
export default new Vue({
el: '#app',
router,
store, // add your store
components: {App},
template: '<App/>'
})
6. committing inside your component
Login.vue
methods:{
logIn() {
// let say you have gotten your user data
// then you can commit it into vuex storage by using the mutation constant name you defined
let userData = {user_id: 1}
this.$store.commit('UPDATE_USER', userData)
}
}
That's it, keep it simple when you first started, don't try anything fancy like modules.
From the look of your given codes, your codes and files is a bit messy, I see you are trying to use vuex with modules, but the way you are doing it seems wrong, can't really tell because I can't see your folders and files structure.
Let start with a simple method of achieving this.
1. create a store folder inside your src folder
2. create a mutation type const js src/store/mutation-types.js
Inside your
mutation-types.jsput this code
export const UPDATE_USER = 'UPDATE_USER'
3. Inside your store folder create an index.js file
4. Inside your src/store/index.js you put the following code
index.js
// import your mutation type name constants
import * as types from './mutation-types'
export default new Vuex.Store({
state: {
user: {} // default empty object
},
getters: {
//...other getters...
},
mutations: {
// mutating your user state
[types.UPDATE_USER](state, user) {
state.user = user
}
},
actions: {
},
})
5. Make sure you import vuex's store inside your main.js
Inside your
main.js
import store from './store'
// other codes
// export Vue
export default new Vue({
el: '#app',
router,
store, // add your store
components: {App},
template: '<App/>'
})
6. committing inside your component
Login.vue
methods:{
logIn() {
// let say you have gotten your user data
// then you can commit it into vuex storage by using the mutation constant name you defined
let userData = {user_id: 1}
this.$store.commit('UPDATE_USER', userData)
}
}
That's it, keep it simple when you first started, don't try anything fancy like modules.
answered Dec 29 '18 at 6:22
TriDiamond
537114
537114
Hey @TriDiamond, thanks a lot for your help... I´m being able to update the user now... but I´m calling an action before the mutation to retrieve the profile details (from firebase) of the logged user before the mutation...
– Adriel Werlich
Dec 30 '18 at 14:35
Going through an action before mutation or commit straight to a mutation is completely depend on your logic. Both is ok.
– TriDiamond
2 days ago
add a comment |
Hey @TriDiamond, thanks a lot for your help... I´m being able to update the user now... but I´m calling an action before the mutation to retrieve the profile details (from firebase) of the logged user before the mutation...
– Adriel Werlich
Dec 30 '18 at 14:35
Going through an action before mutation or commit straight to a mutation is completely depend on your logic. Both is ok.
– TriDiamond
2 days ago
Hey @TriDiamond, thanks a lot for your help... I´m being able to update the user now... but I´m calling an action before the mutation to retrieve the profile details (from firebase) of the logged user before the mutation...
– Adriel Werlich
Dec 30 '18 at 14:35
Hey @TriDiamond, thanks a lot for your help... I´m being able to update the user now... but I´m calling an action before the mutation to retrieve the profile details (from firebase) of the logged user before the mutation...
– Adriel Werlich
Dec 30 '18 at 14:35
Going through an action before mutation or commit straight to a mutation is completely depend on your logic. Both is ok.
– TriDiamond
2 days ago
Going through an action before mutation or commit straight to a mutation is completely depend on your logic. Both is ok.
– TriDiamond
2 days ago
add a comment |
The final solution is like this:
1- vuex/modules/user.js
import firebase from 'firebase'
const state = {
user:null
}
const getters = {
user: state => {
return state.user
}
}
// mutations
const mutations = {
'SET_USER' (state, user) {
state.user = user
}
}
// actions
const actions = {
setUser: ( {commit}, user ) => {
if (user){
let fireUser = null
firebase.firestore().collection("users").doc(user.uid).get()
.then( doc => {
fireUser = doc.data()
commit('SET_USER', fireUser)
}).catch( err => {
console.log(err)
})
}
console.log(state.user)
}
}
export default {
state,mutations,actions,getters
}
2- vuex/store.js
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
Vue.use(Vuex)
// debugger
export const store = new Vuex.Store({
modules:{
user
}
})
3- App.vue
// So here is where the event is fired when the user get´s logged in the firebase
created(){
let _this = this
window.firebase.auth().onAuthStateChanged( user => {
// set user credentials
if (user){
if (_this.$store)
_this.$store.dispatch('setUser', user)
} else {
if(_this.$store)
_this.$store.dispatch('setUser', null)
}
})
}
I don´t know if it is the best implementation, but It´s working for the moment
add a comment |
The final solution is like this:
1- vuex/modules/user.js
import firebase from 'firebase'
const state = {
user:null
}
const getters = {
user: state => {
return state.user
}
}
// mutations
const mutations = {
'SET_USER' (state, user) {
state.user = user
}
}
// actions
const actions = {
setUser: ( {commit}, user ) => {
if (user){
let fireUser = null
firebase.firestore().collection("users").doc(user.uid).get()
.then( doc => {
fireUser = doc.data()
commit('SET_USER', fireUser)
}).catch( err => {
console.log(err)
})
}
console.log(state.user)
}
}
export default {
state,mutations,actions,getters
}
2- vuex/store.js
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
Vue.use(Vuex)
// debugger
export const store = new Vuex.Store({
modules:{
user
}
})
3- App.vue
// So here is where the event is fired when the user get´s logged in the firebase
created(){
let _this = this
window.firebase.auth().onAuthStateChanged( user => {
// set user credentials
if (user){
if (_this.$store)
_this.$store.dispatch('setUser', user)
} else {
if(_this.$store)
_this.$store.dispatch('setUser', null)
}
})
}
I don´t know if it is the best implementation, but It´s working for the moment
add a comment |
The final solution is like this:
1- vuex/modules/user.js
import firebase from 'firebase'
const state = {
user:null
}
const getters = {
user: state => {
return state.user
}
}
// mutations
const mutations = {
'SET_USER' (state, user) {
state.user = user
}
}
// actions
const actions = {
setUser: ( {commit}, user ) => {
if (user){
let fireUser = null
firebase.firestore().collection("users").doc(user.uid).get()
.then( doc => {
fireUser = doc.data()
commit('SET_USER', fireUser)
}).catch( err => {
console.log(err)
})
}
console.log(state.user)
}
}
export default {
state,mutations,actions,getters
}
2- vuex/store.js
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
Vue.use(Vuex)
// debugger
export const store = new Vuex.Store({
modules:{
user
}
})
3- App.vue
// So here is where the event is fired when the user get´s logged in the firebase
created(){
let _this = this
window.firebase.auth().onAuthStateChanged( user => {
// set user credentials
if (user){
if (_this.$store)
_this.$store.dispatch('setUser', user)
} else {
if(_this.$store)
_this.$store.dispatch('setUser', null)
}
})
}
I don´t know if it is the best implementation, but It´s working for the moment
The final solution is like this:
1- vuex/modules/user.js
import firebase from 'firebase'
const state = {
user:null
}
const getters = {
user: state => {
return state.user
}
}
// mutations
const mutations = {
'SET_USER' (state, user) {
state.user = user
}
}
// actions
const actions = {
setUser: ( {commit}, user ) => {
if (user){
let fireUser = null
firebase.firestore().collection("users").doc(user.uid).get()
.then( doc => {
fireUser = doc.data()
commit('SET_USER', fireUser)
}).catch( err => {
console.log(err)
})
}
console.log(state.user)
}
}
export default {
state,mutations,actions,getters
}
2- vuex/store.js
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
Vue.use(Vuex)
// debugger
export const store = new Vuex.Store({
modules:{
user
}
})
3- App.vue
// So here is where the event is fired when the user get´s logged in the firebase
created(){
let _this = this
window.firebase.auth().onAuthStateChanged( user => {
// set user credentials
if (user){
if (_this.$store)
_this.$store.dispatch('setUser', user)
} else {
if(_this.$store)
_this.$store.dispatch('setUser', null)
}
})
}
I don´t know if it is the best implementation, but It´s working for the moment
answered Dec 30 '18 at 15:02
Adriel Werlich
4119
4119
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53952493%2fhow-to-update-a-user-state-from-a-login-component-in-vue-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown