Skip to content

Commit

Permalink
Add displayName column to UserEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
emlun committed Sep 5, 2023
1 parent 56199d8 commit 861b30c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class UserEntity {
@Column({ unique: true, nullable: true, default: () => "NULL" })
username: string;

// Explicit default to workaround a bug in typeorm: https://github.com/typeorm/typeorm/issues/3076#issuecomment-703128687
@Column({ unique: true, nullable: true, default: () => "NULL" })
displayName: string;

@Column({ unique: true, nullable: false })
did: string;

Expand Down Expand Up @@ -104,13 +108,15 @@ class WebauthnCredentialEntity {

type CreateUser = {
username: string;
displayName: string,
did: string;
passwordHash: string;
keys: Buffer;
fcmToken: Buffer;
browserFcmToken: Buffer;
webauthnUserHandle: string;
} | {
displayName: string,
did: string;
keys: Buffer;
fcmToken: Buffer;
Expand Down
22 changes: 14 additions & 8 deletions src/routers/user.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ async function initNewUser(req: Request): Promise<{ fcmToken: Buffer, browserFcm
};
}

async function initSession(did: string): Promise<{ did: string, appToken: string }> {
async function initSession(did: string, displayName: string): Promise<{ did: string, appToken: string, displayName: string }> {
const secret = new TextEncoder().encode(config.appSecret);
const appToken = await new SignJWT({ did })
.setProtectedHeader({ alg: "HS256" })
.sign(secret);
return { did, appToken };
return {
did,
appToken,
displayName,
};
}

noAuthUserController.post('/register', async (req: Request, res: Response) => {
Expand All @@ -53,6 +57,7 @@ noAuthUserController.post('/register', async (req: Request, res: Response) => {

const newUser: CreateUser = {
username: username ? username : "",
displayName: req.body.displayName,
passwordHash: passwordHash,
keys: Buffer.from(keysStringified),
did,
Expand All @@ -68,7 +73,8 @@ noAuthUserController.post('/register', async (req: Request, res: Response) => {
return;
}

res.status(200).send(await initSession(did));
const user = result.unwrap();
res.status(200).send(await initSession(did, user.displayName || username));
});

noAuthUserController.post('/login', async (req: Request, res: Response) => {
Expand All @@ -84,7 +90,7 @@ noAuthUserController.post('/login', async (req: Request, res: Response) => {
}
console.log('user res = ', userRes)
const user = userRes.unwrap();
res.status(200).send(await initSession(user.did));
res.status(200).send(await initSession(user.did, user.displayName || username));
})

noAuthUserController.post('/register-webauthn-begin', async (req: Request, res: Response) => {
Expand Down Expand Up @@ -143,6 +149,7 @@ noAuthUserController.post('/register-webauthn-finish', async (req: Request, res:

const { fcmToken, browserFcmToken, keysStringified, did } = await initNewUser(req);
const newUser: CreateUser = {
displayName: req.body.displayName,
keys: Buffer.from(keysStringified),
did,
fcmToken,
Expand All @@ -166,7 +173,7 @@ noAuthUserController.post('/register-webauthn-finish', async (req: Request, res:
const userRes = await createUser(newUser, false, );
if (userRes.ok) {
console.log("Created user", userRes.val);
res.status(200).send(await initSession(did));
res.status(200).send(await initSession(did, userRes.val.displayName));
} else {
res.status(500).send({});
}
Expand Down Expand Up @@ -240,7 +247,7 @@ noAuthUserController.post('/login-webauthn-finish', async (req: Request, res: Re
if (updateCredentialRes.ok) {
res.status(200).send({
username: user.username,
...await initSession(user.did),
...await initSession(user.did, user.displayName),
});
} else {
res.status(500).send({});
Expand Down Expand Up @@ -304,8 +311,7 @@ userController.post('/webauthn/register-begin', async (req: Request, res: Respon
challenge: challenge.challenge,
user: {
...user,
name: user.username,
displayName: user.username,
name: user.displayName,
},
});

Expand Down

0 comments on commit 861b30c

Please sign in to comment.