(
provider: string,
{
timeoutMs = IOS_NATIVE_AUTH_TIMEOUT_MS,
onTimeout,
onFailure,
}: {
timeoutMs?: number;
onTimeout?: () => void;
onFailure?: (failure: NativeAuthFailure) => void;
} = {},
)
| 201 | * Google or Apple sign-in fails. |
| 202 | */ |
| 203 | export const iosNativeAuth = async ( |
| 204 | provider: string, |
| 205 | { |
| 206 | timeoutMs = IOS_NATIVE_AUTH_TIMEOUT_MS, |
| 207 | onTimeout, |
| 208 | onFailure, |
| 209 | }: { |
| 210 | timeoutMs?: number; |
| 211 | onTimeout?: () => void; |
| 212 | onFailure?: (failure: NativeAuthFailure) => void; |
| 213 | } = {}, |
| 214 | ): Promise<NativeAuthResponse | undefined> => { |
| 215 | const promise = promisifyEventListener< |
| 216 | NativeAuthReply | typeof nativeAuthTimedOut, |
| 217 | NativeAuthReply |
| 218 | >('native-auth', (event) => event.detail, { |
| 219 | timeout: { ms: timeoutMs, value: nativeAuthTimedOut }, |
| 220 | }); |
| 221 | postWebKitMessage(WebKitMessageHandlers.NativeAuth, provider); |
| 222 | const res = await promise; |
| 223 | if (res === nativeAuthTimedOut) { |
| 224 | onTimeout?.(); |
| 225 | return undefined; |
| 226 | } |
| 227 | if (isNativeAuthFailure(res)) { |
| 228 | onFailure?.(res); |
| 229 | return undefined; |
| 230 | } |
| 231 | if (!res || !('token' in res) || !res.token) { |
| 232 | return undefined; |
| 233 | } |
| 234 | // A sheet that outlived an earlier, timed-out request can answer this one. |
| 235 | // Its token belongs to that provider, so it cannot complete this sign-in. |
| 236 | if (res.provider && res.provider !== provider) { |
| 237 | onFailure?.({ |
| 238 | provider: res.provider, |
| 239 | error: `Reply for ${res.provider} while waiting for ${provider}`, |
| 240 | }); |
| 241 | return undefined; |
| 242 | } |
| 243 | return res; |
| 244 | }; |
no test coverage detected