【发布时间】:2019-08-11 17:45:18
【问题描述】:
我为 firebase 创建授权组件。并且发现了很多关于如何创建授权服务的帖子,该帖子的大部分内容是关于创建一些全局 const 或非常有趣的方式 - 使用 auth 方法创建 vue 插件。
但是当我自己做时,我很容易在 vuex 中创建 Auth 模块,并在创建 app.vue 生命周期时调用操作以检查用户并将该数据传递到存储中。
Post 在 vue 插件中创建身份验证时。
还有我的解决方案
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;
}
}
对于路由器,我有类似帖子中的逻辑,只需从状态中检查用户。
所以现在我有一个问题,什么方式更好,带有身份验证服务或 vuex 模块的 vue 插件?
【问题讨论】: