All files / client index.js

7.69% Statements 2/26
0% Branches 0/1
0% Functions 0/2
8.33% Lines 2/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 981x       1x                                                                                                                                                                                          
import gql from 'graphql-tag';
 
// check and warn if regenerator-runtime not installed / present
 
const mutation = gql`
mutation login (
  $email: String!
  $password: String!
) {
  passportLoginEmail (
    email: $email
    password: $password
  ) {
    error
    token
    userId
  }
}
`;
 
class ApolloPassport {
 
  constructor({ apolloClient }) {
    this.apolloClient = apolloClient;
 
    this._subscribers = new Set();
 
    this._token = localStorage.getItem('apToken');
    this._userId = localStorage.getItem('apUserId');
    this._verified = false;
 
    if (this._token)
      Ithis.assertToken();
  }
 
  assertToken() {
 
  }
 
  async loginWithEmail(email, password) {
    const result = await this.apolloClient.mutate({
      mutation,
      variables: { email, password }
    });
 
    // if error...
 
    const queryResult = result.data.passportLoginEmail;
 
    if (queryResult.error)
      throw new Error(queryResult.error);
 
    localStorage.setItem('apToken', queryResult.token);
    localStorage.setItem('apUserId', queryResult.userId);
    this._userId = queryResult.userId;
 
    this.emitState();
  }
 
  signupWithEmail(email, password) {
 
  }
 
  getState() {
    return {
      userId: this._userId,
      verified: this._verified
    };
  }
 
  subscribe(callback) {
    this._subscribers.add(callback);
  }
 
  unsubscribe(callback) {
    this._subscribers.delete(callback);
  }
 
  emitState() {
    const state = this.getState();
    this._subscribers.forEach(callback => callback(state));
  }
 
  // if it's not reactive does this make any sense?
  userId() {
    return this._userId;
  }
 
  logout() {
    localStorage.removeItem('apToken');
    localStorage.removeItem('apUserId');
    delete this._userId;
 
    this.emitState();
  }
}
 
export default ApolloPassport;