jeudi 28 octobre 2021

Trying to Customize the ApolloService to enable Authorization in Ember 3.28. Any special configuration I need to plugin the apollo.js to be my config

Ember-Octane 3.28 Configured the enviroment.js file with the graphql endpoint, My apollo.js file in the service folder looks like

import ApolloService from 'ember-apollo-client/services/apollo';
import { inject as service } from '@ember/service';
import { setContext } from '@apollo/client/link/context';

export default class OverridenApollo extends ApolloService {
  @service session;

  link() {
    let httpLink = super.link();

    let authLink = setContext((request, context) => {
      return this._runAuthorize(request, context);
    });
    return authLink.concat(httpLink);
  }

  _runAuthorize() {
    if (!this.session.isAuthenticated) {
      return {};
    }
    return new Promise((success) => {
      let headers = {};
      let token = this.session.data.authenticated.token;
      headers['Authorization'] = `Bearer ${token}`;

      success({ headers });
    });
  }

  clientOptions() {
    return {
      link: this.link(),
      cache: this.cache,
    };
  }

 //...
}

Using ember-apollo-client pkg




Aucun commentaire:

Enregistrer un commentaire