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 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?
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) {
}
}
);
}
}```