I creating authorization component for firebase. And found many post about how may create authorization service, most part of that post was about create some global const or the very interesting way - create vue plugin with auth methods.
But when i do it self i easy create Auth module in vuex and call action in creating app.vue life cycle for check user and pass that data in store.
Post when creating auth in vue plugin .
And my solution
import {VuexModule, Module, Mutation, Action} from 'vuex-module-decorators'
import * as firebase from 'firebase';
import { SingUpActionPayload, ResponseError, SingUpMutationPayload} from "./interfaces/singUp";
@Module
export default class Auth extends VuexModule {
singUpData: any;
singUpError: ResponseError = {
code: '',
message: ''
};
@Action({rawError: true})
async singUp (payload: SingUpActionPayload) {
try {
let result = firebase.auth().createUserWithEmailAndPassword(payload.email, payload.password);
await result;
this.context.commit('setCurrentUser', firebase.auth().currentUser);
this.context.commit('setSingUpResult', {type: 'result', result});
} catch (error) {
this.context.commit('setSingUpResult', {type: 'error', error});
}
}
@Mutation
setSingUpResult(payload: any){
if(payload.type === 'result'){
this.singUpData = payload.result;
} else {
this.singUpError = payload.error;
}
}
}
@Module
export default class UserModule extends VuexModule {
currentUser = null;
@Action({commit: 'setCurrentUser'})
takeCurrentUser(){
return firebase.auth().currentUser;
}
@Mutation
setCurrentUser(payload: any){
this.currentUser = payload;
}
}
for router i have similar logic like in post, just check user from state.
So now i have question, what way be more good, vue plugin with auth service or vuex module?