Javascript - Integrate your frontend
Minimal example
The following example will display a different message for logged-in users vs logged out.
We’ll go into more detail about how it works and show more advanced examples down below.
import { createClient } from "@propelauth/javascript";
const client = createClient({
// Your auth URL can be found in your PropelAuth dashboard.
// You will have a different URL for your test and production environment, so you may want to store it in an ENV variable.
authUrl: "YOUR_AUTH_URL",
// PropelAuth uses short-lived tokens for security reasons.
// Enabling this feature will automatically refresh those tokens in the background. See technical details for more info.
enableBackgroundTokenRefresh: true,
});
client.getAuthenticationInfoOrNull().then((authInfo) => {
const element = document.getElementById("text");
if (authInfo) {
element.innerText = "You are logged in as " + authInfo.user.email; // User contains other metadata like userId
} else {
element.innerText = "You are not logged in";
}
});
How it works
For a detailed description of what’s going on under the hood, see here.
Configure
Go to your PropelAuth project and click Frontend Integration in the sidebar. You will see:
- Auth URL - This is where your authentication pages are hosted, and you will need this for the setup step.
- Test Environment - This is where you test. In our case, we’ll select Webflow and enter our subdomain.
- Login Redirect Path - After a user logs in, they will be redirected here.
- Logout Redirect Path - After a user logs out, they will be redirected here.
Installation
In your application, install the @propelauth/javascript library.
$ npm install --save @propelauth/javascript
and then you can import it
import { createClient } from "@propelauth/javascript";
Alternatively, you can use a CDN:
<script
src="https://www.unpkg.com/@propelauth/javascript@2.0.0/dist/javascript.min.js"
integrity="sha384-CnMW/GT96q1vxl3xq1fIbUrmXpDIsXVtY+/FpJW+rMSCgjlOWpbVfs5G0dg2bMN5"
crossorigin="anonymous"
></script>
then a global PropelAuth object will be created, and you can call createClient from it:
<script>
PropelAuth.createClient({...});
</script>
createClient
createClient
creates an authentication client which manages your access token,
fetches user information, and provides other useful authentication functions.
The client also refreshes auth information when a user switches focus to your tab or reconnects (if they were offline).
const authClient = createClient({
// The base URL where your authentication pages are hosted. You can find this under the Frontend Integration section for your project.
authUrl: "https://auth.yourdomain.com",
// If true, periodically refresh the access token in the background. This helps ensure you always have a valid token ready to go. Default true.
enableBackgroundTokenRefresh: true,
});
AuthenticationInfo and User metadata
There are a few key types that this library uses, here are their signatures:
type AuthenticationInfo = {
// An access token for the current logged in user
accessToken: string,
// When the access token will no longer be valid, in seconds.
expiresAtSeconds: number,
// A collection of functions for managing organization membership information for the current user
orgHelper: OrgHelper
// A collection of functions for checking roles (RBAC) and permissions for the current user
accessHelper: AccessHelper
// For all organizations the current user is a member of,
// this provides a mapping from their ids to metadata about them
// You should prefer orgHelper and accessHelper to orgIdToOrgMemberInfo.
orgIdToOrgMemberInfo?: OrgIdToOrgMemberInfo,
// Metadata about the current logged in user
user: User,
}
// Metadata about the current logged in user
type User = {
userId: string
email: string
emailConfirmed: boolean,
hasPassword: boolean,
username?: string
firstName?: string,
lastName?: string,
pictureUrl?: string,
locked: boolean,
enabled: boolean,
mfaEnabled: boolean,
createdAt: number,
lastActiveAt: number,
legacyUserId?: string
}
Organizations, Roles, and Authorization
Here are more types that you can find in the AuthenticationInfo object
type OrgHelper = {
// returns all orgs that the user is a member of
getOrgs: () => OrgMemberInfo[],
// returns all org ids that the user is a member of
getOrgIds: () => string[],
// returns org information for a given orgId
getOrg: (orgId: string) => OrgMemberInfo | undefined,
// returns org information for a given org by name
getOrgByName: (orgName: string) => OrgMemberInfo | undefined,
}
type AccessHelper = {
isRole: (orgId: string, role: string) => boolean
isAtLeastRole: (orgId: string, role: string) => boolean
hasPermission: (orgId: string, permission: string) => boolean
hasAllPermissions: (orgId: string, permissions: string[]) => boolean
}
type OrgMemberInfo = {
orgId: string,
orgName: string,
urlSafeOrgName: string
// The role of the user within this organization
userAssignedRole: string
userPermissions: string[]
}
Get current user information
authClient.getAuthenticationInfoOrNull(forceRefresh?: boolean): Promise<AuthenticationInfo | null>
If the user is logged in, this method returns authentication information about them, including their access token, user metadata, and, for B2B applications, organization information. See AuthenticationInfo schema for the full schema. Otherwise, this method returns null.
The promise will generally resolve immediately, without making an external request, unless our current access token is stale in which case it will fetch a new one.
If forceRefresh is true, this method will always fetch a new token. The default is false.
Example Usage
You can see a full list of method on the client in our reference. Here’s an example which creates signup/login/logout buttons that change based on whether the user is logged in or not.
<div id="display-when-logged-out">
<button id="signup">Sign Up</button>
<button id="login">Login</button>
</div>
<div id="display-when-logged-in">
<button id="account">Account</button>
<button id="logout">Logout</button>
</div>
<script>
const client = PropelAuth.createClient({
authUrl: "https://auth.yourdomain.com", // Change me
enableBackgroundTokenRefresh: true,
});
document.getElementById("signup").onclick = client.redirectToSignupPage;
document.getElementById("login").onclick = client.redirectToLoginPage;
document.getElementById("account").onclick = client.redirectToAccountPage;
document.getElementById("logout").onclick = client.logout;
client.addLoggedInChangeObserver((isLoggedIn) => {
if (isLoggedIn) {
document.getElementById("display-when-logged-in").style.display = "revert";
document.getElementById("display-when-logged-out").style.display = "none";
} else {
document.getElementById("display-when-logged-in").style.display = "none";
document.getElementById("display-when-logged-out").style.display = "revert";
}
});
</script>
You can test by going to your Auth URL and signing up. After logging in, you will see the Account and Logout buttons.
Troubleshooting
If you have any issues seeing the logged-in user, check the FAQ.
Making an authenticated HTTP request
There are a lot of ways to make HTTP requests in Javascript. You could use the Fetch API, XMLHttpRequest, or a library like axios
Whichever you choose, to make an authenticated
request on behalf of your user,
you’ll need to provide an access token. Access tokens are available from
getAuthenticationInfoOrNull.
You provide it in the request in the Authorization
header, like so:
Authorization: Bearer YOUR_ACCESS_TOKEN
With the Fetch API, this looks like:
function whoAmI(accessToken) {
return fetch("/whoami", {
method: "GET",
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
}
Next Steps
Done with your frontend? Next you can integrate your backend.