Дочинили авторизацию
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { useLocalSearchParams } from "expo-router";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import React, { useEffect, useRef, useState } from "react";
|
||||
import { Text, View } from "react-native";
|
||||
import { useApp } from "../../src/app-context";
|
||||
import type { Consents } from "../../src/types";
|
||||
@@ -9,6 +9,7 @@ export default function AuthCallbackScreen() {
|
||||
const params = useLocalSearchParams<{ code?: string; state?: string; error?: string }>();
|
||||
const app = useApp();
|
||||
const [error, setError] = useState<unknown>();
|
||||
const completionStarted = useRef(false);
|
||||
const retry = () => {
|
||||
if (!params.code || !params.state || typeof window === "undefined") return;
|
||||
const raw = window.sessionStorage.getItem("han.pending-consents");
|
||||
@@ -17,11 +18,14 @@ export default function AuthCallbackScreen() {
|
||||
void app.finishCallback(params.code, params.state, JSON.parse(raw) as Consents).catch(setError);
|
||||
};
|
||||
useEffect(() => {
|
||||
if (completionStarted.current) return;
|
||||
if (params.error) {
|
||||
completionStarted.current = true;
|
||||
setError(new Error("Авторизация отменена или отклонена."));
|
||||
return;
|
||||
}
|
||||
if (!params.code || !params.state) return;
|
||||
completionStarted.current = true;
|
||||
const raw = typeof window !== "undefined" ? window.sessionStorage.getItem("han.pending-consents") : null;
|
||||
if (!raw) {
|
||||
setError(new Error("Не найдены локально принятые согласия. Начните вход заново."));
|
||||
|
||||
@@ -40,7 +40,7 @@ const secureStore = {
|
||||
};
|
||||
|
||||
const random = () => Crypto.randomUUID().replaceAll("-", "") + Crypto.randomUUID().replaceAll("-", "");
|
||||
const redirectUri = AuthSession.makeRedirectUri({ scheme: "han-chat", path: "oauth/callback" });
|
||||
const redirectUri = AuthSession.makeRedirectUri({ scheme: "han-chat", path: "auth/callback" });
|
||||
const tokenEndpoint = `${oidcIssuer}/protocol/openid-connect/token`;
|
||||
|
||||
export function configureAuthFailure(callback: () => void) {
|
||||
@@ -95,6 +95,10 @@ export async function beginAuthorization() {
|
||||
state,
|
||||
nonce,
|
||||
})}`;
|
||||
if (Platform.OS === "web" && typeof window !== "undefined") {
|
||||
window.location.assign(url);
|
||||
return new Promise<never>(() => undefined);
|
||||
}
|
||||
const result = await WebBrowser.openAuthSessionAsync(url, redirectUri);
|
||||
if (result.type !== "success") return { type: result.type as "cancel" | "dismiss" };
|
||||
const callback = new URL(result.url);
|
||||
|
||||
Reference in New Issue
Block a user