socialize:user-profile

v1.0.5Published 3 years ago

User Profile

This package provides the bare minimum required for a user profile and is intended to be built upon to create a custom profile class that meets your needs.

This is a Meteor package with part of it's code published as a companion NPM package made to work with clients other than Meteor. For example your server is Meteor, but you want to build a React Native app for the client. This allows you to share code between your Meteor server and other clients to give you a competitive advantage when bringing your mobile and web application to market.

Supporting The Project

Finding the time to maintain FOSS projects can be quite difficult. I am myself responsible for over 30 personal projects across 2 platforms, as well as Multiple others maintained by the Meteor Community Packages organization. Therfore, if you appreciate my work, I ask that you either sponsor my work through GitHub, or donate via Paypal or Patreon. Every dollar helps give cause for spending my free time fielding issues, feature requests, pull requests and releasing updates. Info can be found in the "Sponsor this project" section of the GitHub Repo

Meteor Installation

This package relies on the npm package simpl-schema so you will need to make sure it is installed as well.

meteor npm install --save simpl-schema
meteor add socialize:user-profile

Note

This package completely disables updates to the users collection from the client to remove the ability of users to arbitrarily update the profile field on the user document.

NPM Installation

When using this package with React Native, the dependency tree ensures that simpl-schema is loaded so there's no need to install it as when using within Meteor.

npm install --save @socialize/user-profile

Usage Outside Meteor

The client side parts of this package are published to NPM as @socialize/cloudinary for use in front ends outside of Meteor.

When using the npm package you'll need to connect to a server, which hosts the server side Meteor code for your app, using Meteor.connect as per the @socialize/react-native-meteor usage example documentation.

1Meteor.connect('ws://192.168.X.X:3000/websocket');

React Native

When using this package with React Native there is some minor setup required by the @socialize/react-native-meteor package. See @socialize/react-native-meteor react-native for necessary instructions.

Automatic Profile Creation

This package will automatically create user profiles when a new user is created. To disable this behavior you can set User.disableProfileCreation = true.

1import { User } from 'meteor/socialize:user-model';
2
3User.disableProfileCreation = true;

Basic Usage

The Profile class provides your starting point. From there you may want to add methods to the class, and extend the schema to build out a custom user profile for your particular application. You can add methods in two ways, either by using the methods method provided by the BaseModel class, or by extending the profile class directly and including the new methods as part of the new class. If you extend the class directly though, you will need to call the updateTransformFunction() method of the new class You could also extend the class instead and provide the methods as part of the new class so that find and findOne calls would return instances of the new class.

Depending on the environment, Meteor or React Native, you'll need to import classes slightly differently. Apart from imports the rest of the code works exactly the same in both environments.

1// Meteor Imports
2import { Profile, ProfilesCollection } from 'meteor/socialize:user-profile';
1// React Native Imports
2import { Profile, ProfilesCollection } from '@socialize/user-profile';

Extending using methods()

1Profile.methods({
2    fullName() {
3        return `${this.firstName} ${this.lastName}`;
4    }
5});
6
7var userProfile = ProfilesCollection.findOne();
8
9userProfile.fullName(); //=> "John Doe"

Extending Profile class

1export class EnhancedProfile extends Profile{
2    fullName() {
3        return `${this.firstName} ${this.lastName}`;
4    }
5}
6
7var userProfile = ProfilesCollection.findOne();
8
9userProfile.fullName(); //=> "John Doe"

Extending The Schema

Currently the schema for Profile has the following definition.

1{
2    _id: {
3        type: String,
4        regEx: SimpleSchema.RegEx.Id,
5        autoValue() {
6            if (this.isInsert && !this.isFromTrustedCode) {
7                return this.userId;
8            }
9            return undefined;
10        },
11    },
12    username: {
13        type: String,
14        index: 1,
15        unique: true,
16        optional: true,
17        denyUpdate: true,
18    },
19    createdAt: {
20        type: Date,
21        autoValue() {
22            if (this.isInsert) {
23                return ServerTime.date();
24            }
25            return undefined;
26        },
27        denyUpdate: true,
28    },
29    updatedAt: {
30        type: Date,
31        autoValue() {
32            return ServerTime.date();
33        },
34    }
35}

As part of the above examples, in the fullName method we return a string containing the firstName and lastName properties of the document. For these to be allowed as part of the profile document, you will need to add them to the schema for Profile. This can be accomplished by using the attachSchema method inherited from BaseModel.

The schema is handled by SimpleSchema so passing a new schema instance to the attachSchema method will merge the new schema into the old one .

1    Profile.attachSchema({
2        "firstName":{
3            type:String,
4            required: true
5        },
6        "lastName":{
7            type:String,
8            required: true
9        }
10    })

User Extensions

This package extends the socialize:user-model package with a profile method which will return the profile for the found user.

When using with React Native you'll need to import the package so that it properly extends the user class with the profile method.

1import '@socialize/user-profile';
1let user = Meteor.users.findOne();
2
3user.profile();
4
5
6Meteor.user().profile(); // the current users profile

Note

user.profile() will be undefined on the client if profile is not published from the server.

Advanced Usage

The Profile class extends the LinkParent class provided by socialize:linkable-model. This allows you to extend the Profile class using linkable packages such as socialize:likeable, socialize:commentable, and socialize:postable.

For example you could create a profile that would allow other users to add posts to it.

1// Meteor Imports
2import { PostableModel } from 'meteor/socialize:postable';
3import { Profile } from 'meteor/socialize:user-profile';
4import { LinkableModel } from 'meteor/socialize:linkable-model';
1// React Native Imports
2import { PostableModel } from '@socialize/postable';
3import { Profile } from '@socialize/user-profile';
4import { LinkableModel } from '@socialize/linkable-model';
1export class PostableProfile extends PostableModel(Profile){
2    //methods here
3}
4
5PostableProfile.updateTransformFunction();
6
7LinkableModel.registerParentModel(PostableProfile);

For more information on using this package, please refer to API.md