Skip to content

Commit 6a5a585

Browse files
committed
profile rename
1 parent 3fbdcc0 commit 6a5a585

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

TypescriptServer/src/cmd/handlers/auth.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
import { addHandler } from "#cmd/handlePacket";
22
import { Account, getAccountInfo, IAccount } from "#schemas/account";
33
import Session from "#schemas/session";
4-
import { accountActivate, accountCreate, accountLogin, accountRegister, sessionCreate, sessionGet, sessionLogin } from "#util/auth";
4+
import { accountActivate, accountCreate, accountLogin, accountRegister, profileRename, sessionCreate, sessionGet, sessionLogin } from "#util/auth";
55
import trace from "#util/logging";
66
import { Names } from "#util/names";
77

88
addHandler('name get', (c) => {
99
c.sendName();
1010
});
1111

12+
addHandler('name set', async (c, data) => {
13+
let name = data.name;
14+
15+
if (c.logged_in) {
16+
try {
17+
await profileRename(c.profile, name);
18+
c.sendName();
19+
}
20+
catch(e) {}
21+
}
22+
else {
23+
if (!Names.isValid(name)) {
24+
return;
25+
}
26+
if (clients.some(client => client.name === name)) {
27+
return;
28+
}
29+
30+
c.name = name;
31+
c.sendName();
32+
}
33+
});
34+
1235
// create a brand new (temporary) account
1336

1437
addHandler('session create', async (c, data) => {

TypescriptServer/src/util/auth.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ export async function getProfileByName(name: string):Promise<IProfile> {
7676
return await Profile.findOne({ name });
7777
}
7878

79+
export async function profileRename(profile: IProfile, new_name: string):Promise<IProfile> {
80+
if (!Names.isValid(new_name)) {
81+
throw 'invalid name';
82+
}
83+
if (await Profile.exists({ name: new_name })) {
84+
throw 'name already taken';
85+
}
86+
87+
profile.name = new_name;
88+
return await profile.save();
89+
}
7990

8091

8192
// logging in/registering stuff

0 commit comments

Comments
 (0)