auth.ts 2.0 KB

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