/*
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */
const express = require("express");
const msal = require('@azure/msal-node');

const SERVER_PORT = process.env.PORT || 3000;
const REDIRECT_URI = "https://citrix.node4co.uk/redirect";

// Before running the sample, you will need to replace the values in the config, 
// including the clientSecret
const config = {
    auth: {
        clientId: "769b81fe-3663-4034-824f-8c07f3a74138",
        authority: "https://login.microsoftonline.com/common",
        clientSecret: process.env.secret
    },
    system: {
        loggerOptions: {
            loggerCallback(loglevel, message, containsPii) {
                console.log(message);
            },
            piiLoggingEnabled: false,
            logLevel: msal.LogLevel.Verbose,
        }
    }
};

// Create msal application object
const pca = new msal.ConfidentialClientApplication(config);

// Create Express App and Routes
const app = express();

app.get('/', (req, res) => {
    const authCodeUrlParameters = {
        scopes: ["user.read"],
        redirectUri: REDIRECT_URI,
    };

    // get url to sign user in and consent to scopes needed for application
    pca.getAuthCodeUrl(authCodeUrlParameters).then((response) => {
        res.redirect(response);
    }).catch((error) => console.log(JSON.stringify(error)));
});

app.get('/redirect', (req, res) => {
    res.redirect(302, 'https://citrix.node4.co.uk');
    // const tokenRequest = {
    //     code: req.query.code,
    //     scopes: ["user.read"],
    //     redirectUri: REDIRECT_URI,
    // };

    // pca.acquireTokenByCode(tokenRequest).then((response) => {
    //     console.log("\nResponse: \n:", response);
    //     res.sendStatus(200);
    // }).catch((error) => {
    //     console.log(error);
    //     res.status(500).send(error);
    // });
});


app.listen(SERVER_PORT, () => console.log(`Msal Node Auth Code Sample app listening on port ${SERVER_PORT}!`))
