0

As part of a custom OAuth2 flow, the user is being redirected back to a Lightning App Page with URL:

https://x-dev-ed.lightning.force.com/lightning/n/namespace__Page_Name?state=q765wYYgnvzVAsod&code=c1BF-iddZNkD1aMxVYp8p5smeAQUxITQUesC3Q_ggYHyNJzm

With a few query string params state and code.

But the page does a refresh before it loads, and the query string params are removed.

I want to access the values in state and code from the custom LWC component on the page?

I have tried looking in CurrentPageReference:

import { CurrentPageReference } from "lightning/navigation";

Using:

@wire(CurrentPageReference)
currentPageReference;

connectedCallback() {
    console.log("state is: " + JSON.stringify(this.currentPageReference.state));
}

But the state object is empty {}

And, I've tried getting the value from the URL:

connectedCallback() {
    const param = "code";
    const paramValue = this.getUrlParamValue(window.location.href, param);
    console.log(param + ": " + paramValue);
}

getUrlParamValue(url, key) {
    return new URL(url).searchParams.get(key);
}

But nothing.

How can I access the values in state and code from the custom LWC component on the page?

0

1 Answer 1

0

I had to create a Visualforce page to capture the query string params and redirect:

<apex:page showHeader="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false">

    <head>
        <title>OAuth 2.0 Redirect</title>
    </head>
    <script>
    
    window.onload = onLoad;
    
    function onLoad() {
        const code = getUrlParamValue(window.location.href, 'code');
        location.href = '/lightning/n/namespace__Page_Name?namespace__code=' + code;
    }

    function getUrlParamValue(url, key) {
        return new URL(url).searchParams.get(key);
    }

    </script>
    <body>
        Redirecting...
    </body>
</apex:page>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.