# How to redirect to a different URL after successful Okta authentication?

**URL:** <https://devforum.okta.com/t/how-to-redirect-to-a-different-url-after-successful-okta-authentication/21503>\
**Category:** OAuth/OIDC\
**Created:** [August 7, 2022, 6:05pm UTC](https://devforum.okta.com/t/how-to-redirect-to-a-different-url-after-successful-okta-authentication/21503 "2022-08-07T18:05:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zahinhasan43](https://avatars.discourse-cdn.com/v4/letter/z/e19adc/32.png) [@zahinhasan43](https://devforum.okta.com/u/zahinhasan43)\
**Post date:** [August 7, 2022, 6:05pm UTC](https://devforum.okta.com/t/how-to-redirect-to-a-different-url-after-successful-okta-authentication/21503/1 "2022-08-07T18:05:01Z")

</div>

I am trying to integrate Okta authentication to my app.  
The flow is I make a call from `http://localhost:8000` to `http://localhost:5000` where is my node-express app doing the authentication. I was able to successfully authenticate the user. However, after authentication it redirects to `http://localhost:5000` instead of `http://localhost:8000`.

I used this post [Sign users in to your web app using the redirect model | Okta Developer](https://developer.okta.com/docs/guides/sign-into-web-app-redirect/node-express/main/#configure-your-app) to integrate Okta.

I can certainly hardcode the `res.redirect('/')` to redirect me to the `http://localhost:8000`. However, is there any other way I can achieve the same?

````auto
export default class OktaHandler {
    private app: SSOApplication | undefined;
    public register(app: SSOApplication) {
        if (this.app) {
            Log.warn("Duplicate App registration (ignoring)");
            return;
        }
        // const baseURL = this.app.getBaseURL();
        this.app = app;
        app.expInstance.use(session({
            secret: 'CanYouLookTheOtherWay',
            resave: false,
            saveUninitialized: true,
        }));

        app.expInstance.use(passport.initialize());
        app.expInstance.use(passport.session());

        // set up passport
        passport.use('oidc', new Strategy({
            issuer: `https://${yourOktaDomain}/oauth2/default`,
            authorizationURL: `https://${yourOktaDomain}/oauth2/default/v1/authorize`,
            tokenURL: `https://${yourOktaDomain}/oauth2/default/v1/token`,
            userInfoURL: `https://${yourOktaDomain}/oauth2/default/v1/userinfo`,
            clientID: `${yourClientID}`,
            clientSecret: `${yourClientSecret}`,
            callbackURL: `/authorization-code/callback`,
            scope: `openid profile`
        }, (issuer, profile, done) => {
            return done(null, profile);
        }));

        passport.serializeUser((user, next) => {
            next(null, user);
        });
        passport.deserializeUser((obj: any, next) => {
            next(null, obj);
        });

        app.expInstance.get('/oktaLogin', passport.authenticate('oidc'));

        app.expInstance.get('/authorization-code/callback',
            passport.authenticate('oidc', { failureRedirect: '/error' }),
            async (req: any, res: any) => {
                try {
                    const email = (req.user as any).username ?? "";
                    const scopes = await ScopeService.GetScopesForUser(email);
                    const token = TokenManager.createSignedJWTToken({
                        email,
                        google: req.user,
                        scopes
                    }, email);
    
                    const url = req.query["state"] && req.query["state"] + "";
                    const redir = url || "/";
                    res.cookie(SSOApplication.COOKIE_NAME, token, {
                        httpOnly: true,
                        secure: true,
                        sameSite: "none"
                    });
                    Log.audit({
                        action: "Log in",
                        target: undefined,
                        user: email
                    });
                    res.redirect("/");
                } catch (error) {
                    
                }
            }
        );
    }
}```
````

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [February 13, 2024, 1:05am UTC](https://devforum.okta.com/t/how-to-redirect-to-a-different-url-after-successful-okta-authentication/21503/2 "2024-02-13T01:05:22Z")

</div>


