JavaScript
Integrate Nile Auth with any JavaScript backend
Learn how to integrate Nile Auth with any JavaScript backend framework. This guide shows a quickstart example for Node.js to help you get started using the Nile Auth SDK with your backend. In full-stack applications, user signup, authentication and even user profile UI can be handled automatically by the built-in routes and UI components. However, depending on your application’s requirements, you may need to use the SDK to directly call user management APIs in Nile Auth. This quickstart example shows how to do this. For more details on the Nile Auth JavaScript SDK, see the Nile Auth JS SDK documentation.
Quickstart
Nile SDK has APIs for working with users, tenants and sessions. In the example below, we’ll create a new user, login as that user, get and update the user’s profile information. We’ll then create a new tenant for that user, list users in the tenant and delete the tenant. Finally, we’ll show how to use the SDK to protect a route by checking if the user is authenticated and sign the user out.
Create a new NodeJS project
Install the Nile Auth SDK
Import and initialize the Nile Auth SDK
We’ll use a single file for the entire example. Create a new file called nile-qs.js
and add the following code:
Configure the Nile Auth SDK with your environment variables
The Nile
object can be configured directly from environment variables, which you can get from Nile Console.
Place them in a .env
file in the root of your project.
Create a new user
Let’s start by creating a new user, so we can use it for the rest of the example:
Login a user with email and password
To login a user with email and password, you can use the following code:
This will set nile.api.headers
to include the user’s session token and the CSRF token. These will automatically be added to all subsequent requests.
Get the user's profile information
Next, let’s get the user’s profile information by calling the me
API:
Update a user's profile information
Update a user’s profile information by calling the update
API:
Create a new tenant
You can create a new tenant by calling the createTenant
API:
Update a tenant
You rename a tenant by calling the updateTenant
API after setting the tenantId
to the tenant you want to update:
List users in a tenant
You can list the users in a tenant by calling the listUsers
API after setting the tenantId
to the tenant you want to list users from:
Delete a tenant
You can mark a tenant as deleted by calling the deleteTenant
API after setting the tenantId
to the tenant you want to delete:
deleted
field to current timestamp. Check if a user is authenticated
A key use case for the server-side SDK is to check if a user is authenticated before allowing them to access a resource:
Sign out a user
Once we are done, we can sign out the user by calling the signOut
API:
Run the example
To see the example in action, run the following command:
Security Considerations
- Never log or otherwise store user passwords in plain text
- Never log or store authentication tokens or session information
- Always validate and sanitize user input before passing it to Nile Auth APIs
- Avoid exposing detailed error messages to clients that might reveal system information
- Implement rate limiting for authentication attempts to prevent brute force attacks
- Log authentication failures and suspicious activities for monitoring
- Implement appropriate error responses that don’t leak sensitive information
- Keep your Nile SDK and dependencies up to date
For more information and examples
Best Practices
-
Error Handling Implement comprehensive error handling for all authentication flows. Return user-friendly error messages while logging detailed errors server-side. Use try-catch blocks around all authentication operations.
-
Session Management Regularly validate sessions. Implement proper session cleanup on logout. Use secure session storage methods.
-
Code Organization Separate authentication logic into dedicated middleware. Create reusable authentication utilities. Keep configuration in environment variables.
-
Testing Test authentication flows thoroughly. Include both success and failure scenarios. Mock external authentication providers in tests.
Troubleshooting
Common Issues
-
Invalid Session Token Verify that user authentication was successful. Verify the token is being properly passed in requests. Check if the session has expired. Ensure the token format is correct.
-
CORS Issues Ensure your server is configured to accept requests from your client domain. Check that necessary CORS headers are being set.
-
User Creation Failures Make sure you properly handle common user mistakes: Validate email format before submission and check for duplicate email address error that may be returned.
Debug Tips
- Enable debug logging:
- Monitor network requests in your browser’s developer tools
- Check server logs for detailed error messages
- Verify environment variables are properly set
For additional support, visit our Discord community or GitHub discussions.
Was this page helpful?