<!DOCTYPE html>
<html lang="en">
<head>
    <% const rootPath = '../../' %> 
    <%- include(`${rootPath}src/components/common_includes.ejs`) %> 

    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Buzos</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
        let user = null;
        let configureFirebase = null
    </script>
    <script type="module">
        import * as firebase_utils from '../../firebase/utils.js';
        configureFirebase = firebase_utils.configFirebase;
    </script>
</head>
<body>
    <%- include(`${rootPath}/src/components/top_nav_bar/top_nav_bar.ejs`) %>

    <div id="login-view">
        
    </div>
    <script>

        function signInWithMicrosoft() {
            const firebaseConfigured = configureFirebase();

            let firebase_auth = firebaseConfigured.firebase_auth;
            let Oauth_provider = firebaseConfigured.auth_provider;

            const { getAuth, signInWithPopup, OAuthProvider } = firebase_auth;

            const authGot = getAuth();

            signInWithPopup(authGot, Oauth_provider).then((result) => {
                const credential = OAuthProvider.credentialFromResult(result);
                const accessToken = credential.accessToken;

                let idToken = "";
                authGot.currentUser.getIdToken(false).then(token => {
                    idToken = token;
                    document.cookie = `idToken=${idToken}; path=/; SameSite=Lax`
                    document.cookie = `accessToken=${accessToken}; path=/; SameSite=Lax`
                    window.location.reload();
                }).catch(err => {
                    console.error(err)
                })
            }).catch((error) => {
                console.error(error)
            });
        }

    </script>
</body>
</html>