auth.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. console.log("Hello AuthProvider Provider");
  15. var config = {
  16. apiKey: "AIzaSyCMYjjtPPZak7wBBnh9sy8Yr3Fz1145MuM",
  17. authDomain: "hybridosn.firebaseapp.com",
  18. databaseURL: "https://hybridosn.firebaseio.com",
  19. storageBucket: "hybridosn.appspot.com"
  20. };
  21. firebase.initializeApp(config);
  22. this.authProvider = new firebase.auth.TwitterAuthProvider();
  23. this.authProvider.setCustomParameters({
  24. lang: "de"
  25. });
  26. }
  27. login() {
  28. return firebase
  29. .auth()
  30. .signInWithRedirect(this.authProvider)
  31. .then(() => firebase.auth().getRedirectResult())
  32. .then(this.setKeys)
  33. .then(() => this.twitter.initApi());
  34. }
  35. logout() {
  36. this.storage.clear();
  37. }
  38. async isLoggedIn() {
  39. let accessToken = await this.storage.get("accessTokenKey");
  40. let accessTokenKey = await this.storage.get("accessTokenSecret");
  41. return accessToken && accessTokenKey;
  42. }
  43. setKeys = async result => {
  44. await this.storage.set(
  45. "accessTokenKey",
  46. result["credential"]["accessToken"]
  47. );
  48. await this.storage.set("accessTokenSecret", result["credential"]["secret"]);
  49. await this.storage.set(
  50. "userId",
  51. result["additionalUserInfo"]["profile"]["id_str"]
  52. );
  53. };
  54. }