Skip to content

Commit 1ef8c00

Browse files
authored
Merge branch 'main' into nephelite-patch-3
2 parents 62fd787 + d846c8c commit 1ef8c00

30 files changed

+608
-145
lines changed

.env.example

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,3 @@ PEERPREP_QUESTION_INITDB_NAME=peerprepQuestionServiceDB # must match question se
1616
PEERPREP_USER_INITDB_NAME=peerprepUserServiceDB # must match user service .env file and init-mongo.js
1717
PEERPREP_MATCHING_INITDB_NAME=peerprepMatchingServiceDB # must match user service .env file and init-mongo.js
1818
PEERPREP_COLLABORATION_INITDB_NAME=peerprepCollaborationServiceDB # must match collab service .env file and init-mongo.js
19-
20-
REACT_APP_LOCAL_API_URL=http://localhost #http://52.221.131.145 for production
21-
REACT_APP_SOCKET_URL=http://localhost:3003 #http://52.221.131.145:3003 for production

backend/user-service/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ENV=production
1313
# Secrets for creating JWT signatures
1414
JWT_ACCESS_TOKEN_SECRET=<your-jwt-access-token-secret>
1515
JWT_REFRESH_TOKEN_SECRET=<your-jwt-refresh-token-secret>
16+
JWT_RESET_TOKEN_SECRET=<your-jwt-reset-token-secret>
1617

1718
# If using mongoDB containerization, set to true. Else set to false (i.e local testing)
1819
DB_REQUIRE_AUTH=true

backend/user-service/controller/auth-controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ export async function handleLogin(req, res, next) {
1111
try {
1212
const user = await _findUserByEmail(email);
1313
if (!user) {
14-
throw new UnauthorisedError("Wrong email");
14+
throw new UnauthorisedError("Wrong email/password");
1515
}
1616

1717
const match = await bcrypt.compare(password, user.password);
1818
if (!match) {
19-
throw new UnauthorisedError("Wrong password");
19+
throw new UnauthorisedError("Wrong email/password");
2020
}
2121

2222
// Generate access and refresh token

backend/user-service/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,5 @@ app.use((req, res, next) => {
105105

106106
app.use(errorHandler);
107107

108-
app.listen(port, async () => {
109-
console.log(`Server is running at http://localhost:${port}`);
110-
});
111-
112108
export default app;
113109
export { port };

docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ services:
125125
- CHOKIDAR_USEPOLLING=true
126126
- WATCHPACK_POLLING=true
127127
- WDS_SOCKET_PORT=0
128-
- REACT_APP_LOCAL_API_URL=${REACT_APP_LOCAL_API_URL}
129-
- REACT_APP_SOCKET_URL=${REACT_APP_SOCKET_URL}
130128

131129
# Nginx API Gateway
132130
nginx:

frontend/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
REACT_APP_LOCAL_API_URL=http://localhost
22
REACT_APP_SOCKET_URL=http://localhost:3003
3+
REACT_APP_COLLAB_SOCKET_URL=localhost:3004
34

45
# Copy root .env
56
# If using mongoDB containerization, set to true. Else set to false (i.e local testing)

frontend/src/data/users/UserImpl.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ export class UserImpl implements IUser {
3434
return this.dataSource.getUser(userId);
3535
}
3636

37+
async getAllUsers(): Promise<any> {
38+
return this.dataSource.getAllUsers();
39+
}
40+
41+
async deleteUser(userId: string): Promise<any> {
42+
return this.dataSource.deleteUser(userId);
43+
}
44+
45+
async updateUserPrivilege(userId: string, isAdmin: boolean): Promise<any> {
46+
return this.dataSource.updateUserPrivilege(userId, isAdmin);
47+
}
3748
async forgetPassword(email: string): Promise<any> {
3849
return this.dataSource.forgetPassword(email);
3950
}

frontend/src/data/users/UserRemoteDataSource.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ export class UserRemoteDataSource extends BaseApi {
5151
return await this.protectedGet<any>(`/users/${userId}`);
5252
}
5353

54+
async getAllUsers() {
55+
return await this.protectedGet<any>("/users/");
56+
}
57+
58+
async deleteUser(userId: string) {
59+
return await this.protectedDelete<any>(`/users/${userId}`);
60+
}
61+
62+
async updateUserPrivilege(userId: string, isAdmin: boolean) {
63+
return await this.protectedPatch(`/users/${userId}/privilege`, { isAdmin });
64+
}
5465
async forgetPassword(email: string) {
5566
return await this.post<any>("/users/forgetPassword", { email });
5667
}

frontend/src/data/users/mockUser.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import AuthClientStore from "data/auth/AuthClientStore";
22
import { User } from "domain/entities/User";
33
import { IUserRegisterInput, IUserLoginInput, IUserUpdateInput } from "domain/users/IUser";
44

5-
const users: User[] = [
5+
let users: User[] = [
66
{
77
_id: "1",
88
username: "SampleUserName",
@@ -156,6 +156,31 @@ export class MockUser {
156156
});
157157
}
158158

159+
async getAllUsers(): Promise<any> {
160+
return new Promise((resolve, reject) => ({ message: "Fetched all users", data: resolve(users) }));
161+
}
162+
163+
async deleteUser(userId: string): Promise<any> {
164+
return new Promise((resolve, reject) => {
165+
users = users.filter((user) => user._id !== userId);
166+
});
167+
}
168+
169+
async updateUserPrivilege(userId: string, isAdmin: boolean): Promise<any> {
170+
return new Promise((resolve, reject) => {
171+
try {
172+
const foundUser = this.users.find((u) => u._id === userId);
173+
if (!foundUser) {
174+
resolve({ message: "User not found" });
175+
} else {
176+
foundUser.isAdmin = isAdmin;
177+
resolve({ message: "User privileges updated", data: foundUser });
178+
}
179+
} catch (error) {
180+
reject(error);
181+
}
182+
});
183+
}
159184
async forgetPassword(email: string): Promise<any> {}
160185

161186
async resetPassword(password: string, token: string): Promise<any> {}

frontend/src/domain/context/AuthContext.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ interface AuthContextType {
1111
login: (email: string, password: string) => Promise<void>;
1212
logout: () => Promise<void>;
1313
register: (email: string, password: string, username: string) => Promise<void>;
14-
updateUser: (userUpdateInput: IUserUpdateInput) => Promise<void>;
14+
updateUser: (userId: string, userUpdateInput: IUserUpdateInput) => Promise<User>;
15+
deactivateUser: (userId: string) => Promise<void>;
1516
}
1617

1718
const AuthContext = createContext<AuthContextType | undefined>(undefined);
@@ -70,11 +71,19 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
7071
handleSuccessfulAuth(accessToken, user);
7172
};
7273

73-
const updateUser = async (userUpdateInput: IUserUpdateInput) => {
74-
if (user) {
75-
const updatedUser = await userUseCases.updateUser(user?._id, userUpdateInput);
74+
const updateUser = async (userId: string, userUpdateInput: IUserUpdateInput) => {
75+
const updatedUser = await userUseCases.updateUser(userId, userUpdateInput);
76+
if (userId === user?._id) {
7677
setUser(updatedUser);
7778
}
79+
return updatedUser;
80+
};
81+
82+
const deactivateUser = async (userId: string) => {
83+
await userUseCases.deleteUser(userId);
84+
if (userId === user?._id) {
85+
logout();
86+
}
7887
};
7988

8089
const handleSuccessfulAuth = (accessToken: string, user: User) => {
@@ -87,7 +96,9 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
8796
const isUserAdmin = true;
8897

8998
return (
90-
<AuthContext.Provider value={{ user, isLoggedIn, isUserAdmin, login, logout, register, updateUser }}>
99+
<AuthContext.Provider
100+
value={{ user, isLoggedIn, isUserAdmin, login, logout, register, updateUser, deactivateUser }}
101+
>
91102
{children}
92103
</AuthContext.Provider>
93104
);

0 commit comments

Comments
 (0)