Guest User

Untitled

a guest
Mar 17th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. <script>
  2. // Get elements
  3. const txtEmail = document.getElementById('txtEmail');
  4. const txtPassword = document.getElementById('txtPassword');
  5. const btnDoLogin = document.getElementById('btnDoLogin');
  6.  
  7. // Login event
  8. btnDoLogin.addEventListener('click', e => {
  9. // Get email and password
  10. const email = txtEmail.value;
  11. const password = txtPassword.value;
  12. const auth = firebase.auth();
  13. // Sign in
  14. const promise = auth.signInWithEmailAndPassword(email, password);
  15. promise.catch(e => document.getElementById("errorThings").innerHTML = e.message);
  16. });
  17.  
  18. // Add realtime listener
  19. firebase.auth().onAuthStateChanged(firebaseUser => {
  20. if(firebaseUser){
  21. firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
  22. console.log(idToken)
  23. window.location = '/doLoginIn/' + idToken;
  24. // Send token to your backend via HTTPS
  25. // ...
  26. }).catch(function(error) {
  27. // Handle error
  28. });
  29.  
  30. } else {
  31. console.log('not logged in');
  32. }
  33. });
  34. </script>
  35.  
  36. decoded_token = auth.verify_id_token(id_token)
  37.  
  38. @app.route("/login")
  39. def login():
  40. return render_template("login.html")
  41.  
  42. @app.route("/doLoginIn")
  43. @app.route("/doLoginIn/<id_token>")
  44. def doLoginIn(id_token=None):
  45. try:
  46. decoded_token = auth.verify_id_token(id_token)
  47. print(decoded_token)
  48. session['email'] = decoded_token['email']
  49. session['loggedOn'] = True
  50. return redirect("/dashboard")
  51. except:
  52. return redirect("/login")
  53.  
  54. @app.route("/dashboard")
  55. def dashboard():
  56. return render_template("dashboard.html")
Add Comment
Please, Sign In to add comment