Skip to content

Instantly share code, notes, and snippets.

@csymlstd
Last active March 28, 2018 18:42
Show Gist options
  • Save csymlstd/ca751a2a61ce8a621b08ff4918592365 to your computer and use it in GitHub Desktop.
Save csymlstd/ca751a2a61ce8a621b08ff4918592365 to your computer and use it in GitHub Desktop.
auth0 wordpress

Configuring Auth0

Create a client and api following auth0 wordpress docs, https://auth0.com/docs/cms/wordpress/configuration

Adding app_metadata to user token

  1. Add the app_metadata scope to the api.
  2. Add app_metadata to a user
{
  "authorization": {
    "groups": [
      "Admins"
    ],
    "roles": [],
    "permissions": []
  }
}
  1. Create a rule
function (user, context, callback) {
  // copy user metadata value in id_token
  context.idToken.authorization = user.app_metadata.authorization;

  callback(null, user, context);
}
  1. Add filter to wordpress to parse metadata before login, https://www.diycode.cc/projects/auth0/wp-auth0?sync=1
add_action('auth0_user_login', function($user_id, $user_profile, $is_new) {
	if(isset($user_profile->authorization)) {
		if(in_array('Admins', $user_profile->authorization->groups)) {
			wp_update_user(array(
				'ID' => $user_id,
				'role' => 'administrator'
			));
		}
	}
}, 0, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment