cultofcoders:apollo-accounts

v3.4.0Published 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 exposes Meteor Accounts functionality in GraphQL.

Installing

Install on Meteor server

meteor add cultofcoders:apollo-accounts

Initialize the package.

1import { makeExecutableSchema } from 'graphql-tools';
2import { initAccounts } from 'meteor/cultofcoders:apollo-accounts';
3import { load, getSchema } from 'graphql-load';
4
5const { typeDefs, resolvers } = initAccounts({
6  loginWithFacebook: false,
7  loginWithGoogle: false,
8  loginWithLinkedIn: false,
9  loginWithPhone: false,
10  loginWithPassword: true,
11  overrideCreateUser: (createUser, _, args, context) {
12    // Optionally override createUser if you need custom logic
13    // Or simply restrict him from authenticating
14  }
15});
16
17// optional
18load({
19  typeDefs,
20  resolvers,
21});
22
23// Gets all the resolvers and type definitions loaded in graphql-loader
24const schema = getSchema();
25const 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);

Phone support

Login support using phone number and verification code. Requires ujwal:accounts-phone package.

meteor add ujwal:accounts-phone

From your client, execute the following mutation:

mutation createUserWithPhone {
      createUserWithPhone (phone: "+11234567890", profile: {name: "A Phone User"}) {
			success
      }
    }

Server response:

1{
2  "data": {
3    "createUserWithPhone": {
4      "success": true
5    }
6  }
7}

If Twilio has been set up on the server, a verification code will be sent to the phone via SMS.

To login with the verification code, use the following mutation:

mutation loginWithPhone {
	loginWithPhone (phone: "+11234567890", verificationCode: "6593") {
		id
		token
		tokenExpires
	}
}

Server response:

1{
2  "data": {
3    "loginWithPhone": {
4      "id": "eHMzRW9B685curZ63",
5      "token": "Kg9mESwmEAs6xraKZ_hPv0tzOvQpTgMPhWTNXDFCet0",
6      "tokenExpires": 1535581386595
7    }
8  }
9}

You can use the response to store the login token:

1await setTokenStore.set(id, token, new Date(tokenExpires));

To request a new verification code, use the following mutation:

mutation resendPhoneVerification {
	resendPhoneVerification (phone: "+11234567890") {
		success
	}
}

Server response:

1{
2  "data": {
3    "resendPhoneVerification": {
4      "success": true
5    }
6  }
7}

If Twilio has been set up, then the verification code will sent via SMS.

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