JavaScript
Integrate Nile Auth with any JavaScript backend
JavaScript Backend Integration
Learn how to integrate Nile Auth with any JavaScript backend framework.
Overview
This guide covers the general principles of integrating Nile Auth with any JavaScript backend framework.
Installation
Core Concepts
Nile Instance
A configured Nile instance handles proxying requests to the Nile Auth API. Your client connects to your server, which then formats and ensures the requests can be handled by the Nile Auth API.
The SDK is designed to make clients talking through your server to the Nile auth service as transparent as possible.
In addition, you can use the SDK to directly access the Nile Auth API from your server.
Authentication Flow
1. User Initiates Authentication
- The user clicks a “Sign in” button.
- This action triggers a
signIn
method with the chosen provider. Your server handles all requests, which in most cases is simply forwarding them on to the Nile auth service with some additional information to help identify the client.
2. Redirect to Provider (OAuth Flow)
- If an OAuth provider (e.g., Google, GitHub) is used, the user is redirected to the provider’s authentication page. This works by Nile auth returning redirects to your application, which the SDK handles in order to send the user to the provider.
- The user enters their credentials and grants permission to the application. Because your server is handling the requests, the user is redirected back to your application.
3. Provider Callback & Token Exchange
- After successful authentication, the provider redirects the user back to your application, which proxies the request to the Nile auth service.
- Nile auth exchanges the authorization code for an access token and forwards the authorization information to your server, which in turn would just pass that on to the client.
4. Session Creation
- Via your service, nile auth provides a secure cookie.
- The cookie includes basic user information, which can be accessed using the
nile.api.auth.getSession
or a full user profile vianile.api.auth.me
5. Accessing the Session
- A session is always within the context of a request. You can access session data using:
A Nile auth application server must respond to API requests. To see a full list of available API routes, check out the API Reference.
User Management
While user signup, authentication and even user profile UI is handled automatically by the built-in routes and UI components, you can use the SDK to directly call user management APIs in Nile Auth.
For example, to login a user with email and password, you can use the following code:
This will set nile.token
to the user’s session token, which you can use to make authenticated requests to the Nile Auth API.
You can get the user’s profile information by calling the me
API:
You can also create a new user by calling the create
API:
Or create a user in a specific tenant by setting tenantId
before creating the user:
You can also update a user’s profile information by calling the update
API:
Tenant Management
Nile Auth supports multi-tenancy out of the box and includes tenant management APIs. You can create a new tenant by calling the createTenant
API:
You rename a tenant by calling the updateTenant
API after setting the tenantId
to the tenant you want to update:
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:
You can mark a tenant as deleted by calling the deleteTenant
API after setting the tenantId
to the tenant you want to delete:
You can add users to a tenant and remove them by calling linkUser
and unlinkUser
APIs:
API Authentication
To authenticate API requests, you can use the session token:
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
Framework-Specific Guides
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?