<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dashboard</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link href="https://fonts.googleapis.com/css?family=Roboto|Varela+Round" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://code.getmdl.io/1.1.3/material.indigo-pink.min.css">
<script defer src="https://code.getmdl.io/1.1.3/material.min.js"></script>
<style type="text/css">
    body {
        color: #999;
		background: #dfe7e9;
		font-family: 'Varela Round', sans-serif;
	}
	.form-control {
		box-shadow: none;
		border-color: #ddd;
	}
	.form-control:focus {
		border-color: #1E88E5; 
	}
	.login-form {
        width: 350px;
		margin: 0 auto;
		padding: 30px 0;
	}
    .login-form form {
        color: #434343;
		border-radius: 1px;
    	margin-bottom: 15px;
        background: #fff;
		border: 1px solid #f3f3f3;
        box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
        padding: 30px;
	}
	.login-form h4 {
		text-align: center;
		font-size: 22px;
        margin-bottom: 20px;
	}
    .login-form .avatar {
        color: #fff;
		margin: 0 auto 30px;
        text-align: center;
		width: 100px;
		height: 100px;
		border-radius: 50%;
		z-index: 9;
		background: #1E88E5;
		padding: 15px;
		box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.1);
	}
    .login-form .avatar i {
        font-size: 62px;
    }
    .login-form .form-group {
        margin-bottom: 20px;
    }
	.login-form .form-control, .login-form .btn {
		min-height: 40px;
		border-radius: 2px; 
        transition: all 0.5s;
	}
	.login-form .close {
        position: absolute;
		top: 15px;
		right: 15px;
	}
	.login-form .btn {
		background: #1E88E5;
		border: none;
		line-height: normal;
	}
	.login-form .btn:hover, .login-form .btn:focus {
		background: #1565C0;
	}
    .login-form .checkbox-inline {
        float: left;
    }
    .login-form input[type="checkbox"] {
        margin-top: 2px;
    }
    .login-form .forgot-link {
        float: right;
    }
    .login-form .small {
        font-size: 13px;
    }
    .login-form a {
        color: #1E88E5;
    }
</style>
</head>
<body>
<div class="login-form"> 
    <div id="p2" class="mdl-progress mdl-js-progress mdl-progress__indeterminate"></div>   
    <form id="idForm">
		<div class="avatar"><i class="material-icons">&#xE7FF;</i></div>
        <h4 class="modal-title">Login to Your Dashboard</h4>
        <% if (message.length > 0) { %>
            <div class="alert alert-danger"><%= message %></div>
        <% } %>
        <div id="err" class="alert alert-danger"></div>
        <div class="form-group">
            <input type="email" class="form-control input-lg" id="email" name="email" placeholder="Email Address" required="required">
        </div>
        <div class="form-group">
            <input type="password" class="form-control" id="pass" name="password" placeholder="Password" required="required">
        </div>
        <div class="form-group small clearfix">
            <label class="checkbox-inline"><input type="checkbox"> Remember me</label>
            <a href="/reset" class="forgot-link">Forgot Password?</a>
        </div> 
        <input type="submit" class="btn btn-primary btn-block btn-lg" value="Login">              
    </form>			
    <div class="text-center small">Don't have an account? <a href="/register">Sign up</a></div>
</div>

<%- include("firebase"); -%> 
<script>
    $(function(){
        $('#err').hide();
        $('#p2').hide();
        $("#idForm").submit(function(e) {
            $('#pass').attr('disabled','');
            $('#p2').show();
            e.preventDefault(); // avoid to execute the actual submit of the form.
            let email= $('#email').val();
            let password=$('#pass').val();
            
            firebase.auth().setPersistence(firebase.auth.Auth.Persistence.NONE);

            // When the user signs in with email and password.
            firebase.auth().signInWithEmailAndPassword(email, password).then(user => {
                // Get the user's ID token as it is needed to exchange for a session cookie.
                firebase.auth().currentUser.getIdToken().then(idToken => {   // idToken==jwt
                    // Session login endpoint is queried and the session cookie is set.
                    //console.log(idToken);
                    //return postIdTokenToSessionLogin('/sessionLogin', idToken);

                    window.location.assign('/sessionLogin?token='+idToken);
                });
            }).then(() => {
            // A page redirect would suffice as the persistence is set to NONE.
            return firebase.auth().signOut();
            }).catch(err=>{
                console.log(err.message);
                $('#err').show();
                $('#p2').hide();
                $('#pass').removeAttr('disabled');
    
                $('#err').text(err.message);
            })
        });
    });
</script>

</body>
</html>                                		                            