> ## Documentation Index
> Fetch the complete documentation index at: https://docs.portalhq.io/llms.txt
> Use this file to discover all available pages before exploring further.

# parseRedirect

> Reads an inbound redirect URL into the sign-in it completes, without completing it, or returns nil when the URL is not a Portal sign-in.

**Function Signature**

```swift theme={null}
public func parseRedirect(_ url: URL) -> AuthRedirect?
public func parseRedirect(_ url: String) -> AuthRedirect?
```

A `PortalAuth` method. Reads the URL locally and synchronously: it sends nothing to Portal, touches no storage, logs nothing and uses up no grant, so it is safe to call on every URL your app receives. Complete the result with the method for its case: [`getMagicLinkDetails`](./portalauthgetmagiclinkdetails) and, once the user confirms the email, [`confirmMagicLink`](./portalauthconfirmmagiclink) for a magic link; [`completeOAuthLogin`](./portalauthcompleteoauthlogin) for Google or Apple.

**Parameters**

* **`url`**: The URL iOS delivered to your app, as a `URL` (from `onOpenURL`, your scene delegate or your app delegate) or a `String`. The `URL` overload forwards `absoluteString`, so percent-encoding stays intact and the grant is decoded exactly once.

**Returns**

* **`AuthRedirect?`**:
  * **`.magicLink(AuthRedirect.MagicLink)`**: A magic link. Pass its `token` to `getMagicLinkDetails`, show the user the email, and call `confirmMagicLink` only once they confirm it.
  * **`.oauth(AuthRedirect.OAuth)`**: A Google or Apple grant. `provider` is `.google` or `.apple`. Pass every one to `completeOAuthLogin`: it completes only the sign-in this device started, so a grant someone else obtained, opened through your redirect URL by a web page, message or app, signs nobody in.
  * **`.failed(error:)`**: The sign-in did not complete — the user cancelled, the provider rejected it, or it failed for any other reason — so Portal redirected with an error in the URL (`error=oauth_failed`) instead of a grant. Nothing was sent to Portal. The value is bounded to 100 characters, but it comes from an inbound URL that any app or website can open, so show your own text rather than the raw value.
  * **`nil`**: The URL is not a Client Auth redirect for this instance. Let your other URL handlers try. Returned when the URL:
    * is empty or longer than 8192 UTF-16 code units (checked before it is parsed),
    * does not target this instance's `redirectUrl` (scheme and host compared case-insensitively, the path case-sensitively, ignoring trailing slashes and surrounding whitespace),
    * carries both `authMethod` and `login_type` (Portal never sends both), or
    * carries no `error` parameter and either no `token` (or an empty or whitespace-only one) or no sign-in marker this SDK recognizes.

**Throws**

Nothing. A redirect that reports an error is `.failed(error:)`.

**Types**

```swift theme={null}
public enum AuthRedirect: Hashable, Sendable {
    case magicLink(MagicLink)
    case oauth(OAuth)
    case failed(error: String)

    public struct MagicLink: Hashable, Sendable {
        public let token: String
    }

    public struct OAuth: Hashable, Sendable {
        public let provider: AuthMethod
        // The grant token is internal: only completeOAuthLogin(_:) reads it.
    }
}
```

**Notes**

* Only `parseRedirect` can build a `.magicLink` or `.oauth` value; their initializers are not public.
* Printing an `AuthRedirect` (`description`, `debugDescription`, `dump`) shows `<redacted>` in place of the token. `Equatable` and `Hashable` still compare the real token, so two values for the same link are equal. `MagicLink.token` read on its own is a plain `String`, so keep the `MagicLink` value rather than the bare token, and never log or persist it.
* Unless it carries both `authMethod` and `login_type`, a redirect that carries `?error=…` is `.failed(error:)`, even when it also carries a token or a sign-in marker, and its token is never handed out.
* Magic-link redirects carry `authMethod=EMAIL_MAGIC_LINK`; Google and Apple redirects carry `login_type=GOOGLE` or `login_type=APPLE`. Both are matched exactly. See [What Portal appends](https://docs.portalhq.io/resources/authentication/redirect-urls.md#what-portal-appends).
* If your redirect URL already carries a `token` parameter, the last occurrence is read, which is always the one Portal appended.
* Never log the raw URL; it contains the grant.
* Available starting from SDK version 8.2.0.

**Example Usage**

```swift theme={null}
import Foundation
import PortalSwift

@MainActor
func route(_ url: URL) async {
    switch auth.parseRedirect(url) {
    case let .magicLink(link)?:
        // A link the user already confirmed goes straight to confirmMagicLink; any other
        // is looked up, shown, and confirmed only once the user agrees. See getMagicLinkDetails.
        await showMagicLinkConfirmation(for: link)
    case let .oauth(redirect)?:
        // completeOAuthLogin refuses a redirect this device did not start. See completeOAuthLogin.
        await completeOAuth(redirect)
    case .failed?:
        // The redirect URL carried an error instead of a grant; nothing was sent to Portal.
        showSignInError("Sign-in was not completed. Please try again.")
    case nil:
        // Not a Portal sign-in redirect.
        break
    }
}
```

**Related Documentation**

* [getMagicLinkDetails reference](https://docs.portalhq.io/sdks/ios/reference/portalauthgetmagiclinkdetails.md)
* [confirmMagicLink reference](https://docs.portalhq.io/sdks/ios/reference/portalauthconfirmmagiclink.md)
* [completeOAuthLogin reference](https://docs.portalhq.io/sdks/ios/reference/portalauthcompleteoauthlogin.md)
* [Read the redirect](https://docs.portalhq.io/sdks/ios/guide/client-auth.md#read-the-redirect)
* [Register your redirect](https://docs.portalhq.io/sdks/ios/guide/client-auth.md#register-your-redirect)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.