npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pointyapi-user-activation

v0.0.9

Published

*"User Activation module for PointyAPI"*

Downloads

3

Readme

PointyAPI User Activation Module

Created by Stateless Studio

Step 1: Installation

npm i pointyapi-user-activation

You'll also need the Mailer PointyAPI Module.

Step 2: Create email templates

Create a Welcome template (in /assets/html/emails or wherever you store templates):

/assets/html/emails/welcome.html

<table align="center" border="0" cellpadding="0" cellspacing="0" class="email-container" style="width:600px">
	<tbody>
		<tr>
			<td style="text-align:center">
				<h2>{{fname}},</h2>
				<p>Thank you for signing up for {{SITE_TITLE}}! Please click the button to activate your account:</p>
				<br>
				<a class="button-a" href="{{activation_link}}" style="background: #2674fb; border: 15px solid #2674fb; padding: 0 10px;color: #ffffff; font-family: sans-serif; font-size: 13px; line-height: 1.1; text-align: center; text-decoration: none; display: block; border-radius: 3px; font-weight: bold; max-width: 200px; margin: auto;">Activate Account!</a>
			</td>
		</tr>
	</tbody>
</table>

and an Email Updated template:

/assets/html/emails/user-email-updated.html

<table align="center" border="0" cellpadding="0" cellspacing="0" class="email-container" style="width:600px">
	<tbody>
		<tr>
			<td style="text-align:center">
				<h2>{{fname}},</h2>
				<p>Someone has tried to update your email. If this was you, please confirm this change:</p>
				<br>
				<a class="button-a" href="{{activation_link}}" style="background: #2674fb; border: 15px solid #2674fb; padding: 0 10px;color: #ffffff; font-family: sans-serif; font-size: 13px; line-height: 1.1; text-align: center; text-decoration: none; display: block; border-radius: 3px; font-weight: bold; max-width: 200px; margin: auto;">Confirm Email</a>
			</td>
		</tr>
	</tbody>
</table>

Add these template files to our sample data module:

/src/test-data.ts

	await addResource(EmailTemplate, {
		keyname: 'welcome',
		subject: 'Welcome to ' + process.env.SITE_TITLE,
		body: fs.readFileSync('assets/html/emails/welcome.html', {
			encoding: 'utf8'
		})
	});

	await addResource(EmailTemplate, {
		keyname: 'user-email-updated',
		subject: 'Your email address has been updated',
		body: fs.readFileSync('assets/html/emails/user-email-updated.html', {
			encoding: 'utf8'
		})
	});

Step 3: Initialize UserActivationModule

Import the module into your server, and run init in the pointy.before function.

/src/server.ts

...
import { mailer } from './Mailer';
import { User } from './models/user';

// Import UserActivationModule
import { UserActivationModule } from 'pointyapi-user-activation';

...

pointy.before = (app) => {
	...
	UserActivationModule.init(mailer, User);
	...
}

...

Step 4: Add tempEmail to your User entity

@Entity()
class User extends BaseUser {
	...

	// Email (temporary)
	@Column({ nullable: true, unique: true })
	@Matches(/^[_a-zA-Z0-9\.]+@[0-9a-zA-Z_]+?\.[a-zA-Z]+$/i)
	@IsOptional()
	@OnlySelfCanRead()
	@OnlySelfCanWrite()
	public tempEmail: string = undefined;

	...
}

Step 5: Setup User Hooks

The UserActivationModule will run during User hooks.

Add the following hooks:

/src/models/user.ts

	...

	/**
	 * beforePost
	 */
	public async beforePost(request: Request, response: Response) {
		// Lowercase username
		if (this.username) {
			this.username = this.username.toLowerCase();
		}

		// Lowercase email
		if (this.email) {
			this.email = this.email.toLowerCase();
		}

		// Run UserActivationModule hook
		const status = await UserActivationModule.beforePost(
			this,
			request,
			response
		).catch((error) => {
			pointy.error(error);
		});

		// Check status
		if (status) {
			// Success
			this.thumbnail = process.env.DEFAULT_THUMBNAIL;

			return super.beforePost(request, response);
		}
		else {
			// Failed
			return false;
		}
	}

	/**
	 * beforeUpdate
	 */
	public async beforePatch(request: Request, response: Response) {
		// Lowercase username
		if (this.username) {
			this.username = this.username.toLowerCase();
		}

		// Lowercase email
		if (this.email) {
			this.email = this.email.toLowerCase();
		}

		// Run UserActivationModule hook
		const status = await UserActivationModule.beforePatch(
			this,
			request,
			response
		).catch((error) => {
			pointy.log(error);
		});

		// Check if email update
		if (status) {
			return super.beforePatch(request, response);
		}
		else {
			return false;
		}
	}


	...

Step 6: Add routes

/src/routes/user.ts

// Activation routes
router.post('/activate/', UserActivationModule.activationEndpoint);
router.post('/resend-activation/', UserActivationModule.resendEndpoint);