auth.ts 1.6 KB

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