Botão entrar e sair Google via Firebase

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">


<!--<meta name="google-signin-client_id" content="***.apps.googleusercontent.com" />-->
<meta name="google-signin-scope" content="profile email">

</head>
<body>
<button onclick="logar()">Logar</button>
<button onclick="sair()">Sair</button>
<script src="https://www.gstatic.com/firebasejs/4.6.2/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "***-***",
authDomain: "tenisfacil-187611.firebaseapp.com",
databaseURL: "http://tenisfacil-187611.firebaseio.com",
projectId: "tenisfacil-187611",
storageBucket: "tenisfacil-187611.appspot.com",
messagingSenderId: "***"
};
firebase.initializeApp(config);

var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().languageCode = 'pt';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();

function logar() {
firebase.auth().signInWithPopup(provider).then(function (result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
console.log(user);
// ...
}).catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
}

/**verifica se já logou*/
function isUserEqual(googleUser, firebaseUser) {
if (firebaseUser) {
var providerData = firebaseUser.providerData;
for (var i = 0; i < providerData.length; i++) {
if (providerData[i].providerId === firebase.auth.GoogleAuthProvider.PROVIDER_ID &&
providerData[i].uid === googleUser.getBasicProfile().getId()) {
// We don't need to reauth the Firebase connection.
return true;
}
}
}
return false;
}

function sair() {
firebase.auth().signOut().then(function () {
// Sign-out successful.
console.log("Saiu com sucesso!!!");
}).catch(function (error) {
// An error happened.
console.error(error);
});
}
</script>
</body>
</html>

 

Comentários

Postagens mais visitadas deste blog

SPAM de hacker pedindo bitcoin

MER BRModelo