Skip to content

Commit 5748f64

Browse files
committed
Default user as admin on register
1 parent a8d0d74 commit 5748f64

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

backend/user-service/.env.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,7 @@ [email protected]
4444
SMTP_PASSWORD=scdqcveqpurzzajj
4545

4646
# App URL
47-
APP_URL=http://localhost:3000
47+
APP_URL=http://localhost:3000
48+
49+
# Feature flags
50+
DEFAULT_ADMIN_ON_REGISTER_FEATURE=true

backend/user-service/model/repository.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export async function connectToDB() {
3636
}
3737

3838
export async function createUser(username, email, password) {
39-
return new UserModel({ username, email, password }).save();
39+
const isAdmin = process.env.DEFAULT_ADMIN_ON_REGISTER_FEATURE === "true";
40+
return new UserModel({ username, email, password, isAdmin }).save();
4041
}
4142

4243
export async function findUserByEmail(email) {

frontend/src/domain/context/AuthContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
9393
};
9494

9595
//const isUserAdmin = isLoggedIn === true && user != null && user.isAdmin;
96-
const isUserAdmin = true;
96+
const isUserAdmin = !!user?.isAdmin;
9797

9898
return (
9999
<AuthContext.Provider

frontend/src/presentation/pages/admin/users/UserManagement.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { DeleteUserForm } from "presentation/components/users/DeleteUserForm/Del
99
import { UpdateUserPrivilegeForm } from "presentation/components/users/UpdateUserPrivilegeForm/UpdateUserPrivilegeForm";
1010
import { useAuth } from "domain/context/AuthContext";
1111
import { SearchBar } from "presentation/components/common/SearchBar";
12+
import { toast } from "react-toastify";
13+
import { handleError } from "presentation/utils/errorHandler";
1214

1315
export const UserManagement: React.FC<{}> = () => {
1416
const [users, setUsers] = useState<User[]>([]);
@@ -21,8 +23,12 @@ export const UserManagement: React.FC<{}> = () => {
2123

2224
useEffect(() => {
2325
const getAllUsers = async () => {
24-
const users = await userUseCases.getAllUsers();
25-
setUsers(users);
26+
try {
27+
const users = await userUseCases.getAllUsers();
28+
setUsers(users);
29+
} catch (err) {
30+
toast.error(handleError(err));
31+
}
2632
};
2733
getAllUsers();
2834
}, []);

0 commit comments

Comments
 (0)