forked from Northgate-Systems/RemitX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
45 lines (39 loc) · 1.41 KB
/
Copy pathroute.ts
File metadata and controls
45 lines (39 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { supabase } from "@/lib/supabase";
import { getCurrentUser } from "@/lib/auth";
import { createTestnetAccount } from "@/lib/stellar";
import { successResponse, errorResponse, unauthorizedResponse } from "@/lib/api-response";
export async function POST() {
try {
const user = await getCurrentUser();
if (!user) {
return unauthorizedResponse();
}
if (user.stellarPublicKey) {
return errorResponse("Account already has a Stellar public key.", 409);
}
const account = await createTestnetAccount();
const { error } = await supabase
.from("users")
.update({ stellarPublicKey: account.publicKey })
.eq("id", user.id);
if (error) {
console.error("Account update error:", error);
return errorResponse("Failed to save Stellar account", 500);
}
// The secret key is returned exactly once, here, and never stored
// server-side. If the user loses it, this testnet account is gone -
// that's the correct tradeoff for a non-custodial wallet, even in dev.
return successResponse(
{
publicKey: account.publicKey,
secretKey: account.secretKey,
warning:
"Save this secret key now - it will not be shown again and is not stored on the server.",
},
201
);
} catch (err) {
console.error("Account creation error:", err);
return errorResponse("Failed to create Stellar account", 500);
}
}