Window is not defined for npm package okta-auth-js

Viewed 24

I'm trying to figure out how to do proper authorization flow for okta-auth-js for my company, but I'm getting a strange issue trying to run my sample code.

[okta-auth-sdk] WARN: Memory storage can only support simple single user use case on server side, please provide custom storageProvider or storageKey if advanced scenarios need to be supported.
file:///C:/Users/.../Documents/Workspace/okta/node_modules/@okta/okta-auth-js/esm/node/oidc/util/loginRedirect.js:41
    if (!isRedirectUri(window.location.href, sdk)) {
                       ^

ReferenceError: window is not defined
    at isLoginRedirect (file:///C:/Users/.../Documents/Workspace/okta/node_modules/@okta/okta-auth-js/esm/node/oidc/util/loginRedirect.js:41:24)
    at OktaAuth.start (file:///C:/Users/.../Documents/Workspace/okta/node_modules/@okta/okta-auth-js/esm/node/OktaAuth.js:230:25)
    at async file:///C:/Users/.../Documents/Workspace/okta/up-front.mjs:13:1

I assume this is something to do with me trying to run this as a es module and not just straight js?

This is my file for up-front.mjs

import * as okta from '@okta/okta-auth-js';

const authClient = new okta.OktaAuth({
    issuer: 'https://.../oauth2/.../default',
    clientId: '...',
    clientSecret: '...',
    scopes: ['license:write'],
    cookies: {
        sessionCookie: true
    }
});

await authClient.start();

const result = await authClient.signInWithCredentials({
    username: '...',
    password: '...'
});

console.log(result);

await authClient.stop();

How do I define window?

Update

According to comments, I used the documentation https://github.com/okta/okta-auth-js#node-js-and-react-native-usage to make the following changes:

up-front.js

var OktaAuth = require('@okta/okta-auth-js').OktaAuth;

const authClient = new OktaAuth({
    issuer: 'https://.../oauth2/.../default',
    clientId: '...',
    clientSecret: '...',
    scopes: ['license:write'],
    cookies: {
        sessionCookie: true
    }
});

authClient.start().then(async () => {
    const result = await authClient.signInWithCredentials({
        username: '...',
        password: '...'
    });
    
    console.log(result);
    
    await authClient.stop();
});

I also tried using all .then and no awaits and it changed nothing

0 Answers
Related