Skip to content

Commit 8b6cce8

Browse files
committed
Streamlined oAuth device flow
1 parent 9283a54 commit 8b6cce8

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

src/device/DeviceAuthPage.tsx

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export const DeviceAuthPage: React.FC = () => {
4646
const [error, setError] = React.useState<string | null>(null);
4747
const [success, setSuccess] = React.useState(false);
4848
const [step, setStep] = React.useState<"code" | "confirm">("code");
49+
const [autoSubmitted, setAutoSubmitted] = React.useState(false);
4950

5051
// If not authenticated, redirect to login with state for return
5152
if (!ApiHelper.isAuthenticated) {
@@ -54,20 +55,26 @@ export const DeviceAuthPage: React.FC = () => {
5455

5556
const userChurches = context?.userChurches || [];
5657

57-
const handleCodeSubmit = async (e: React.FormEvent) => {
58-
e.preventDefault();
58+
// Auto-submit if code is provided in query string
59+
React.useEffect(() => {
60+
const codeFromUrl = searchParams.get("code");
61+
if (codeFromUrl && codeFromUrl.length >= 6 && !autoSubmitted && step === "code") {
62+
setAutoSubmitted(true);
63+
submitCode(codeFromUrl.replace(/-/g, "").toUpperCase().slice(0, 6));
64+
}
65+
}, [searchParams, autoSubmitted, step]);
66+
67+
const submitCode = async (code: string) => {
5968
setLoading(true);
6069
setError(null);
6170

6271
try {
63-
// Normalize code (remove hyphens, uppercase)
64-
const normalizedCode = userCode.replace(/-/g, "").toUpperCase();
65-
66-
const result = await ApiHelper.get(`/oauth/device/pending/${normalizedCode}`, "MembershipApi");
72+
const result = await ApiHelper.get(`/oauth/device/pending/${code}`, "MembershipApi");
6773

6874
if (result && !result.error) {
6975
setDeviceInfo(result);
7076
setStep("confirm");
77+
// Auto-select church if only one
7178
if (userChurches.length === 1) {
7279
setSelectedChurchId(userChurches[0].church.id);
7380
}
@@ -81,6 +88,12 @@ export const DeviceAuthPage: React.FC = () => {
8188
}
8289
};
8390

91+
const handleCodeSubmit = async (e: React.FormEvent) => {
92+
e.preventDefault();
93+
const normalizedCode = userCode.replace(/-/g, "").toUpperCase();
94+
await submitCode(normalizedCode);
95+
};
96+
8497
const handleApprove = async () => {
8598
if (!selectedChurchId) {
8699
setError("Please select a church");
@@ -200,21 +213,27 @@ export const DeviceAuthPage: React.FC = () => {
200213
</Typography>
201214
</Alert>
202215

203-
<FormControl fullWidth sx={{ mb: 2 }}>
204-
<InputLabel id="church-select-label">Select Church</InputLabel>
205-
<Select
206-
labelId="church-select-label"
207-
value={selectedChurchId}
208-
label="Select Church"
209-
onChange={(e) => setSelectedChurchId(e.target.value)}
210-
>
211-
{userChurches.map((uc: LoginUserChurchInterface) => (
212-
<MenuItem key={uc.church.id} value={uc.church.id}>
213-
{uc.church.name}
214-
</MenuItem>
215-
))}
216-
</Select>
217-
</FormControl>
216+
{userChurches.length > 1 ? (
217+
<FormControl fullWidth sx={{ mb: 2 }}>
218+
<InputLabel id="church-select-label">Select Church</InputLabel>
219+
<Select
220+
labelId="church-select-label"
221+
value={selectedChurchId}
222+
label="Select Church"
223+
onChange={(e) => setSelectedChurchId(e.target.value)}
224+
>
225+
{userChurches.map((uc: LoginUserChurchInterface) => (
226+
<MenuItem key={uc.church.id} value={uc.church.id}>
227+
{uc.church.name}
228+
</MenuItem>
229+
))}
230+
</Select>
231+
</FormControl>
232+
) : userChurches.length === 1 && (
233+
<Typography variant="body2" sx={{ mb: 2 }}>
234+
<strong>Church:</strong> {userChurches[0].church.name}
235+
</Typography>
236+
)}
218237

219238
<Box sx={{ mb: 2 }}>
220239
<Typography variant="body2" fontWeight="bold">

0 commit comments

Comments
 (0)