Skip to content

Instantly share code, notes, and snippets.

@tomthorogood
Created November 29, 2012 16:16
Show Gist options
  • Select an option

  • Save tomthorogood/4170086 to your computer and use it in GitHub Desktop.

Select an option

Save tomthorogood/4170086 to your computer and use it in GitHub Desktop.
Add a Mogu User
# This will show a complete example of adding a user into a Mogu application.
# First, you will need to provide a username andpassword. Both of these widgets should be named widgets. In this example, we use an email address as a username to also exemplify adding a contact email for password resets.
# The username is going to be the user's email address.
# We are going to create a validator for this field, which will
# be updated in real time as a user enters their email.
# We also add a tooltip here.
widgets["registration:form:username"] = \
{
type : "{input}",
contents: "Please enter your email address."
validator: "email",
tooltip: "Enter your email address here: example@email.com"
name: "register:username"
}
# The password field is similar to the email field, except
# we declare a different validator.
widgets["registration:form:password"] = \
{
type : "{password}",
contents: "Enter a Password",
validator: "password",
tooltip: "Your password should be at least 8 characters in length.",
name: "register:password"
}
# We also want a field to confirm one's password. This uses the 'test'
# event, which works in the same way as a validator, but is able to compare
# the value of a different widget
widgets["registration:form:confirm_password"] = \
{
type : "{password}",
contents: "Confirm Your Password",
tooltip: "Enter the same password that you had above."
name: "register:pw_match"
}
# We'll also need a place to store messages to the user:
widgets["registration:form:feedback"] = \
{
type : "{text}",
contents: " "
name : "register:feedback"
}
# And lastly, something for the user's to submit their form:
widgets["registration:form:submit"] = \
{
type : "{text}",
contents: "Register!"
}
## Next, we'll need to attached events to all of the above. ##
# With input fields that have validators, they will automatically emit
# a "success" or "fail" signal whenever a key is pressed.
# We can use this signal to give feedback to the user.
events["registration:form:username"] = [
# If the user has not yet entered a valid email address,
# let them know what we're expecting:
{
trigger : "{fail}",
action: "{set_text}",
listener: "|register:feedback|",
message: "Please enter a valid email address."
},
# If the user has entered a valid email address, clear the
# feedback field.
{
trigger : "{succeed}",
action: "{set_text}",
listener: "|register:feedback|",
message: " "
}
]
# We'll do something similar with the password field:
events["registration:form:password"] = [
{
trigger : "{fail}",
action: "{set_text}"
listener: "|register:feedback|",
message: "Your password should be at least 8 characters."
},
{
trigger : "{succeed}",
action: "{set_text}",
listener: "|register:feedback|",
message : " "
}
]
# Now, for the password confirmation field. This is almost like the above two,
# but we have to roll our own validation:
events["registration:form:confirm_password"] = [
# This is a simple event that says "when the key is pressed, see if
# the value of this field matches the value of the field named 'register:password'"
# This will emit a "success" or "fail" signal as above.
{
trigger : "{keyup}",
action: "{match}",
listener: "{self}",
message: "|register:password| $value$"
}
},
{
trigger : "{fail}",
action: "{set_text}",
listener: "|register:feedback|",
message: "Your passwords don't match!"
},
{
trigger : "{succeed",
action: "{set_text}",
listener: "|register:feedback|"
message: " "
}
]
# We've got our user data, so now all we have to do is
# hand it off to Mogu for registration!
events["registration:form:submit"] = [
# Before we continue, make sure that there's nothing in the feedback box.
# This will indicate that there are no errors:
{
trigger: "{click}",
action: "{match}",
message: " "
listener: "|register:feedback| $value$"
},
{
trigger: "{succeed}",
action: "{slot}",
listener: "USERID",
message: "|register:username| $value$"
},
{
trigger: "{succeed}",
action: "{slot}",
listener: "USERAUTH",
message: "|register:password| $value$"
},
{
trigger: "{succeed}",
action: "{register_user}"
},
# The last event is a little inelegant, but will, after
# registering the user, store their email address in the
# contact email field for use internally if they need to
# reset their password.
# The only part of this that is very odd is the `!DNP!`
# command, which instructs Mogu to stop parsing the token.
# Otherwise, the listener would be the value stored at the
# contact email node, which in this case would be non-existent.
{
trigger: "{succeed}",
action: "{store}",
listener: "!DNP! [contact] &email&"
message: "|register:username| $value$"
}
]
# Last, we need to set up our validators. This is
# very easy:
validators["email"] = \
{
type : "regex",
# This is an email regex recipe #
test: " "^[a-zA-Z]+((['\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*\s+< (\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})>$|^(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})$"
}
validators["password"] = \
{
type : "regex",
# this regex recipe allows any character as long as the entire string is between 8 and 64 characters long.
test: "^((.){8,64})$"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment