auth.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { HttpClient } from "@angular/common/http";
  2. import { Injectable } from "@angular/core";
  3. import { Storage } from "@ionic/storage";
  4. import firebase from "firebase";
  5. import { TwitterApiProvider } from "../twitter-api/twitter-api";
  6. @Injectable()
  7. export class AuthProvider {
  8. authProvider: any;
  9. constructor(
  10. public http: HttpClient,
  11. private storage: Storage,
  12. private twitter: TwitterApiProvider
  13. ) {
  14. const config = {
  15. apiKey: "AIzaSyDRb5IVPCjdKld_5ubsGuP9tEPTRG0kgQ4",
  16. authDomain: "twitter-hosn.firebaseapp.com",
  17. databaseURL: "https://twitter-hosn.firebaseio.com",
  18. projectId: "twitter-hosn",
  19. storageBucket: "twitter-hosn.appspot.com",
  20. messagingSenderId: "502393136960",
  21. appId: "1:502393136960:web:886faa1155c38a37087ca5",
  22. measurementId: "G-XPXJQF3LWV"
  23. };
  24. firebase.initializeApp(config);
  25. this.authProvider = new firebase.auth.TwitterAuthProvider();
  26. this.authProvider.setCustomParameters({
  27. lang: "de"
  28. });
  29. }
  30. /**
  31. * Performs the login to Twitter
  32. */
  33. login() {
  34. return firebase
  35. .auth()
  36. .signInWithRedirect(this.authProvider)
  37. .then(() => firebase.auth().getRedirectResult())
  38. .then(this.setKeys)
  39. .then(() => this.twitter.initApi());
  40. }
  41. getCurrentUser(){
  42. return firebase.auth().currentUser;
  43. }
  44. /**
  45. * Logs the user out by deleting session data
  46. */
  47. logout() {
  48. firebase.auth().signOut().then(function() {
  49. // Sign-out successful.
  50. console.log("user signed out successful");
  51. }, function(error) {
  52. // An error happened.
  53. console.log("UNABLE TO sign user out:",error);
  54. });
  55. this.storage.clear();
  56. }
  57. /**
  58. * Checks if a user is currently logged in
  59. */
  60. async isLoggedIn() {
  61. let accessToken = await this.storage.get("accessTokenKey");
  62. let accessTokenKey = await this.storage.get("accessTokenSecret");
  63. console.log("userid is:",this.storage.get( "userId"));
  64. return accessToken && accessTokenKey;
  65. }
  66. /**
  67. * Saves acces token and user id to locale storage
  68. */
  69. setKeys = async result => {
  70. console.log("SETTING KEYS HERE NOW",result);
  71. await this.storage.set(
  72. "accessTokenKey",
  73. result["credential"]["accessToken"]
  74. );
  75. await this.storage.set("accessTokenSecret", result["credential"]["secret"]);
  76. await this.storage.set(
  77. "userId",
  78. result["additionalUserInfo"]["profile"]["id_str"]
  79. );
  80. };
  81. }