The Memberstack DOM package has methods for signing members up, logging them in and out, and keeping them logged in, all from your front-end JavaScript. This article covers email/password signup and login, passwordless login with a code sent by email, and social login with providers like Google and Facebook. It also explains how sessions and tokens work and how to handle errors.
View the full DOM Package documentation for more guidance.
Note: The Memberstack DOM Package can be accessed through the Webflow Package as well by using window.$memberstackDom.signupMemberEmailPassword(...) instead of memberstack.signupMemberEmailPassword(...).
1) Concepts
Before diving into the code, let's review some key concepts:
- Authentication - The process of verifying user identity.
- Authorization - Determining what resources and actions a user can access.
- Sessions - After a member signs up or logs in, the DOM package stores their session in the browser, so they stay logged in across pages and visits until they log out or the session expires.
- JWT - JSON Web Token. A member's session is a JWT (their access token), and the DOM package sends it with its requests for you.
2) Email/Password Authentication
The most common way to authenticate is by having a member enter an email and password. The DOM package provides two methods for this:
Signup
Creates a new member with an email and password:
// Sign up a new member
memberstack.signupMemberEmailPassword({
email: "member@example.com",
password: "password123"
}).then(({ data }) => {
// The new member is now logged in
console.log(data.member.id, data.member.auth.email);
}).catch((error) => {
// e.g. error.code is "email-already-in-use" or "invalid-password-too-short"
console.log(error.code, error.message);
});-
emailandpasswordare required. The password must be at least 8 characters. - The signup, login and logout methods return a promise: use
.thenand.catch, orawaitinsidetry/catch. - On success, the new member is logged in. The promise resolves to
{ data }:data.memberis the new member anddata.tokens.accessTokenis their JWT. - Handle errors such as an email that already has an account or a password that is too short (see Handling Errors below).
Login
Logs in an existing member with their email and password:
// Log in an existing member
memberstack.loginMemberEmailPassword({
email: "member@example.com",
password: "password123"
}).then(({ data }) => {
// The member is now logged in
console.log(data.member.id, data.member.auth.email);
}).catch((error) => {
// Wrong password, or no member with this email: error.code is "invalid-credentials"
console.log(error.code, error.message);
});- Again,
emailandpasswordare required. - On success, the promise resolves to
{ data }withdata.memberanddata.tokens. The DOM package saves the session for you, so you don't need to store the token yourself. - A wrong password and an email with no account both fail with the same
invalid-credentialserror.
3) Passwordless Authentication
Passwordless authentication lets members sign up or log in without a password. Memberstack emails them a six-digit code, they type it into your site, and you pass it to Memberstack. The email contains the code, not a login link. Each code works once and expires after 10 minutes by default. You can edit the email and the code expiration in the Memberstack dashboard under Emails → Transactional → Passwordless auth. Read more here.
Send a Login Code
// Email an existing member a 6-digit login code
memberstack.sendMemberLoginPasswordlessEmail({
email: "member@example.com"
}).then(() => {
// Code sent: show a field where the member can type it
}).catch((error) => {
// No member with this email: error.code is "passwordless-email-not-found"
console.log(error.code, error.message);
});- Sends the six-digit code to a member who already has an account.
Login with the Code
// Log in with the code the member typed
memberstack.loginMemberPasswordless({
email: "member@example.com",
passwordlessToken: "123456" // the 6-digit code from the email
}).then(({ data }) => {
// The member is now logged in
console.log(data.member.id, data.member.auth.email);
}).catch((error) => {
// Wrong, already used or expired code, or 5 wrong tries: error.code is "invalid-passwordless-login-code"
console.log(error.code, error.message);
});- Pass the same
emailand the code aspasswordlessToken. - On success, the member is logged in and the promise resolves to
{ data }withdata.memberanddata.tokens.
Send a Signup Code
// Email a new member a 6-digit signup code
memberstack.sendMemberSignupPasswordlessEmail({
email: "new-member@example.com"
}).then(() => {
// Code sent: show a field where they can type it
}).catch((error) => {
// The email already has an account: error.code is "email-already-in-use"
console.log(error.code, error.message);
});- New members need a signup code: the login code above only goes to emails that already have an account.
Signup with the Code
// Create the member with the code they typed
memberstack.signupMemberPasswordless({
email: "new-member@example.com",
passwordlessToken: "123456" // the 6-digit code from the email
}).then(({ data }) => {
// The new member is now logged in
console.log(data.member.id, data.member.auth.email);
}).catch((error) => {
// Wrong, already used or expired code, or 5 wrong tries: error.code is "invalid-passwordless-login-code"
console.log(error.code, error.message);
});- Pass the same
emailand the code aspasswordlessToken. - On success, the new member is created and logged in, and the promise resolves to
{ data }withdata.memberanddata.tokens.
4) Social Authentication
Let members sign up or log in with providers like Google and Facebook. First turn the provider on in the Memberstack dashboard under Authentication → Auth providers. Each method opens a popup where the member signs in with the provider.
// Sign up with Google (opens a popup)
memberstack.signupWithProvider({
provider: "google"
}).then(({ data }) => {
// Signed up, or logged in if this Google account is already linked to a member
console.log(data.member.id, data.member.auth.email);
}).catch((error) => {
// e.g. the email already has an account that doesn't use Google: error.message says how to log in
console.log(error.message);
});// Connect Facebook to the member who is logged in (opens a popup)
memberstack.connectProvider({
provider: "facebook"
}).then(({ data }) => {
// data.providers lists the member's connected providers
console.log(data.providers);
}).catch((error) => {
// e.g. the Facebook account's email doesn't match the member's email
console.log(error.message);
});// A blocked or closed popup doesn't reject the promise. Listen for this event instead:
window.addEventListener("memberstack:popup", (event) => {
const { status, action, provider } = event.detail;
// status is "blocked" or "closed"; action is "signup", "login" or "connect"
console.log(status, action, provider);
});- Pass the provider name in lowercase, like
"google"or"facebook". - To log in existing members without ever creating a new one, use
loginWithProvider({ provider: "google" })instead (unless you passallowSignup: true). -
connectProviderlinks another provider to the member who is logged in, so they can log in with it too. A member must be logged in, and the provider account must use the same email as the member. - If the member closes the popup, or the browser blocks it, the promise does not reject. Use the
memberstack:popupevent to react, for example by showing the email form. The same happens if the provider isn't turned on, or if you callconnectProviderwhile nobody is logged in: the popup shows an error, and you only hear about it through thememberstack:popupevent when it's closed.
5) Sessions and Tokens
// Who is logged in? data is null when nobody is
memberstack.getCurrentMember().then(({ data: member }) => {
if (member) {
// The logged-in member's JWT, e.g. to send to your own server
const token = memberstack.getMemberCookie();
}
});
// Log out when the member clicks your logout button.
// This removes the stored session from this browser.
document.querySelector("#logout").addEventListener("click", () => {
memberstack.logout();
});- After a signup or login, the DOM package stores the member's JWT in the browser: in
localStorageby default, or in the_ms-midcookie if you initialize it withuseCookies: true. - Sessions last 14 days by default. You can change this in the Memberstack dashboard under Authentication → Security → Session length.
-
getCurrentMember()resolves to{ data: null }when nobody is logged in, so checkdata, not the result itself. - To trust a member on your own server, send the token there and verify it with
verifyToken({ token, audience })on a client from the@memberstack/adminpackage (version 1.8.0 or later), passing your App ID asaudience. Logging out doesn't revoke the token: a copy of it keeps verifying until it expires. See Using Permissions and Token Verification to Secure Your Site on a Custom Backend. - To run code when a member logs in or out, use
onAuthChange. See Events and Webhooks with Memberstack DOM.
6) Handling Errors
When a method fails, the promise rejects with an object that has a message and, for most errors, a code. Show message to the member, and check code in your logic. Common codes:
-
invalid-credentials- Login with a wrong password, or with an email that has no account. -
email-already-in-use- Signup with an email that already has an account. -
invalid-password-too-short- Signup with a password shorter than 8 characters. -
invalid-email- Signup with an email that isn't a valid address. -
passwordless-email-not-found- A login code was requested for an email that has no account. -
invalid-passwordless-login-code- A passwordless code that is wrong, already used or expired, or has been tried wrongly 5 times. -
no-password-set- Password login for a member who has no password (for example, one who signed up passwordless).
This covers the basics of member authentication with the DOM package!
Check out the other articles in this series:
- Getting Started with the Memberstack DOM Package (Front-end API)
- Working with Memberstack DOM Package Modals
- Managing App Data with the Memberstack DOM Package
- Member Authentication with the Memberstack DOM Package
- Member Billing and Checkout with the Memberstack DOM Package
- Member Management with the Memberstack DOM Package
- Events and Webhooks with Memberstack DOM
Comments
3 comments
Is it also possible to generate an one time link for my members? So that they are able to sign up and after that I would send them a one time link which also contains email and password for signing in without using a login form. It should be easy as possible to sign in.
But how I am able to set up a one-time-link for specific member, so that they can sign in only by clicking the link?
Hey Oliver Sienel
Thank you for reaching out!
I want to make sure I fully understand your request. It sounds like you’re looking to generate a one-time link for your members that allows them to sign in without using a traditional login form, ideally using their email and password after they’ve signed up.
Memberstack does offer a one-time password (OTP) option, which allows members to sign up using their email and then receive an email with a link to sign in. However, at the moment, there isn't a feature that allows you to generate a one-time login link for members after sign-up that bypasses the need for a password.
For secure logins, we rely on members either:
If I misunderstood your question or you’re looking for a different type of functionality, please let me know! I'd be happy to explore any workarounds or solutions that might fit your use case.
Thanks again for posting!
Hi Josh,
thanks for the post and the answer you gave Oliver 10 month ago.
If I see it correctly, the post was updated 2 months ago and now it will be possible to use passwordless login to login members?
Let me know if I misunderstood this.
Please sign in to leave a comment.