Skip to content

Instantly share code, notes, and snippets.

@passatgt
passatgt / stormpath-api-keys.js
Created August 8, 2016 19:06
Routes to generate, list and delete api keys using Node.js Stormpath Express
// Create api key
module.exports.createApiKey = function(req, res) {
req.user.createApiKey(function(err, apiKey){
if(err) return helpers.handleError(err,res);
return res.status(201).send({status: 'success', code: 'api_key_created', message: 'Api key created.', data: apiKey});
});
}
@passatgt
passatgt / stormpath-express-logout.js
Created August 9, 2016 15:20
Delete all access and refresh tokens for logged in user in Stormpath Express
//Sign out -> delete all access and refresh tokens
module.exports.signOut = function(req, res){
//Delete refresh tokens
req.user.getRefreshTokens(function(err,refreshTokens){
refreshTokens.each(deleteToken, function (err) {
if(err) {
res.status(400).send({ message: 'Oops! There was an error: ' + err.userMessage});
} else {
@passatgt
passatgt / stormpath-login-email.js
Last active August 24, 2016 21:30
Send en email to a user with Stormpath Express on new login attempt
const async = require('async');
const parser = require('ua-parser-js');
function checkMultipleLogin(account, req, res) {
var sendemail = true;
var uagent = req.headers['user-agent'];
var ua = parser(uagent);
var ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
@passatgt
passatgt / stormpath-custom-email-verification.js
Last active December 14, 2016 18:29
Custom email verification logic for Stormpath
// Init Stormpath for user management and authentication
app.use(stormpath.init(app, {
//Your other options you might have
expand: {
//The verification code will be saved to customData, so expand it first
customData: true
},
postRegistrationHandler: function(account, req, res, next) {
//Create a verification code to verify email address later
helpers.createVerificationCode(account);
@passatgt
passatgt / stormpath-express-failed-login-attempt.js
Last active April 15, 2019 20:00
Log failed login attempts
preLoginHandler: function(formData, req, res, next) {
var authRequest = {
username: formData.login,
password: formData.password
};
//Try to authenticate the user
req.app.get('stormpathApplication').authenticateAccount(authRequest, function(err, result) {
//if (err) return helpers.handleError(err,res);
if (err && err.code == 7100) {
@passatgt
passatgt / stormpath-express-idsite-token-exchange.js
Last active February 21, 2017 20:39
Exchange the stormpath token to an oauth access token with id site
//This works with the node.js stormpath sdk
const stormpath = require('stormpath');
/*
This route will redirect to the Stormpath ID Site
*/
module.exports.idSiteRedirect = function(req,res,next) {
//Create ID Site Url
var url = req.app.get('stormpathApplication').createIdSiteUrl({
@passatgt
passatgt / woocommerce_get_active_shipping_methods.php
Created November 14, 2019 23:55
Get an array of all available active shipping methods from all zones
<?php
function get_shipping_methods() {
$active_methods = array();
$custom_zones = WC_Shipping_Zones::get_zones();
$worldwide_zone = new WC_Shipping_Zone( 0 );
$worldwide_methods = $worldwide_zone->get_shipping_methods();
foreach ( $custom_zones as $zone ) {
$shipping_methods = $zone['shipping_methods'];
foreach ($shipping_methods as $shipping_method) {
@passatgt
passatgt / class-webhook.php
Created November 17, 2019 10:32
Create a new custom Webhook in WooCommerce with a custom response
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WC_Sample_Webhook', false ) ) :
class WC_Sample_Webhook {
@passatgt
passatgt / vp-products-pdf-pricelist.php
Created May 18, 2020 10:50
Creates a new page under WooCommerce to display a list of all the products and prices, which you can print out for a simple pricelist.
<?php
/*
Plugin Name: VP Árlista export
Plugin URI: http://visztpeter.me
Author: Viszt Péter
Version: 1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
@passatgt
passatgt / wc-only-checkout.php
Created May 28, 2020 11:38
Use WooCommerce with only a checkout page
<?php
/*
I built custom product pages with a simple add to cart button(using the add_to_cart shortcode) for a webshop.
There was no need for category pages, shop pages, cart or anything like that, just a simple product page with an add to cart button that redirects to checkout directly.
Here is how you can do this:
1. Delete the My Account page
2. Delete the Shop page
*/