Skip to content

Commit 246e371

Browse files
committed
Account creation hook added to frontend 'register account' action
1 parent 4b1e9c1 commit 246e371

4 files changed

Lines changed: 93 additions & 1 deletion

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.acme.controller;
2+
3+
import io.quarkus.logging.Log;
4+
import io.quarkus.security.identity.SecurityIdentity;
5+
import jakarta.inject.Inject;
6+
import jakarta.validation.Validator;
7+
import jakarta.ws.rs.*;
8+
import jakarta.ws.rs.core.*;
9+
10+
import org.acme.api.error.ApiError;
11+
import org.acme.auth.AuthUtils;
12+
import org.acme.model.dto.Auth.AccountHookResponse;
13+
14+
@Path("/api")
15+
public class AccountResource {
16+
17+
@Inject
18+
Validator validator;
19+
20+
@GET
21+
@Path("/account-hooks")
22+
public Response getScreeners(@Context SecurityIdentity identity,
23+
@QueryParam("action") String action) {
24+
String userId = AuthUtils.getUserId(identity);
25+
if (userId == null) {
26+
return Response.status(Response.Status.UNAUTHORIZED)
27+
.entity(new ApiError(true, "Unauthorized.")).build();
28+
}
29+
30+
if (action == null || action.isBlank()) {
31+
return Response.status(Response.Status.BAD_REQUEST).entity(
32+
new ApiError(true, "Query parameter 'action' is required."))
33+
.build();
34+
}
35+
Log.info("Running account hooks for: " + userId);
36+
37+
if (action.equals("add example screener")) {
38+
Log.info("***** Adding an exaample screener to the account *****");
39+
}
40+
41+
AccountHookResponse responseBody = new AccountHookResponse(
42+
"add example screener", true);
43+
44+
return Response.ok(responseBody).build();
45+
}
46+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.acme.model.dto.Auth;
2+
3+
public record AccountHookResponse(String action, boolean success) {
4+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { env } from "@/config/environment";
2+
3+
import { authGet } from "@/api/auth";
4+
5+
const apiUrl = env.apiUrl;
6+
7+
export const getAccountHooks = async () => {
8+
const searchParams = new URLSearchParams([
9+
["action", "add example screener"],
10+
]);
11+
const accountHookUrl = new URL(
12+
`${apiUrl}/account-hooks?${searchParams.toString()}`,
13+
);
14+
15+
try {
16+
const response = await authGet(accountHookUrl.toString());
17+
18+
if (!response.ok) {
19+
throw new Error(`Account hooks failed with status: ${response.status}`);
20+
}
21+
const data = await response.json();
22+
return data;
23+
} catch (error) {
24+
console.error("Error calling account hooks:", error);
25+
throw error; // rethrow so you can handle it in your component if needed
26+
}
27+
};

builder-frontend/src/context/AuthContext.jsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
} from "firebase/auth";
1616

1717
import { auth } from "../firebase/firebase";
18+
import { authGet } from "@/api/auth";
19+
import { getAccountHooks } from "@/api/account";
1820

1921
const AuthContext = createContext();
2022
const googleProvider = new GoogleAuthProvider();
@@ -41,7 +43,20 @@ export function AuthProvider(props) {
4143
};
4244

4345
const register = async (email, password) => {
44-
return createUserWithEmailAndPassword(auth, email, password);
46+
return createUserWithEmailAndPassword(auth, email, password).then(
47+
(userCredential) => {
48+
// Call "account creation hook" API endpoint here?
49+
console.log("***** after account creation hook *****");
50+
getAccountHooks().then(
51+
() => {
52+
console.log("Successfully hooked the account.");
53+
},
54+
(error) => {
55+
console.log("Error hooking the account", error);
56+
},
57+
);
58+
},
59+
);
4560
};
4661

4762
const loginWithGoogle = async () => {

0 commit comments

Comments
 (0)