Username can now be changed

This commit is contained in:
Thaum Rystra
2020-05-09 13:15:03 +02:00
parent c3ed3d55ce
commit 8c608937bb
4 changed files with 117 additions and 1 deletions

View File

@@ -127,6 +127,30 @@ Meteor.users.isAdmin = function(userId){
return user && user.roles.includes('admin');
}
Meteor.users.canPickUsername = new ValidatedMethod({
name: 'Users.methods.canPickUsername',
validate: userSchema.pick('username').validator(),
run({username}){
if (Meteor.isClient) return;
let user = Accounts.findUserByUsername(username);
// You can pick your own username
if (user && user._id === this.userId){
return false;
}
return !!user;
}
});
Meteor.users.setUsername = new ValidatedMethod({
name: 'Users.methods.setUsername',
validate: userSchema.pick('username').validator(),
run({username}){
if (!this.userId) throw 'Can only set your username if logged in';
if (Meteor.isClient) return;
return Accounts.setUsername(this.userId, username)
}
});
Meteor.users.findUserByUsernameOrEmail = new ValidatedMethod({
name: 'Users.methods.findUserByUsernameOrEmail',
validate: new SimpleSchema({

View File

@@ -8,6 +8,7 @@ import LibraryEditDialog from '/imports/ui/library/LibraryEditDialog.vue';
import LibraryNodeCreationDialog from '/imports/ui/library/LibraryNodeCreationDialog.vue';
import LibraryNodeEditDialog from '/imports/ui/library/LibraryNodeEditDialog.vue';
import ShareDialog from '/imports/ui/sharing/ShareDialog.vue';
import UsernameDialog from '/imports/ui/user/UsernameDialog.vue';
export default {
CreatureFormDialog,
@@ -20,4 +21,5 @@ export default {
LibraryNodeCreationDialog,
LibraryNodeEditDialog,
ShareDialog,
UsernameDialog,
};

View File

@@ -16,7 +16,7 @@
<v-subheader>
Username
</v-subheader>
<v-list-tile>
<v-list-tile data-id="username">
<v-list-tile-action>
<v-tooltip right>
<span>Change Username</span>
@@ -24,6 +24,7 @@
slot="activator"
icon
flat
@click="changeUsername"
>
<v-icon>create</v-icon>
</v-btn>
@@ -53,11 +54,13 @@
<v-list-tile-title>
Pledged amount: ${{ entitledCents/100 }}
</v-list-tile-title>
<!--
<v-list-tile-action>
<v-btn icon>
<v-icon>refresh</v-icon>
</v-btn>
</v-list-tile-action>
-->
</v-list-tile>
<v-list-tile>
<v-list-tile-title>
@@ -127,6 +130,12 @@
},
},
methods: {
changeUsername(){
this.$store.commit('pushDialogStack', {
component: 'username-dialog',
elementId: 'username',
});
},
signOut(){
Meteor.logout();
router.push('/');

View File

@@ -0,0 +1,81 @@
<template lang="html">
<dialog-base>
<text-field
:value="newUsername || username"
@change="change"
/>
<div
v-if="error"
class="error"
>
{{ error }}
</div>
<v-spacer slot="actions" />
<v-btn
slot="actions"
flat
:disabled="!valid"
:loading="loading"
@click="setUsername"
>
Update
</v-btn>
</dialog-base>
</template>
<script>
import DialogBase from '/imports/ui/dialogStack/DialogBase.vue';
export default {
components: {
DialogBase,
},
data(){return {
valid: true,
newUsername: null,
updatingUsername: false,
error: null,
loading: false,
};},
meteor:{
username(){
let user = Meteor.user();
return user && user.username;
},
},
methods: {
change(username, ack){
this.loading = true;
Meteor.users.canPickUsername.call({username}, (error, result) => {
this.loading = false;
console.log({error, result})
if (error){
this.valid = false;
ack(error.message || error);
} else if (result){
this.valid = false;
ack('Username is already taken');
} else {
this.valid = true;
this.newUsername = username;
ack();
}
});
},
setUsername(){
this.loading = true;
Meteor.users.setUsername.call({username: this.newUsername}, error => {
this.loading = false;
if (error){
this.error = error.message || error;
} else {
this.$store.dispatch('popDialogStack')
}
});
}
}
}
</script>
<style lang="css" scoped>
</style>