Skip to content

Commit

Permalink
Guarantee all users get a name (#800)
Browse files Browse the repository at this point in the history
* Guarantee all users get a name and it is used when talking to the backend

* Small code cleanup
  • Loading branch information
fozziethebeat authored Jan 17, 2023
1 parent 6b82b2c commit 0a36e45
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
11 changes: 11 additions & 0 deletions website/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"sharp": "^0.31.3",
"swr": "^2.0.0",
"tailwindcss": "^3.2.4",
"unique-username-generator": "^1.1.3",
"use-debounce": "^9.0.2"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions website/src/lib/oasst_api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class OasstApiClient {
type: taskType,
user: {
id: userToken.sub,
display_name: userToken.name || userToken.email,
display_name: userToken.name,
auth_method: "local",
},
});
Expand Down Expand Up @@ -146,7 +146,7 @@ export class OasstApiClient {
type: updateType,
user: {
id: userToken.sub,
display_name: userToken.name || userToken.email,
display_name: userToken.name,
auth_method: "local",
},
task_id: taskId,
Expand Down
19 changes: 16 additions & 3 deletions website/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import CredentialsProvider from "next-auth/providers/credentials";
import DiscordProvider from "next-auth/providers/discord";
import EmailProvider from "next-auth/providers/email";
import prisma from "src/lib/prismadb";
import { generateUsername } from "unique-username-generator";

const providers: Provider[] = [];

Expand Down Expand Up @@ -97,10 +98,11 @@ export const authOptions: AuthOptions = {
* This let's use forward the role to the session object.
*/
async jwt({ token }) {
const { isNew, role } = await prisma.user.findUnique({
const { isNew, name, role } = await prisma.user.findUnique({
where: { id: token.sub },
select: { role: true, isNew: true },
select: { name: true, role: true, isNew: true },
});
token.name = name;
token.role = role;
token.isNew = isNew;
return token;
Expand All @@ -110,7 +112,18 @@ export const authOptions: AuthOptions = {
/**
* Update the user's role after they have successfully signed in
*/
async signIn({ user, account }) {
async signIn({ user, account, isNewUser }) {
if (isNewUser && account.provider === "email") {
await prisma.user.update({
data: {
name: generateUsername(),
},
where: {
id: user.id,
},
});
}

// Get the admin list for the user's auth type.
const adminForAccountType = adminUserMap.get(account.provider);

Expand Down

0 comments on commit 0a36e45

Please sign in to comment.