auth.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. /**
  27. * Performs the login to Twitter
  28. */
  29. login() {
  30. return firebase
  31. .auth()
  32. .signInWithRedirect(this.authProvider)
  33. .then(() => firebase.auth().getRedirectResult())
  34. .then(this.setKeys)
  35. .then(() => this.twitter.initApi());
  36. }
  37. /**
  38. * Logs the user out by deleting session data
  39. */
  40. logout() {
  41. this.storage.clear();
  42. }
  43. /**
  44. * Checks if a user is currently logged in
  45. */
  46. async isLoggedIn() {
  47. let accessToken = await this.storage.get("accessTokenKey");
  48. let accessTokenKey = await this.storage.get("accessTokenSecret");
  49. return accessToken && accessTokenKey;
  50. }
  51. /**
  52. * Saves acces token and user id to locale storage
  53. */
  54. setKeys = async result => {
  55. await this.storage.set(
  56. "accessTokenKey",
  57. result["credential"]["accessToken"]
  58. );
  59. await this.storage.set("accessTokenSecret", result["credential"]["secret"]);
  60. await this.storage.set(
  61. "userId",
  62. result["additionalUserInfo"]["profile"]["id_str"]
  63. );
  64. };
  65. }