cultofcoders:apollo-accounts

v3.2.3Published 6 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Meteor Apollo Accounts

A implementation of Meteor Accounts only in GraphQL with Apollo.

This package uses the Meteor Accounts methods in GraphQL, it's compatible with the accounts you have saved in your database and you may use apollo-accounts and Meteor's DPP accounts at the same time.

Installing

Install on Meteor server

meteor add cultofcoders:apollo-accounts

Initialize the package.

1import { makeExecutableSchema } from 'graphql-tools';
2import { initAccounts } from 'meteor/nicolaslopezj:apollo-accounts';
3import { load, getSchema } from 'graphql-load';
4
5const { typeDefs, resolvers } = initAccounts({
6  loginWithFacebook: false,
7  loginWithGoogle: false,
8  loginWithLinkedIn: false,
9  loginWithPassword: true,
10});
11
12// optional
13load({
14  typeDefs,
15  resolvers,
16});
17
18// Gets all the resolvers and type definitions loaded in graphql-loader
19const schema = getSchema();
20const executableSchema = makeExecutableSchema(schema);

Install on your apollo app

May or may not be the same app.

npm install --save meteor-apollo-accounts

Examples

Tutorials

Methods

Meteor accounts methods, client side only. All methods are promises.

loginWithPassword

Log the user in with a password.

1import { loginWithPassword } from 'meteor-apollo-accounts';
2
3loginWithPassword({ username, email, password }, apollo);
  • username: Optional. The user's username.

  • email: Optional. The user's email.

  • password: The user's password. The library will hash the string before it sends it to the server.

  • apollo: Apollo client instance.

changePassword

Change the current user's password. Must be logged in.

1import { changePassword } from 'meteor-apollo-accounts';
2
3changePassword({ oldPassword, newPassword }, apollo);
  • oldPassword: The user's current password. This is not sent in plain text over the wire.

  • newPassword: A new password for the user. This is not sent in plain text over the wire.

  • apollo: Apollo client instance.

logout

Log the user out.

1import { logout } from 'meteor-apollo-accounts';
2
3logout(apollo);
  • apollo: Apollo client instance.

createUser

Create a new user.

1import { createUser } from 'meteor-apollo-accounts';
2
3createUser({ username, email, password, profile }, apollo);
  • username: A unique name for this user.

  • email: The user's email address.

  • password: The user's password. This is not sent in plain text over the wire.

  • profile: The profile object based on the UserProfileInput input type.

  • apollo: Apollo client instance.

verifyEmail

Marks the user's email address as verified. Logs the user in afterwards.

1import { verifyEmail } from 'meteor-apollo-accounts';
2
3verifyEmail({ token }, apollo);
  • token: The token retrieved from the verification URL.

  • apollo: Apollo client instance.

forgotPassword

Request a forgot password email.

1import { forgotPassword } from 'meteor-apollo-accounts';
2
3forgotPassword({ email }, apollo);
  • email: The email address to send a password reset link.

  • apollo: Apollo client instance.

resetPassword

Reset the password for a user using a token received in email. Logs the user in afterwards.

1import { resetPassword } from 'meteor-apollo-accounts';
2
3resetPassword({ newPassword, token }, apollo);
  • newPassword: A new password for the user. This is not sent in plain text over the wire.

  • token: The token retrieved from the reset password URL.

  • apollo: Apollo client instance.

loginWithFacebook

Logins the user with a facebook accessToken

1import { loginWithFacebook } from 'meteor-apollo-accounts';
2
3loginWithFacebook({ accessToken }, apollo);

loginWithGoogle

Logins the user with a google accessToken

1import { loginWithGoogle } from 'meteor-apollo-accounts';
2
3loginWithGoogle({ accessToken }, apollo);

onTokenChange

Register a function to be called when a user is logged in or out.

1import { onTokenChange } from 'meteor-apollo-accounts';
2
3onTokenChange(function() {
4  console.log('token did change');
5  apollo.resetStore();
6});

userId

Returns the id of the logged in user.

1import { userId } from 'meteor-apollo-accounts'
2
3async function () {
4  console.log('The user id is:', await userId())
5}

React-Native usage

1//First you'll need to import the Storage library that you'll use to store the user details (userId, tokens...),
2// AsyncStorage is highly recommended.
3
4import {
5  ...
6  AsyncStorage
7} from 'react-native';
8
9import { loginWithPassword, userId, setTokenStore} from 'meteor-apollo-accounts'
10
11// Then you'll have to define a TokenStore for your user data using setTokenStore
12// (for instance when your component is mounted):
13setTokenStore({
14  set: async function ({userId, token, tokenExpires}) {
15    await AsyncStorage.setItem('Meteor.userId', userId)
16    await AsyncStorage.setItem('Meteor.loginToken', token)
17    // AsyncStorage doesn't support Date type so we'll store it as a String
18    await AsyncStorage.setItem('Meteor.loginTokenExpires', tokenExpires.toString())
19  },
20  get: async function () {
21    return {
22      userId: await AsyncStorage.getItem('Meteor.userId'),
23      token: await AsyncStorage.getItem('Meteor.loginToken'),
24      tokenExpires: await AsyncStorage.getItem('Meteor.loginTokenExpires')
25    }
26  }
27})
28
29// Finally, you'll be able to use asynchronously any method from the library:
30async login (event) {
31  event.preventDefault();
32
33  try {
34    const id_ = await loginWithPassword({ "email", "password" }, this.client)
35    this.client.resetStore()
36  } catch (error) {
37
38  }
39}

Contributors