Git Product home page Git Product logo

Comments (3)

vysakh0 avatar vysakh0 commented on May 6, 2024 2

Thank you so much! 👍

For anyone who is also wondering, I tried this approach. 🍺

// authClient.js
import {
  AUTH_LOGIN,
  AUTH_CHECK,
  AUTH_LOGOUT,
} from 'admin-on-rest';
import { AUTH_GET_PERMISSIONS } from 'aor-permissions';
import 'whatwg-fetch'

const authenticate = (email, password) => {
  const GRAPHQL_ENDPOINT = 'https://api.graph.cool/simple/v1/yourid'

  const options = {
    uri: GRAPHQL_ENDPOINT,
    headers: {
      'Content-Type': 'application/json',
    },
    method: 'POST',
    body: JSON.stringify({ query: `
      mutation {
        signinUser(email: {
          email: "${email}"
          password: "${password}"
        }) {
          token
          user {
            name
            email
          }
        }
      }
      `
    })
  }
  return fetch(GRAPHQL_ENDPOINT, options)
}

export default (type, params) => {
  if (type === AUTH_LOGIN) {
    const { username, password } = params;
    return authenticate(username, password).then((res) => {
      const user = JSON.stringify(res);
      window.localStorage.setItem('userData', user);
    }).catch((error) => {
      throw new Error(error.message);
    });
  }
  if (type === AUTH_CHECK) {
    return localStorage.getItem('userData') ? Promise.resolve() : Promise.reject();
  }
  if (type === AUTH_LOGOUT) {
    return firebase.auth().signOut().then(() => {
      localStorage.removeItem('userData');
    }).catch((error) => {
      console.log(error);
    });
  }
  if (type === AUTH_GET_PERMISSIONS) {
    const user = JSON.parse(localStorage.getItem('userData'))
    return Promise.resolve(user.role);
  }
  return Promise.resolve();
}

from aor-graphql.

dervos avatar dervos commented on May 6, 2024 1

I would suggest to just use fetch for a one off mutation. All you're trying to do is requesting the token and putting it in localStorage, keep it simple.

from aor-graphql.

djhi avatar djhi commented on May 6, 2024 1

@dervos is right, the easiest way would be to use fetch.

However, if you really want to use the gql-client, you could create a factory for your authClient, taking the gql-client as a parameter and returning a promise. This promise would resolve to the authClient.

from aor-graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.