Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication Callback Issue #36

Open
Younis-Ahmed opened this issue Jan 24, 2025 · 0 comments
Open

Authentication Callback Issue #36

Younis-Ahmed opened this issue Jan 24, 2025 · 0 comments

Comments

@Younis-Ahmed
Copy link

Authentication Callback Issue

Environment

  • OS: Ubuntu wsl2
  • Browser: Chrome
  • nodejs : 22.2.0

Description

When attempting to authenticate using the login function, the callback URL (/auth/callback) gets stuck if the user is not already authenticated. However, if the user is already authenticated and starts the CLI with the login function, everything works as intended.

Steps to Reproduce

  1. Start the CLI and call the login command.
  2. The browser opens the authentication URL: https://partner.zid.sa/cli/authorized --> https://partner.zid.sa/login.
  3. If the user is not already authenticated, (live session).
  4. Observe that the callback URL (/auth/callback) does not get triggered to GET TOKEN.

Expected Behavior

The callback URL should be triggered after the user inputs their credentials and the token should be retrieved successfully.

Actual Behavior

The callback URL (/auth/callback) gets stuck and does not retrieve the token if the user is not already authenticated.

Troubleshooting Steps Taken

  1. Browser Caching: Cleared browser cache to ensure the authentication process starts fresh.
  2. Network Issues: Confirmed that there are no network issues or firewalls blocking the request to http://localhost:4444/auth/callback.
  3. Port Related Issue : Ensured the port is free and forwarding from origin

Additional Information

  • The issue does not occur if the user is already authenticated.
  • Added additional logging to trace the flow of the authentication process.

Code Snippet

const login = async (
    port: number = 4444,
): Promise<void> => {
    logger.log('Starting authentication process...');
    const authUrl = `https://partner.zid.sa/cli/authorized`;
    logger.log('Opening Zid Partner Dashboard in your default browser...');
    await open(authUrl);

    const app = express();
    app.use(cors());
    const server = app.listen(port, () => {
        logger.log(`Listening for authentication callback on http://localhost:${port}/auth/callback`);
    });

    app.get('/auth/callback', (req, res) => {
        logger.log('Received callback request');
        console.log(req.query);
        const authToken = req.query.token;
        if (authToken && typeof authToken === 'string') {
            logger.log('Token received:', authToken);
            if (!setToken(authToken)) {
                res.send('Failed to save token.');
                logger.log('Failed to save token.');
                server.close();
                return;
            }
            res.send('Authentication successful! You can close this window.');
            logger.log('Authentication successful!');
        } else {
            let msg = 'Authentication failed.';
            res.send(msg);
            logger.log(msg, 'red');
        }
        server.close();
    });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant