Skip to content

Commit

Permalink
feat: Adjust user role assignment based on environment (#268)
Browse files Browse the repository at this point in the history
Co-authored-by: Eva Decker <[email protected]>
  • Loading branch information
belhajManel and evadecker authored Dec 9, 2024
1 parent 1d4ed14 commit 872064c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-roses-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"namesake": patch
---

Default the user role to admin during signup when the environment is set to development.
50 changes: 50 additions & 0 deletions convex/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { convexTest } from "convex-test";
import { describe, expect, it, vi } from "vitest";
import { api } from "./_generated/api";
import { createOrUpdateUser } from "./auth";
import schema from "./schema";
import { modules } from "./test.setup";

describe("auth", () => {
describe("createOrUpdateUser", () => {
it("should set role to admin in development environment", async () => {
const t = convexTest(schema, modules);

vi.stubEnv("NODE_ENV", "development");
await t.run(async (ctx) => {
return await createOrUpdateUser(ctx, {
profile: {
email: "[email protected]",
},
});
});

const user = await t.query(api.users.getByEmail, {
email: "[email protected]",
});

expect(user?.role).toBe("admin");
vi.unstubAllEnvs();
});

it("should set role to user in production environment", async () => {
const t = convexTest(schema, modules);

vi.stubEnv("NODE_ENV", "production");
await t.run(async (ctx) => {
return await createOrUpdateUser(ctx, {
profile: {
email: "[email protected]",
},
});
});

const user = await t.query(api.users.getByEmail, {
email: "[email protected]",
});

expect(user?.role).toBe("user");
vi.unstubAllEnvs();
});
});
});
63 changes: 32 additions & 31 deletions convex/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,42 @@ import type { MutationCtx } from "./_generated/server";
import { ResendOTPPasswordReset } from "./passwordReset";
import { getByEmail } from "./users";

export const { auth, signIn, signOut, store } = convexAuth({
providers: [Password({ reset: ResendOTPPasswordReset })],
export const createOrUpdateUser = async (ctx: MutationCtx, args: any) => {
// Handle merging updated fields into existing user
if (args.existingUserId) {
return args.existingUserId;
}

callbacks: {
async createOrUpdateUser(ctx: MutationCtx, args) {
// Handle merging updated fields into existing user
if (args.existingUserId) {
return args.existingUserId;
}
// Handle account linking
if (args.profile.email) {
const existingUser = await getByEmail(ctx, {
email: args.profile.email,
});
if (existingUser) return existingUser._id;
}

// Handle account linking
if (args.profile.email) {
const existingUser = await getByEmail(ctx, {
email: args.profile.email,
});
if (existingUser) return existingUser._id;
}
// Create a new user with defaults
return ctx.db
.insert("users", {
email: args.profile.email,
emailVerified: args.profile.emailVerified ?? false,
role: process.env.NODE_ENV === "development" ? "admin" : "user",
})
.then((userId) => {
ctx.db.insert("userSettings", {
userId,
theme: "system",
groupQuestsBy: "dateAdded",
});
return userId;
});
};

// Create a new user with defaults
return ctx.db
.insert("users", {
email: args.profile.email,
emailVerified: args.profile.emailVerified ?? false,
role: "user",
})
.then((userId) => {
ctx.db.insert("userSettings", {
userId,
theme: "system",
groupQuestsBy: "dateAdded",
});
return userId;
});
},
export const { auth, signIn, signOut, store } = convexAuth({
providers: [Password({ reset: ResendOTPPasswordReset })],

callbacks: {
createOrUpdateUser,
async redirect({ redirectTo }) {
return redirectTo;
},
Expand Down

0 comments on commit 872064c

Please sign in to comment.