@mifistix-cloud/auth
v2.0.5
Published
Authentication SDK for Mifistix Cloud - User authentication and session management
Downloads
402
Readme
@mifistix-cloud/auth
Authentication SDK for Mifistix Cloud - User authentication and session management.
License
This module is licensed for internal use within Mifistix only. See LICENSE for details.
Installation
npm install @mifistix-cloud/authQuick Start
const { initializeApp } = require('@mifistix-cloud/app');
const { getAuth, signInWithEmailAndPassword, createUserWithEmailAndPassword, signOut } = require('@mifistix-cloud/auth');
// Initialize app
const app = initializeApp({
apiKey: 'your-api-key',
projectId: 'your-project-id'
});
// Get auth instance
const auth = getAuth(app);
// Sign in
const { user } = await signInWithEmailAndPassword(auth, '[email protected]', 'password123');
console.log('Signed in as:', user.email);
// Sign up
const { user: newUser } = await createUserWithEmailAndPassword(auth, '[email protected]', 'password123');
console.log('Created user:', newUser.uid);
// Sign out
await signOut(auth);API Reference
getAuth(app)
Get authentication instance for an app.
Parameters:
app(Object, required): Initialized app instance
Returns: Auth client instance
Example:
const auth = getAuth(app);createUserWithEmailAndPassword(auth, email, password)
Create a new user with email and password.
Parameters:
auth(Object): Auth instanceemail(string, required): User email (validated)password(string, required): User password (min 6 characters, validated)
Returns: Promise with { user } object
Example:
const { user } = await createUserWithEmailAndPassword(auth, '[email protected]', 'password123');signInWithEmailAndPassword(auth, email, password)
Sign in with email and password.
Parameters:
auth(Object): Auth instanceemail(string, required): User email (validated)password(string, required): User password (validated)
Returns: Promise with { user } object containing token
Example:
const { user } = await signInWithEmailAndPassword(auth, '[email protected]', 'password123');
console.log('Auth token:', user.token);signOut(auth)
Sign out current user.
Parameters:
auth(Object): Auth instance
Returns: Promise
Example:
await signOut(auth);getCurrentUser(auth)
Get currently signed-in user.
Parameters:
auth(Object): Auth instance
Returns: User object or null
Example:
const user = getCurrentUser(auth);
if (user) {
console.log('Current user:', user.email);
}onAuthStateChanged(auth, callback)
Listen for authentication state changes.
Parameters:
auth(Object): Auth instancecallback(Function): Callback function that receives user object
Returns: Unsubscribe function
Example:
const unsubscribe = onAuthStateChanged(auth, (user) => {
if (user) {
console.log('User signed in:', user.email);
} else {
console.log('User signed out');
}
});
// Later: unsubscribe();Security Features
- Email Validation: Validates email format before API calls
- Password Validation: Enforces minimum 6 character password
- Session Persistence: Securely stores session in localStorage
- Token Management: Handles auth tokens securely
- Error Handling: Throws
AuthenticationErrorfor auth failures - CSRF Protection: Project ID validation
Session Management
The auth SDK automatically:
- Persists user session to localStorage
- Restores session on page reload
- Notifies listeners of auth state changes
- Clears session on sign out
Error Handling
try {
const { user } = await signInWithEmailAndPassword(auth, email, password);
} catch (error) {
if (error.name === 'AuthenticationError') {
console.error('Authentication failed:', error.message);
} else if (error.name === 'ValidationError') {
console.error('Invalid input:', error.message);
}
}Architecture
auth/
├── src/
│ ├── core/
│ │ └── AuthClient.js # Main auth client
│ ├── services/
│ │ ├── AuthService.js # Authentication operations
│ │ └── SessionManager.js # Session persistence
│ ├── utils/
│ │ └── helpers.js # Helper functions
│ ├── types/
│ │ └── index.js # Type definitions
│ └── config/
│ └── constants.js # Configuration constants
└── index.jsLicense
MIT
